1

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.

5
  • I believe the problem is that the shell evaluates det=$(./ossec-rootcheck) before it does the cd because it is evaluating the shell parameters in the expression before it executes it. Why do you need the result in det? Commented Sep 17, 2013 at 12:18
  • The C program returns 0 or 1, if det equals 1 rest of script would do something. Commented Sep 17, 2013 at 12:24
  • 1
    You need to escape the dollar like this: det=\$(./ossec-rootcheck) so that the command runs on the remote machine. Commented Sep 17, 2013 at 12:34
  • @mbratch: So there's no way to get return value of a C program in bash script? I don't want what is sent to stdout. Commented Sep 17, 2013 at 12:36
  • @dogbane: post that as the answer. Commented Sep 17, 2013 at 13:10

2 Answers 2

2
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
Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

To store it in a variable: det=$(ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck; echo $?')
@ konsolebox:What about the stdout output of the program that I want to be stored in a file res?
If you want to copy the output, just do echo "$det" > res after that.
Also, do you mean stdout as both messages for stdout and stderr? Or just the result code?
You can redirect both your stdout and stderr to fd 2, then use fd 1 for the exit code: det=$(ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck >&2; echo $?' 2>/home/ossl7/res)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.