1

I read the answer for this issue from this link in Stackoverflow.com. But I am so new in writing shell script that I did something wrong. The following are my scripts:

testscript:

#!/bin/csh -f
pid=$(ps -opid= -C csh testscript1)
while [ -d /proc/$pid ] ; do
    sleep 1
done && csh testscript2

exit

testscript1:

#!/bin/csh -f
/usr/bin/firefox
exit

testscript2:

#!/bin/csh -f
echo Done
exit

The purpose is for testscript to call testscript1 first; once testscript1 already finish (which means the firefox called in script1 is closed) testscript will call testscript2. However I got this result after running testscript:

$ csh testscript
Illegal variable name.

Please help me with this issue. Thanks ahead.

Community
  • 1
  • 1
HuongOrchid
  • 332
  • 3
  • 15

3 Answers3

2

I believe this line is not CSH:

pid=$(ps -opid= -C csh testscript1)

In general in csh you define variables like this:

set pid=...

I am not sure what the $() syntax is, perhaps back ticks woudl work as a replacement:

set pid=`ps -opid= -C csh testscript1`
woolstar
  • 5,063
  • 20
  • 31
1

Perhaps you didn't notice that the scripts you found were written for bash, not csh, but you're trying to process them with the csh interpreter.

It looks like you've misunderstood what the original code was trying to do -- it was intended to monitor an already-existing process, by looking up its process id using the process name.

You seem to be trying to start the first process from inside the ps command. But in that case, there's no need for you to do anything so complicated -- all you need is:

#!/bin/csh
csh testscript1
csh testscript2

Unless you go out of your way to run one of the scripts in the background, the second script will not run until the first script is finished.

Although this has nothing to do with your problem, csh is more oriented toward interactive use; for script writing, it's considered a poor choice, so you might be better off learning bash instead.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • I actually did try bash file and it said `ps: illegal argument: ./testscript1.sh`, and execute testscript2.sh – HuongOrchid Dec 10 '13 at 23:57
  • I've updated my answer. The original script was doing something rather different that what you seem to need. – Jim Lewis Dec 11 '13 at 00:54
  • Thanks for helping me. The script that you recommend me works well in Linux. I tried this before in Mac OS X and got the result that 'Done' was printed right after the Firefox was open. And I didn't try it again in Linux. Anyway working in Linux is what I need now. But could you please explain to me why it didn't work that way in Mac OS X? Thanks!!! – HuongOrchid Dec 11 '13 at 15:01
0

Try,

below script will check testscript1's pid, if it is not found then it will execute testscirpt2

 sp=$(ps -ef | grep testscript1 | grep -v grep | awk '{print $2}')
 /bin/ls -l /proc/ | grep $sp > /dev/null 2>&1 && sleep 0 || /bin/csh testscript2
Ranjithkumar T
  • 1,886
  • 16
  • 21