I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you!
cat backup.sh
#!/bin/bash
function usage {
echo "USAGE: $(basename $0) <host1> <host2>"
exit 1
}
function backup_run () {
ssh -q <host> "test -x /tmp/abc"
if [ $? -eq 0 ]; then
echo "ssh -q <host> /tmp/abc"
fi
}
if [[.....]]; then
backup_run <host1>
else
backup_run <host2>
fi
Kindly excuse my first post. Here is my actual script and I looked around to see if I could edit the first post. I'm trying to find how to pass 2 arguments to the script which in turn passes those 2 arguments to the function. I can make it work for one argument using $1 within the function.
#./backup.sh <host1> <host2>
# cat backup.sh
#!/bin/bash
function usage {
echo "USAGE: $(basename $0) <host1> <host2>"
exit 1
}
if [[ $# != 2 ]]; then
usage
fi
function backup_run () {
SITE=$(ssh -q $HOST cat /serverinfo | awk '{print $2}' 2>/dev/null)
echo $SITE
HOST=$(ssh -q $HOST hostname|cut -c1,2,3,4 >/dev/null)
echo $HOST
#Setting SITE based on region
case $SITE in
AAA)
region="AAA"
;;
BBB)
region="BBB"
;;
*)
echo "FAIL: Incorrect region"
exit 1
;;
esac
#OS Version
REL=$(ssh -q $HOST rpm -q redhat-release-server 2>/dev/null)
# Print KEY value based on host, site and os version
if [[ ${HOST} == "PROD" ]]; then
KEY="${SITE} NEW ${REL}"
else
KEY="${SITE} OLD ${REL}"
fi
}
backup_run <host1>
backup_run <host2>
So you succeeded, calling backup_run $1 ? And your next step would be backup_run $2 , then? How about for looping across all positional parameters, using $@ as proposed by stomp?
Thank you for all the responses.
I tested using for loop which worked out well and ended up using backup_run $1 followed by backup_run $2 as I had to call function for each argument at 2 different locations within the script.