1

I have a system with over 20 network devices backing up running config to a backup server. On the server side I have to check if the backup jobs are still working. So I have this script:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

device="$1"
backupdir="/backup/$device"
latestbackup="`find $backupdir -mmin -1440 -name "$device*"`"
if [[ -f $latestbackup ]];
 then
  echo "`date` - $device - latest backup is $latestbackup"
 else
  echo "`date` - $device - backup not working"
fi

so I can run this script with following statement:

./backup.sh router1

Is there anyway to make this script available with many more arguments? (over 20) like:

./backup.sh router1 router2 router3 ...

2 Answers 2

2

You can run the script in a loop, each time with another argument:

for x in router1 router2 router3 ...; do ./backup.sh $x; done;
Sign up to request clarification or add additional context in comments.

4 Comments

A pragmatic alternative to having to modify the script.
@mklement0 if it's something that will be used like this more than once I'd change the script, but for "one off" I would hack it with the one-liner above ;)
Fully agreed; that's exactly the sentiment I was trying to express in my comment; unfortunately, someone voted your answer down.
@mklement0 I see lots of trolls here lately (I consider anyone who downvotes and does not comment to explain why as a troll).
0

You can use the $@ special parameter here -- which represents the entire argument string (1st argument and later) in conjunction with a for loop.

The code would look like this:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

for device in "$@"; do
  backupdir="/backup/$device"
  latestbackup="`find $backupdir -mmin -1440 -name "$device*"`"
  if [[ -f $latestbackup ]];
    then
    echo "`date` - $device - latest backup is $latestbackup"
  else
    echo "`date` - $device - backup not working"
  fi
done

4 Comments

While $@ will probably work in this particular case, it is generally advisable to use "$@" - note the enclosing double-quotes - for robustness, as only that ensures that the original partitioning of the command line into arguments is preserved. Terminology quibble: $@ is a special parameter in Bash parlance, not an operator.
Thanks @mklement0, updated my answer. Didn't know that "$@" was preferred, as it still worked when I tested it out initially, but your explanation makes sense.
@aguibert: Thanks for updating your answer; to give a concrete example as to why unquoted $@ is problematic: bash -c 'for a in $@; do echo "[$a]"; done' '(unused)' 'nospaces' 'with spaces' '*' - as you'll see, word splitting and pathname expansions (globbing) are applied, so that the 2nd argument is split into 2, and the 3rd argument is pathname-expanded.
@aguibert: Another handy, though somewhat obscure option is to simply use for device; do ... (sic) - this is equivalent to for device in "$@"; do ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.