[expert] Bash scripts: Displaying output of command and testing in same line?
I'm a newbie trying to create a bash script. (I'm not sure whether this is a newbie level question or not.) I'd like to run a command (almost anything) in an if statement and direct it's normal output to standard out but also test that output using grep to use the result in the logic of the script. For example, the output of mailq is Mail queue is empty if the mail queue is empty, and a list of messages if there are any in the queue. In butchered pseudocode, I'd like to do something like this: * run mailq displaying output * if mailq was empty (tested like: mailq | grep -c empty) do something else do something else fi I could do something like run mailq and capture the output in a file or variable, then: * print the file or variable * test the file or variable Or, I could run the command twice, but I want to avoid that. I thought there might be an easier way (or a one liner type approach, which while it might be one line might be fairly difficult (for me) to understand ;-) I've started trying variations of the following, but I'm really just shooting in the dark: if mailq tee grep -c empty Randy Kramer Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] Bash scripts: Displaying output of command and testingin same line?
On Thu, 22 Aug 2002, Randy Kramer wrote: I'm a newbie trying to create a bash script. (I'm not sure whether this is a newbie level question or not.) I'd like to run a command (almost anything) in an if statement and direct it's normal output to standard out but also test that output using grep to use the result in the logic of the script. There are several approaches you could take, all depending on the length of output and what else you'd need to do with it. One approach that you've found is to redirect the output to a temporary file: somecommand tmpfile.$$ The $$ expands to the process ID of the command and is useful for quickly generating a filename. You can then parse the file with sed, awk, or whatever you need. For example, the output of mailq is Mail queue is empty if the mail queue is empty, and a list of messages if there are any in the queue. In butchered pseudocode, I'd like to do something like this: * run mailq displaying output * if mailq was empty (tested like: mailq | grep -c empty) do something else do something else fi I could do something like run mailq and capture the output in a file or variable, then: * print the file or variable * test the file or variable Or, I could run the command twice, but I want to avoid that. Yes, definitely avoid this. The output may change between runs. I thought there might be an easier way (or a one liner type approach, which while it might be one line might be fairly difficult (for me) to understand ;-) Look through the bash conditional operators. There's a ?: operator that may do what you need. I've started trying variations of the following, but I'm really just shooting in the dark: if mailq tee grep -c empty You're missing the pipe '|' between commands. Randy Kramer Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts - Thankyou
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 After suggestion from one of you guys (forgot the name and have deleted the mail - sorry) i have started to use the dynamic dns of no-ip.com, this is a really great system. Thanks Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8fjNfXCpWOla2mCcRApl7AJ4xMj+zV4GM+SG8iVEx/+QJ0zraMgCeKVlC qQThfAnsQF16L9vM2pZyjvg= =IVIM -END PGP SIGNATURE- Want to buy your Pack or Services from MandrakeSoft? Go to http://wwwmandrakestorecom
RE: [expert] BASH Scripts
yeah, I use ddclient with dyndns.org same deal, and it emails root, (or whomever roots alias is) with the details of the ip address whenever the machine reconnects, I run my mailserver at home like that. works great, and I always have an email with the new IP in it. rgds Frank -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of David Guntner Sent: Thursday, 28 February 2002 7:18 AM To: [EMAIL PROTECTED] Subject: Re: [expert] BASH Scripts Tom Badran grabbed a keyboard and wrote: How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. if [ -s diff ]; then {do your command or commands here} fi What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. Have you checked out the free DDNS service at no-ip.com? They have a Linux client that you can run when you connect, and then you can just ssh into yourmachine.no-ip.com instead of worrying about the current IP address. --Dave -- David Guntner GEnie: Just say NO! http://www.akaMail.com/pgpkey/davidg or key server for PGP Public key Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts - Thankyou
Tom Badran grabbed a keyboard and wrote: After suggestion from one of you guys (forgot the name and have deleted the mail - sorry) i have started to use the dynamic dns of no-ipcom, this is a really great system Thanks That was me And you're welcome :-) --Dave -- David Guntner GEnie: Just say NO! http://wwwakaMailcom/pgpkey/davidg or key server for PGP Public key Want to buy your Pack or Services from MandrakeSoft? Go to http://wwwmandrakestorecom
Re: [expert] BASH Scripts - Thankyou
I do not have an adsl connection, but a ppp dialup, but some of the solutions I use here may be applicable: The system I use is run from etc/ppp/ip-uplocal, a file that is run automatically when a ppp connection comes up - there are also network scripts that run when other interfaces come online: 1) The file contains $IPLOCAL and $IPREMOTE variables already set via the network scripts (by parsing ifconfig results I think) 2) uses the info to change the IP at dyndns and ods via ez-ipupdate 3) creates a simple web page containing date/time and IP numbers and ftp's it up to my webspace on the ISP's server 4) kicks ntpd which since 81 occaisionally hangs and refuses to recognise a new connection 5) restarts the firewall with the latest IP 6) same with named - not strictly neccessary, but solved some undefined, not quite working right type problems if you know what I mean!! 7) sends a smb message (winpopup) to the windoze machines attached telling them the connection is back up A similar script in ip-downlocal tells them when the ISP logs the connection off (kicker) 8) Recently I added some stuff to separate the ISP ppp connection and my laptop using another ppp connection through a serial port on the same machine From experience, I have found the redundancy of two dynamic dns systems and a separate web page is well worth while! BillK On Fri, 2002-03-01 at 00:53, Praedor Tempus wrote: Just some noiseI am late getting to this and in the past I had this desire too (to be informed every time my ip address changed) All I did was have a cronjob send me a simple email every 30 minutes (or so, depending on what I determined my renewal period was) The email didn't even need to contain any information because what I was interested in were the headers I'd get the email and check the headers see my ip address and could therefore see if my address had changed Want to buy your Pack or Services from MandrakeSoft? Go to http://wwwmandrakestorecom
Re: [expert] BASH Scripts - Thankyou
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Thursday 28 Feb 2002 5:43 pm, you wrote: Tom Badran grabbed a keyboard and wrote: After suggestion from one of you guys (forgot the name and have deleted the mail - sorry) i have started to use the dynamic dns of no-ip.com, this is a really great system. Thanks That was me. And you're welcome. :-) Then ill thank you directly, Cheers! Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8fnYYXCpWOla2mCcRAktOAKC0tBvD7QRD//HGXwhM63ioxzODSgCgt1zO 57Cy1X6iot7zjrYM2qZVjjA= =UtrS -END PGP SIGNATURE- Want to buy your Pack or Services from MandrakeSoft? Go to http://wwwmandrakestorecom
[expert] BASH Scripts
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. Thanks Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8fWUSXCpWOla2mCcRAmyuAJ0dNeH725n8jc0UhaeIYgdxlerG3wCfQwtM mEDR987gtUilwovjGKbV3b8= =IODe -END PGP SIGNATURE- Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
Tom Badran grabbed a keyboard and wrote: How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. if [ -s diff ]; then {do your command or commands here} fi What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. Have you checked out the free DDNS service at no-ip.com? They have a Linux client that you can run when you connect, and then you can just ssh into yourmachine.no-ip.com instead of worrying about the current IP address. --Dave -- David Guntner GEnie: Just say NO! http://www.akaMail.com/pgpkey/davidg or key server for PGP Public key Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
Tom Badran [EMAIL PROTECTED] wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. One way to do it is this sort of hack: [rustyc@fw rustyc]$ cat bin/watchmail #!/bin/bash foo=`ls -l /var/spool/mail/rustyc` while : ; do for j in 1 2 3 4 5 6 7 8 ; do for i in 1 2 3 4 5 6 7 8 9 0 ; do if [ `ls -l /var/spool/mail/rustyc` = $foo ] ; then echo -n '.' sleep 30 else echo echo `date` #echo Was $foo echo `ls -l /var/spool/mail/rustyc` foo=`ls -l /var/spool/mail/rustyc` egrep '^Subject' /var/spool/mail/rustyc | tail -2 sleep 30 fi done #date #; echo -n '.'~/rnlog done echo #echo -n `date ; ls -l /var/spool/mail/rustyc` done (that script watches for a change to file /var/spool/mail/rustyc and shows the change (and a few lines from the file) when it does - sort of a poor man's textual biff ;-) The key lines are: foo=`ls -l /var/spool/mail/rustyc` while : ; do if [ `ls -l /var/spool/mail/rustyc` = $foo ] ; then # no change else # file has changed fi done On the other hand, diff will return nonzero return status if the files are different, so you can just say something like: diff foo1 foo2 if [ $? -eq 0] ; then # no change else # change fi (Assuming I'm remembering the syntax for 'return status from last command' right, if not I'm sure someone will fix this for us ;-) rc Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
On Wed, 27 Feb 2002, Tom Badran wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. The only problem I see with using the raw output from ifconfig is that the RX and TX fields will always be different. If you were to parse the output of ifconfig to return only the IP address before writing it to the file, you could then just compare the current IP against a 'cat' of the old IP. I.e. IP=`ifconfig eth0|grep inet addr|cut -d: -f2|cut -d' ' -f1` (or something equally ugly) Then do something like: Old_IP=`cat $FILENAME` [ $IP == $OLD_IP ]; then do_something Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
Tom Badran wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. Well first, you can test the exit value of diff to see if there were differences: if diff /var/cache/IP/1 /var/cache/IP/2; then #commands for when files are the same else #commands for when files differ fi Or if you only care when they differ: if ! diff /var/cache/IP/1 /var/cache/IP/2; then #commands for when files differ fi If you really need to test for empty files, try: if [ -s filename ]; then #commands for when file is *not* empty else #commands for when file is empty fi Or if you only care for the empty ones: if [ ! -s filename ]; then #commands for when the file is empty fi -Kyle -- _ ---ooO( )Ooo--- Kyle J. McDonald (o o) Systems Support Engineer Sun Microsystems Inc.| Enterprise Server Products[EMAIL PROTECTED] 1 Network Drive BUR03-4630 \\\// voice: (781) 442-2184 Burlington, MA 01803 (o o)fax: (781) 442-1542 ---ooO(_)Ooo--- Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
Hi Tom, I wrote a shell script for the same purpose. Here it is: #!/bin/sh NewIP=`/sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print $2}' \ | sed -e 's/.*://'` if [ -e /home/rjonasz/ip.txt ]; then OldIP=$(cat /home/rjonasz/ip.txt) else OldIP= fi if [ $OldIP != $NewIP ]; then echo $NewIP | mail -s Foucault's IP Address has changed! \ [EMAIL PROTECTED] echo $NewIP /home/rjonasz/ip.txt fi Hope this helps, Randy On Wed, 27 Feb 2002, Tom Badran wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How can i use an if in a bash script so that it will only run commands if the specified file is empty? Basically, ive set up a cache system by which the output of ifconfig is stored in /var/cache/IP/1 Then, every minute my script is run. It first puts the output of ifconfig in /var/cache/IP/2 and diff's it with 1 outputing that to a file 'diff'. I then want to run a series of commands if the file diff is not empty. What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. Thanks Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8fWUSXCpWOla2mCcRAmyuAJ0dNeH725n8jc0UhaeIYgdxlerG3wCfQwtM mEDR987gtUilwovjGKbV3b8= =IODe -END PGP SIGNATURE- -- Under any conditions, anywhere, whatever you are doing, there is some ordinance under which you can be booked. -- Robert D. Sprecht, Rand Corp. Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re[2]: [expert] BASH Scripts
On Wed, 27 Feb 2002, Rusty Carruth wrote: There's probably much better ways to do this, but I got it working this way and have no plan to fix it ;-) Hey now, the very fabric of the Internet is stitched together with assorted hacks, workarounds, ugly scripts, we'll fix it later code, and suchlike bailing wire and duct tape! If people only knew... :D Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re[2]: [expert] BASH Scripts
[EMAIL PROTECTED] wrote: On Wed, 27 Feb 2002, Tom Badran wrote: What im trying to do is get an email sent to me every time the IP changes on a specific machine, so i always have a record of it for logging in via ssh. The specific connection is ppp0 which auto redials on disconnect. If there is an easier way of doing this please share with me. I.e. IP=`ifconfig eth0|grep inet addr|cut -d: -f2|cut -d' ' -f1` (or something equally ugly) Then do something like: Old_IP=`cat $FILENAME` [ $IP == $OLD_IP ]; then do_something Oh. Duh. If I'd read the email closer, I'd have posted THIS script instead: [root@fw antispam]# cat /etc/rc.d/init.d/what.a.hack #!/bin/bash newip=`/sbin/ifconfig ppp0 | /bin/egrep 'inet addr' | /bin/sed 's/inet addr://' | /bin/awk '{print $1}'` if [ .$newip = . ] ; then echo oops - newip is blank again # this happens more than I would like, one of these days I will figure out why. exit -1 fi oldip=`cat /tmp/current.ip` if [ $newip == $oldip ] ; then exit 0 ; fi echo $newip /etc/rc.d/init.d/iplog /bin/sed s/FARBOO/$newip/ /etc/rc.d/init.d/rc.fw.basehack /root/rc.hacko echo $newip /tmp/current.ip chmod 700 /root/rc.hacko /sbin/ipchains -L -n -v -x /root/rc.hacko echo $newip | elm [EMAIL PROTECTED] And I run that in a cron job. Here's my crontab entry: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /etc/rc.d/init.d/what.a.hack Yeah, its quite a hack. But it does the trick of resetting my firewall rules whenever the ip addr changes, and it notifies me, and everything. There's probably much better ways to do this, but I got it working this way and have no plan to fix it ;-) rc Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] BASH Scripts
Rusty Carruth grabbed a keyboard and wrote: And I run that in a cron job. Here's my crontab entry: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /etc/rc.d/init.d/what.a.hack Were you aware that you could just do: */5 * * * * /etc/rc.d/init.d/what.a.hack Which tells cron to run the job every 5 minutes? Just FYI. It'll save you some typing the next time you have to create a new cron job. :-) --Dave -- David Guntner GEnie: Just say NO! http://www.akaMail.com/pgpkey/davidg or key server for PGP Public key Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com