LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2010, 11:24 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Rep: Reputation: 69
A question on inverting a bash script statement...


Let's say I want to have the System Bell ring when a process is over say a download.

First I created a file named 'beep' that plays the System Bell. Because the System Bell rings by hitting cntrl-G the 'beep' file looks like this.
Code:
echo ^G
I then give the file owner execute permission. I know that this command would serve my purpose.
Code:
wget http://www.domain.com/somefile && /root/beep
However I want to sharpen my Bash programming skills.

I wanted to write the Bash script along the following logic and with the fewest lines possible. Not really a script, I want to insert this short script via command line instead of a file. Let's say the download has commenced and the PID = 16666.
Code:
until ps -p 16666
do
/root/beep
done
Now obviously 'ps -p 16666' will already evaluate to true. My question is, is there a way to maybe enclose 'ps -p 16666' and prepend some operator that inverts the condition to where until 'ps -p 16666' evaluates to false then run /root/beep?
 
Old 07-11-2010, 11:44 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Well I would probably just ask why you don't just use while instead of until?
 
Old 07-11-2010, 12:04 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
How would I construct it exactly?
Code:
while ps -p 16666
do
/root/beep
done
That would just evaluate the while statement as true and run the 'beep' file non-stop.

I also tried
Code:
while ps -p 16666
do
sleep 1
done
/root/beep
Thinking that once 'ps -p 16666' evaluated to false the loop would end and then run /root/beep, but that didn't work.

Last edited by trist007; 07-11-2010 at 12:05 PM.
 
Old 07-11-2010, 12:29 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,087

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
Quote:
..but that didn't work.
if something then something else something is worth taking a look at.

I think you need some bash tutorials.

Here are some links:
http://www.linuxtutorialblog.com/pos...-if-statements
http://tldp.org/LDP/abs/html/
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://freshmeat.net/projects/advanc...scriptingguide
 
Old 07-11-2010, 01:23 PM   #5
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
I tried this script and it didn't work
Code:
#!/bin/bash

while true
do
if ps -p 14189
then
sleep 2
else
/root/beep
fi
done
Maybe I didn't hear the bell because as a script it was running in a different tty?
 
Old 07-11-2010, 07:17 PM   #6
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
You can use /proc/<pid> instead of ps -p to determine if a process is still running. Here is a little demo script notifyjob (just for fun) that waits for a pid to finish and writes to the specified user's terminal:

Code:
#/bin/bash
if [ $# -lt 1 -o $# -gt 2 ]
then
   echo "usage notifyjob PID [notify-user]"
   exit 1
fi
NOTIFY=$2
# No user specified - use current user id
if [ -z "$NOTIFY" ]      
then
    NOTIFY=$(id -un)
fi

while [ -d /proc/$1 ]
do
   sleep 5
done
echo "Your job ($1) has now finished" | write $NOTIFY
exit 0
You should also have a look at the bash internal wait command (will wait for child process(es)/job(s) of the current shell):

Code:
    /usr/local/bin/myjob &
    wait
    echo "myjob is now finished" | write trist
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch statement for bash script wjs1990 Linux - Newbie 7 11-20-2009 12:37 AM
Using apt-get in if statement in bash script jax8 Programming 4 03-29-2009 11:23 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
problem with if statement in bash script crewblunts Programming 3 03-11-2006 01:38 PM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration