I have a script with two parameters, host and domain name of this host. Look:
#!/bin/bash
data=$(date +%Y.%m.%d)
log=/var/log/upgrade_old_ALLOS.log.$data
function show_help {
echo "Usage: $0 -h host -d dns"
echo "For example: ./upgrade_old_ALLOS.sh -h wwe.com01 -d wwe.com"
}
function logging {
echo $(date) "______" "$1" | tee -a $log
}
while getopts h:d:s opt; do
case $opt in
s)
show_help
exit 0
;;
h)
if [ -z "$OPTARG" ]; then
logging "Error. Name of host is not provided"
show_help
exit 1
else
host="$OPTARG"
fi
;;
d)
if [ -z "$OPTARG" ]; then
logging "Error. Domain is not provided"
show_help
exit 1
else
dns="$OPTARG"
fi
;;
*)
show_help >&2
exit 1
;;
esac
done
if [ -z "$host" ]; then
logging "Host was not provided"
show_help
exit 1
fi
if [ -z "$dns" ]; then
logging "Domain was not provided"
show_help
exit 1
fi
host="$1"
logging "$host"
echo $host > /root/tag
dns="$2"
logging "$dns"
echo $dns >> /root/tag
if [ -f "/root/tag" ] ; then
hostname=$(awk NR==1 "/root/tag")
domain=$(awk NR==2 "/root/tag")
logging "$hostname"
logging "$domain"
else
logging "Not found /root/tag"
exit 1
fi
I should execute this script, like this:
./upgrade_old_ALLOS.sh -h wwe.com01 -d wwe.com
But in a result file, i have this:
-h
wwe.com01
But i should have:
wwe.com01
wwe.com
Where i have error ? Please help.
You overwrite the value of host
and dns
after the getopts ... your script works as intended
./upgrade_old_ALLOS.sh -h wwe.com01 -d wwe.com
$1 : -h
$2 : wwe.com01
$3 : -d
$4 : wwe.com
host="$1" => host = "-h"
dns="$2" => dns = "wwe.com01"
Remove host="$1"
and dns="$2"
and it will do what you want