User:Melab-1/Archives/Reference Desk/Computing/November 11 2008/Number 10

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Troublesome issue with a shell script[edit]

Greetings! In the following shell script, assume $2 is the name of a text file with each line within it containing the names of processes, and $1 is the name of a user on the local machine to email if the process is running.

tmp3=$(less $2)
echo $tmp3
for check in $tmp3 do
        result=$(ps -C $check | wc -l | LANG=C sed 's/\([0-9]*\).*/\1/')
        if [ $result -gt 1 ]; then
                echo "Process is running!" > /dev/tty
                message=echo $check is currently running'!'
                echo $message | mail -s $message $1

Now, the issue I am having is with result=$(ps -C $check | wc -l | LANG=C sed 's/\([0-9]*\).*/\1/'). I am certain the command itself (that is, without a variable in the mix) works as intended, which returns a single number, then enters the if statement if there is more than one line detected in the process list.

However, upon trying to run it, the shell is stating there is a syntax error somewhere, and I just cannot see it. The complete error is below

[kes@fluffy stuff]$ ./shellScript 2 test.txt
ps ls init gnome-terminal syslogd
./shellScript: line 16: syntax error near unexpected token `result=$(ps -C $check | wc -l | LANG=C sed 's/\([0-9]*\).*/\1/')'
./shellScript: line 16: `       result=$(ps -C $check | wc -l | LANG=C sed 's/\([0-9]*\).*/\1/')'

Any suggestions on how to overcome this confusing issue would be greatly appreciated. Thanks!

Sincerely, Kesiana. (talk) 18:02, 11 November 2008 (UTC)

The "for loop" syntax goes "for i in a b c; do body; done". Those semicolons can be replaced by newlines, but you don't have either. Also, you should use cat rather than less. --Sean 18:12, 11 November 2008 (UTC)