Grabbing value from command output and monitoring for changes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grabbing value from command output and monitoring for changes
# 8  
Old 08-23-2010
Quote:
Originally Posted by monty77
..only thing that's not perfect now is that it (understandably) will always write a row to the log when you first fire it up. Any way to avoid this?
Cool -- glad you found something that works for you. Yes, change the if a bit to skip if last_value is 'x' or whatever you initialised it to. Something like:

Code:
 if [[ $last_value != "x"  &&   $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
   fi
last_value=$new_value

You will also always have to capture the current value in last_value, so move that line following the fi. There are other ways, but I think this is pretty straight forward.

Cheers!

Last edited by agama; 08-24-2010 at 12:44 AM.. Reason: corrected 2nd typo in if statement
This User Gave Thanks to agama For This Post:
# 9  
Old 08-23-2010
Quote:
Originally Posted by agama
Cool -- glad you found something that works for you. Yes, change the if a bit to skip if last_value is 'x' or whatever you initialised it to. Something like:

Code:
 if [[$last_value != "x" ||  $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
   fi
last_value=$new_value

You will also always have to capture the current value in last_value, so move that line following the fi. There are other ways, but I think this is pretty straight forward.

Cheers!
Can't seem to get that last change to work, I get this:

./adam.sh[8]: [[x: not found
./adam.sh[8]: x: not found

Any ideas?
Thanks!
Monty
# 10  
Old 08-23-2010
Oops, typo.

The [[ needs to have a trailing space.

Code:
if [[ $last_value != "x" ||  $last_value != $new_value ]]

Sorry bout that.
This User Gave Thanks to agama For This Post:
# 11  
Old 08-23-2010
Quote:
Originally Posted by agama
Try something like this:

Code:
last_value=x
log_file=/tmp/log.file
while true
do
   your-command-string | read new_value
   if [[ $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
       last_value=$new_value
   fi
   sleep 300      # every 5 minutes
done

This assumes bash -- most modern UNIXes link /usr/bin/sh to bash -- the [[ expression on the if are much more efficient. If AIX doesn't link to bash, and/or ksh isn't available, you'll have to use if [...].
Quote:
Originally Posted by agama
Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | tail -1| read new_value;

If there is more output following the value you want, but it is always line seven, then try this:
Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk 'NR==7 {print}' | read new_value;

Have a go with one of these and see if that works.
Those suggestions will often fail, since some very popular shells (bash included) will run the read command at the end of the pipeline in a subshell. The value assigned to new_value will never be visible in the script's environment, since it will only ever exist in the subshell's environment (which is destroyed as soon as the read is completed).

Example using bash:
Code:
$ x=; echo hello | read x; echo $x

$ x=; echo hello | ( read x; echo SUBSHELL: $x ); echo CURRENT: $x
SUBSHELL: hello
CURRENT:

Regards,
Alister
# 12  
Old 08-24-2010
Quote:
Originally Posted by alister
Those suggestions will often fail, since some very popular shells (bash included) will run the read command at the end of the pipeline in a subshell. The value assigned to new_value will never be visible in the script's environment, since it will only ever exist in the subshell's environment (which is destroyed as soon as the read is completed).

Example using bash:
Code:
$ x=; echo hello | read x; echo $x

$ x=; echo hello | ( read x; echo $x ) 
hello

Regards,
Alister
Right about bash, however Ksh does the right thing and this will work in ksh.

Code:
$ cat t19
#!/usr/bin/env ksh
x= 
echo hello | read x 
echo $x

$ t19
hello

This User Gave Thanks to agama For This Post:
# 13  
Old 08-24-2010
Quote:
Originally Posted by agama
Oops, typo.

The [[ needs to have a trailing space.

Code:
if [[ $last_value != "x" ||  $last_value != $new_value ]]

Sorry bout that.
Thanks for the correction .. still doesn't work however, it just writes to the log on startup and after the sleep. Confused, looks like it should work to me.

Cheers,
Monty
# 14  
Old 08-24-2010
Bugger! I need to call it a night and go to bed.

Test should be AND rather than OR.

Code:
if [[ $last_value != "x"  &&  $last_value != $new_value ]]

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Shell Programming and Scripting

Grabbing value from file and run command in ``

Hi ALL, How can I make a script take data from a file and execute the commands within `` in the file n put that that in a variable? for i in `cat file` do file=`grep -i key file` cp ${file} test done file /tmp/`date +y`log /tmp/unix`date +y`log (1 Reply)
Discussion started by: 3junior
1 Replies

3. Shell Programming and Scripting

Grabbing IP and zonename from multiline 'ifconfig' output

Hi There, I have a Solaris server that has a bunch of zones configured and I am trying to write a script that will take all interfaces other than the loopback ones (e.g. lo0:3 etc) and present them so that I can easily determine the zone that owns the IP So in the case of the following... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Infrastructure Monitoring

AIX monitoring tools for graphical output

Hi , I am new for Aix i am using IBM AIX server in our org. I am using tomcat and JDK 1.6 for our own ERP software the data base was stored in another server (windows ) i want to monitor my AIX server with graphical output from another system it is possible please help me, any other... (7 Replies)
Discussion started by: krishna_vnr`
7 Replies

6. Solaris

Monitoring the output of 'top' command on hourly basis.

I need to capture the following data on an hourly basis through cronjob scheduling:- 1. load averages 2. Total no. of processes. 3. CPU state 4. Memory 5. Top 3 process details. All the above information is available through the command 'top'. But here we need to automate the same and... (4 Replies)
Discussion started by: subharai
4 Replies

7. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

8. UNIX for Dummies Questions & Answers

File Missing When Grabbing Files from SFTP Server using SCP Command

Hi, I have this problem where sometimes my files would go missing when I schedule my crontab to run the SCP command to get file from the SFTP server. My crontab will run the scripts at an interval of 3 minutes (between the two scripts) The following is the setting in my crontab. ... (1 Reply)
Discussion started by: gingervitus
1 Replies

9. UNIX for Dummies Questions & Answers

Grabbing a value from an output file

I am executing a stored proc and sending the results in a log file. I then want to grab one result from the output parameters (bolded below, 2) so that I can store it in a variable which will then be called in another script. There are more details that get printed in the beginning of the log file,... (3 Replies)
Discussion started by: hern14
3 Replies

10. UNIX for Dummies Questions & Answers

Grabbing result of sql command

Hi guys, Is there a way a script can run an SQL statement and dump the results into a variable which can then be used later in the script? Thanks. (3 Replies)
Discussion started by: hern14
3 Replies
Login or Register to Ask a Question