0

Following works for me:

>sleep 20 &
[1] 30414
>sleep 30 &
[2] 30415
>wait $30414 $30415

This works all right until I want to write this into tmp.csh

In my tem.csh file

sleep 20 &
set pid1=$!
sleep 30 &
set pid2=$!

When it comes to "wait"

wait $pid1 $pid2 => too many arguments
wait $pid1 => too many arguments
wait \$$pid1 => too many arguments
wait $($pid1) => Illegal variable name

How shall I write it?

And this question is for a solution of How can I wait until specified "xterm" finished?

Community
  • 1
  • 1
thundium
  • 995
  • 4
  • 12
  • 30
  • 2
    **Avoid using the C-shell**. Read [C shell considered harmful](http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/). Try `zsh` or `bash` or `fish` shell instead. Read [Advanced Bash Scripting Guide](http://www.tldp.org/LDP/abs/html/) – Basile Starynkevitch Aug 07 '13 at 13:10
  • Notice that `$30414` is expanded to empty! Try your `wait` command prefixed by `echo` to be sure.... – Basile Starynkevitch Aug 07 '13 at 13:19
  • echo $pid1 echo $pid2 gives correct number. I guess it is my grammer error for $($pid1) ? – thundium Aug 07 '13 at 13:27
  • Really switch to a more modern shell. As you can see, nobody is answering because we all have forgotten *C-shell* (I did use it in the 1990s on Sun4/110 workstations). – Basile Starynkevitch Aug 07 '13 at 15:02
  • Not everyone has the luxury of choosing the shell to use. I have to use csh at work (in my scripting as well) because we have some tools that don't play well with bash. I think it's save to assume the OP is using csh because they have to... like Fortran, you wouldn't pick it up for fun. – SethMMorton Aug 08 '13 at 06:16
  • To the OP's original question, it appears that `wait` doesn't accept any arguments in `csh`. As Basile points out, `$30414` expands to an empty string because that variable is not declared. `wait` appears to wait for all jobs in the background to finish. You might be able to just call `wait`, no arguments. – SethMMorton Aug 08 '13 at 06:20

2 Answers2

1

The "wait" command will not wait for specific PIDs. Try the following in CSH to wait for specific PIDs:

#!/bin/csh -f

sleep 30 &
set pid1 = $!
sleep 40 &
set pid2 = $!

while ( `ps -p "$pid1,$pid2" | wc -l` > 1 )
  sleep 1
end
swdev
  • 2,941
  • 2
  • 25
  • 37
1

This works for me in tcsh:

#!/bin/tcsh

time
sleep 10 &
sleep 5 &
wait
time

It looks like that wait doesn't take any argument, just waits until every backgorunded process finish.

abiadak
  • 11
  • 1