salvation3103
salvation3103

Reputation: 13

run a bash script with many argument

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 ...

Upvotes: 1

Views: 59

Answers (2)

Andy Guibert
Andy Guibert

Reputation: 42926

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

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

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

for x in router1 router2 router3 ...; do ./backup.sh $x; done;

Upvotes: 2

Related Questions