LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A question on inverting a bash script statement... (https://www.linuxquestions.org/questions/linux-newbie-8/a-question-on-inverting-a-bash-script-statement-819222/)

trist007 07-11-2010 10:24 AM

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?

grail 07-11-2010 10:44 AM

Well I would probably just ask why you don't just use while instead of until?

trist007 07-11-2010 11:04 AM

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.

tredegar 07-11-2010 11:29 AM

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

trist007 07-11-2010 12:23 PM

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?

simon.sweetman 07-11-2010 06:17 PM

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



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