unable to get the details of the variables in bash script

23 views
Skip to first unread message

sharan kumar

unread,
Aug 2, 2022, 6:36:24 PM8/2/22
to rundeck-discuss
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

sharan kumar

unread,
Aug 3, 2022, 12:50:28 PM8/3/22
to rundeck...@googlegroups.com
Team,

Could some one please post your suggestions on my query when you get a chance.

Regards,
SJ

--
You received this message because you are subscribed to a topic in the Google Groups "rundeck-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rundeck-discuss/vUHSh-1EDQo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rundeck-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rundeck-discuss/cbf57c55-92b1-488c-a630-3b30e9629014n%40googlegroups.com.

rac...@rundeck.com

unread,
Aug 3, 2022, 1:57:31 PM8/3/22
to rundeck-discuss

Hi Kumar,

As a tip, save the current time in this way:

mydate=$(date | cut -c21-23)
echo $mydate

It works on my env in that way.

Hope it helps :-)

sharan kumar

unread,
Aug 3, 2022, 2:53:18 PM8/3/22
to rundeck...@googlegroups.com
Thanks for your time  racuna,  But still seeing the same issue.


server list:

#cat /tmp/usv037drservers.txt
1.2.3.4
5.6.7.8

I am using for loop to run it recursively.


cat 9newtz.sh

#!/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 "+%Z";echo "---------Checking timeZone-----------";ls -ltr /etc/localtime; ctz=$(date | cut -c21-23); echo $ctz" ##
echo $ctz ##

Here i am  unable to see the ouput of echo $ctz in report file 5-time-report.out, any idea how can this be achieved?

out put is in this format:

 cat 5-time-report.out
1.2.3.4:EST
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 23 Aug  3 13:37 /etc/localtime -> /usr/share/zoneinfo/EST


5.6.7.8:EST
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 23 Aug  3 13:37 /etc/localtime -> /usr/share/zoneinfo/EST

sharan kumar

unread,
Aug 4, 2022, 1:14:59 PM8/4/22
to rundeck...@googlegroups.com
Thanks  Racuna, for your suggestions.

Think the for loop was not working and taking the variable as expected, since it was taking the values of the master server where I am running the task.

But since the time zone change should be updated on remote servers we used scp to copy the script to the remote server and get it executed.

Script we modified and is working as expected.

Adding the script details for reference and knowledge sharing as I am a beginner in scripting. Kindly provide your suggestions for improvement.

#cat 11newtz.sh
#!/bin/bash
#place public IPs of project/customers servers in text file  ex:/tmp/drservers.txt
#copying the /home/ansible/tzchange.sh to servers in text  file

read -p "Please enter Server Name : " sname
server=$(echo $sname)
> /tmp/$server-time-report.out
line=*******************************************************************
for host in `cat /tmp/drservers.txt`
do
scp -r /home/ansible/tzchange.sh $host:/home/ansible/
echo -n "$host:"
ssh $host  /home/ansible/tzchange.sh
done  >> /tmp/$server-time-report.out

#cat tzchange.sh
sudo date  |cut -c21-23
echo "---------Checking timeZone-----------"
ls -ltr /etc/localtime
ctz=$(sudo date  |cut -c21-23)
echo "Server time is:" $ctz
mtz="EST"
#echo $ctz

if [ "$ctz" != "$mtz" ]; then
 echo "Performing  required actions..."
 echo "Updating Time Zone to: $mtz"
 sudo unlink /etc/localtime; sudo ln -s /usr/share/zoneinfo/EST /etc/localtime; echo "######Updated TimeZone details#####";sudo ls -ltr /etc/localtime
 echo "Reboot is pending on the server:" `hostname`
 echo -e "############################################################################################\n"
elif [ "$ctz" == "$mtz" ]; then
 echo "$host TZ already set to: $mtz"
 sudo ls -ltr /etc/localtime
 echo -e "############################################################################################\n"
else
 echo "NO Action Required OR Manaully Check"
fi

#cat /tmp/drservers.txt
1.2.3.4
5.6.7.8

Output:

ansible123@:~> ./11newtz.sh
Please enter Server Name : DR37-august-4

ansible123@:/tmp>#cat DR37-august-4-time-report.out
1.2.3.4:CDT
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 35 Aug  3 14:12 /etc/localtime -> /usr/share/zoneinfo/America/Chicago
Server time is: CDT
Performing  required actions...
Updating Time Zone to: EST
######Updated TimeZone details#####
lrwxrwxrwx 1 root root 23 Aug  3 16:32 /etc/localtime -> /usr/share/zoneinfo/EST
Reboot is pending on the server: drunos
############################################################################################

5.6.7.8:UTC
---------Checking timeZone-----------
lrwxrwxrwx 1 root root 23 Aug  3 21:15 /etc/localtime -> /usr/share/zoneinfo/UTC
Server time is: UTC
Performing  required actions...
Updating Time Zone to: EST
######Updated TimeZone details#####
lrwxrwxrwx 1 root root 23 Aug  3 16:32 /etc/localtime -> /usr/share/zoneinfo/EST
Reboot is pending on the server: drduos
############################################################################################


Regards,
Sharan SJ





Reply all
Reply to author
Forward
0 new messages