简体   繁体   English

在Bash脚本中运行C程序

[英]Running a C Program in Bash Script

When I run a C program in this bash script it returns the error. 当我在此bash脚本中运行C程序时,它将返回错误。

ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && det=$(./ossec-rootcheck)">/home/sthh/res

Error: 错误:

 ./ossec-rootcheck: No such file or directory

I want to ssh to a remote machine and then run a program on it. 我想ssh到远程计算机,然后在其上运行程序。 I know that this file is located in that path because when I edit it as you see, it works. 我知道此文件位于该路径中,因为当您编辑它时,它就可以工作了。

ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && ./ossec-rootcheck">/home/sthh/res

and as it echo $1 I can see that it does cd /home/sth/remote right. 当它echo $1我可以看到它确实是cd /home/sth/remote right。 But I want the return value of that program to be stored in a variable,for example det . 但是我希望该程序的返回值存储在变量中,例如det

ssh -n -f *.*.*.* "cd /home/sth/remote; echo "$1"; ./ossec-rootcheck || do_your_work">/home/sthh/res

You don't have to store it in a variable. 您不必将其存储在变量中。

|| || executes do_your_work if the exit status of ossec-rootcheck != 0 如果ossec-rootcheck!= 0的退出状态执行do_your_work

If you want to store the numeric exit status in a variable, or echo it, you can do (with proper escaping): 如果要将数字退出状态存储在变量中或回显它,则可以执行此操作(使用适当的转义符):

./ossec-rootcheck; ecode=$?; echo $ecode

To get the return code or exit code of the remote code: 要获取远程代码的返回码或退出码:

ssh -n -f *.*.*.* "cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res

To capture errors as well: 还要捕获错误:

ssh -n -f *.*.*.* "exec 2>&1; cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res

Also, you probably need to omit && echo \\"$1\\" when you find it to be working already. 另外,当发现它已经可以正常工作时,可能需要省略&& echo \\"$1\\" And you could just use single quotes for that: 您可以使用单引号:

ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res

Or 要么

ssh -n -f *.*.*.* 'exec 2>&1; cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM