简体   繁体   中英

Running a C Program in Bash Script

When I run a C program in this bash script it returns the error.

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. 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. But I want the return value of that program to be stored in a variable,for example 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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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