Infinite "while" loop subshell loses current date variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Infinite "while" loop subshell loses current date variable
# 1  
Old 05-19-2015
Infinite "while" loop subshell loses current date variable

I have a simple script to log network connectivity to a set of systems.

However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue?


Code:
#!/bin/bash
LOG=/tmp/netlog
when=`/bin/date`

while true;
do

for server in srv1 srv2 srv3 srv4 srv5;
do

ping -c 1 $server > /dev/null
if [ $? -ne 0 ]; then
 echo "$server-DOWN!" $when >> $LOG.$server
#   mail
else
        echo "$server-UP!" $when >> $LOG.$server
fi
done

done

# 2  
Old 05-19-2015
To execute a command, put it in $( ). You can embed this inside "" quotes like a variable.

i.e.

Code:
echo "$(date +%Y-%m-%d-%H-%M-%S) server down"

I recomend YYYY-MM-DD-HH-MM-SS dates at the beginning of log lines, because they sort and compare so nicely. You'll be able to check if a line is >= "2014-10-10-12-00-00" just with ordinary alphanumeric comparison, etc.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-19-2015
Assign "when" inside the loop.
This User Gave Thanks to Scott For This Post:
# 4  
Old 05-19-2015
Quote:
Originally Posted by Scott
Assign "when" inside the loop.

So simple..Thanks.

---------- Post updated at 04:04 PM ---------- Previous update was at 04:03 PM ----------

Quote:
Originally Posted by Corona688
To execute a command, put it in $( ). You can embed this inside "" quotes like a variable.

i.e.

Code:
echo "$(date +%Y-%m-%d-%H-%M-%S) server down"

I recomend YYYY-MM-DD-HH-MM-SS dates at the beginning of log lines, because they sort and compare so nicely. You'll be able to check if a line is >= "2014-10-10-12-00-00" just with ordinary alphanumeric comparison, etc.
Thanks, I'll give this a try as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable into shell command "date -d": possible...?

Hello, there! I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in: v='2'; date -d "now + $v weeks" But in awk it does not, as in: v="2" "date -d 'now + v weeks'" | getline newdate close ("date -d 'now... (3 Replies)
Discussion started by: fbird3
3 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

4. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

7. Shell Programming and Scripting

How to create a dir with name "current date".

I need to create a dir in my script with its name as current date in the server. how can i do that please help. (2 Replies)
Discussion started by: abhishek27
2 Replies

8. Linux

"YPBINDPROC_DOMAIN: Domain not bound" in infinite loop

After configuring NIS client & server when I restart my system there is a message "YPBINDPROC_DOMAIN: Domain not bound" which falls in infinite loop, although I stopped ypbind service. Please help me in this regard as I have to install all from start. (1 Reply)
Discussion started by: yajneshilu
1 Replies

9. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

10. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies
Login or Register to Ask a Question