Team,
I am a beginner in scripting and was trying to write a script that can change the time zone in Linux from anytime zone to EST.
For this, we are using for loop, if else, and wanted to see the current time on the server,
Given a variable "ctz" inside for loop but we are seeing the part messgae.
current server time is
want to see the message like "current server time is CDT"
I know the script we have written is below par the forum standards, but would like to hear your suggestions for improvement here.
#!/bin/bash
read -p "Please enter Server Name : " sname
server=$(echo $sname)
> /tmp/$server-time-report.out
line=*******************************************************************
for host in `cat /tmp/usv037drservers.txt`
do
echo -n "$host:"
ssh $host " sudo date |cut -c21-23 ;echo "---------Checking timeZone-----------" ; ls -ltr /etc/localtime; ctz=sudo date |cut -c21-23; echo "current server time is $ctz" "
mtz="EST"
echo "required time to set is $mtz"
if [ "$ctz" != "$mtz" ]; then
echo "We need to chnage time zone here"
echo "Updating the Time Zone..."
#ssh $host "sudo unlink /etc/localtime; sudo ln -s /usr/share/zoneinfo/EST /etc/localtime; sudo ls -ltr /etc/localtime "
echo " Reboot is pending on server: " $host
else
echo " No chnages required"
fi
done >> /tmp/$server-time-report.out
output:
./7newtz.sh
Please enter Server Name : dr22
ansible@123:/tmp> cat dr22-time-report.out
123.12.x.x:CDT
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 35 Aug 2 14:59 /etc/localtime -> /usr/share/zoneinfo/America/Chicago
CDT
current server time is
required time to set is EST
We need to chnage time zone here
Updating the Time Zone...
Reboot is pending on server: 123.12.x.x
123.13.x.x:UTC
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 23 Aug 2 19:59 /etc/localtime -> /usr/share/zoneinfo/UTC
UTC
current server time is
required time to set is EST
We need to chnage time zone here
Updating the Time Zone...
Reboot is pending on server: 123.13.x.x
ansible@123:/tmp>
Thanks for your time.
Regards,
SJ