Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux:在另一个脚本完成后启动脚本_Linux_Shell_Csh - Fatal编程技术网

Linux:在另一个脚本完成后启动脚本

Linux:在另一个脚本完成后启动脚本,linux,shell,csh,Linux,Shell,Csh,我从一本书中读到了这个问题的答案 在Stackoverflow.com上。但我在写shell脚本方面太生疏了,所以我做错了什么。以下是我的脚本: 测试脚本: #!/bin/csh -f pid=$(ps -opid= -C csh testscript1) while [ -d /proc/$pid ] ; do sleep 1 done && csh testscript2 exit $ csh testscript Illegal variable name.

我从一本书中读到了这个问题的答案 在Stackoverflow.com上。但我在写shell脚本方面太生疏了,所以我做错了什么。以下是我的脚本:

测试脚本:

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

exit
$ csh testscript
Illegal variable name.
testscript1:

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

#!/bin/csh -f
echo Done
exit
目的是让testscript先调用testscript1;一旦testscript1已经完成(这意味着在script1中调用的firefox已关闭),testscript将调用testscript2。但是,在运行testscript之后,我得到了这个结果:

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

exit
$ csh testscript
Illegal variable name.

请帮我解决这个问题。谢谢。我相信这句话不是CSH:

pid=$(ps -opid= -C csh testscript1)
通常,在csh中,您定义如下变量:

set pid=...
我不确定$()语法是什么,也许回勾可以替代:

set pid=`ps -opid= -C csh testscript1`

也许您没有注意到您找到的脚本是为bash编写的,不是为csh编写的,而是为bash编写的 您正试图用csh解释器处理它们

看起来您误解了原始代码的意图——它是 旨在通过使用进程名称查找其进程id来监视已存在的进程

您似乎试图从
ps
命令内部启动第一个进程。但是 在这种情况下,你没有必要做这么复杂的事情——你所需要的一切 是:

除非你特意在后台运行其中一个脚本, 在第一个脚本完成之前,第二个脚本不会运行

虽然这与您的问题无关,但csh更倾向于 互动使用;用于脚本编写,所以您可能 最好还是学bash吧。

试试

下面的脚本将检查testscript1的pid,如果找不到它,则将执行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

我尝试了你的建议,收到了以下消息:
错误:找不到TTY。
和一个使用psI的选项列表实际上尝试了bash文件,它说
ps:unlawk argument:./testscript1.sh
,并执行testscript2。我已经更新了答案。原来的剧本做了一些你似乎需要的完全不同的事情。谢谢你的帮助。您推荐给我的脚本在Linux中运行良好。我以前在MacOSX上试过这个,结果是“完成”在Firefox打开后立即打印出来。我没有在Linux中再次尝试。无论如何,在Linux中工作是我现在需要的。但是你能解释一下为什么它在MacOSX中不能这样工作吗?谢谢