samedi 12 mai 2018

How can I output a files random selection to a new command

I'm trying to practice some coding and I am trying to make OpenVPN pick a random OVPN file for my Qubes OS laptop, and eventually for my Pop!_OS one. How do I output the commands from pick.sh into openvpn correctly e.g.

openvpn --config (result from pick.sh)

the Pick.sh is as such

#!/bin/bash
# Reads a given directory and picks a random file.

# The directory you want to use. You could use "$1" instead if you
# wanted to parametrize it.
#DIR="./openvpn"
# DIR="$1"

# Internal Field Separator set to newline, so file names with
# spaces do not break our script.
IFS='
'

if [[ -d "${DIR}" ]]
then
  # Runs ls on the given dir, and dumps the output into a matrix,
  # it uses the new lines character as a field delimiter, as explained above.
  file_matrix=($(ls "${DIR}"))
  num_files=${#file_matrix[*]}
  # This is the command you want to run on a random file.
  # Change "ls -l" by anything you want, it's just an example.
  ls --file-type ovpn "${DIR}/${file_matrix[$((RANDOM%num_files))]}"
fi

exit 0




Aucun commentaire:

Enregistrer un commentaire