質問

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.

役に立ちましたか?

解決 2

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.

他のヒント

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`

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top