1

Here i am trying to execute linux command from a variable in file.sh.

test.sh

OUT= "date";
echo $OUT;

Output:
It is Working perfectly.

But when i try to execute the command pgrep vpnc

OUT= "pgrep vpnc";
echo $OUT;

Output
test.sh: 1: test.sh: pgrep vpnc: not found

my expectation when the above file is executed,it returns pid.

I also did tried by eval.

OUT= "pgrep vpnc";
$ eval $OUT;

Output:
test.sh: 1: test.sh: pgrep vpnc: not found
test.sh: 2: test.sh: $: not found

Can any one help me how to run command and store its value in a variable.

Any help is highly appreciated.

2
  • 1
    Why are you even storing full commands in a variable? Can't you just run the commands? Unless of course you're using user input which just seems like a security issue waiting to happen. Commented Aug 7, 2013 at 15:13
  • The code you posted isn't doing what you think it is. The space between = and the command causes you to simply run the quoted string as a command with the null-valued variable OUTPUT in its environment. Commented Aug 7, 2013 at 15:23

2 Answers 2

2

it shoud be ` instead of "

OUT=`pgrep process`;
echo $OUT;

display the pid of process.

Sign up to request clarification or add additional context in comments.

5 Comments

It worked out, able to get the process id but in console what it has not found test.sh: 1: vpn_test.sh: 20266: not found. Can you explain me
It must be elsewhere in your script, we can't tell you if you not show us the entire script.
But using $(...) instead of backquotes is more readable, still conforming to posix shells, and nestable. I would have suggested OUT=$(pgrep process)
My intention behind this work around is to hide process id (pid) which is generating in my nohup.out file. Yeah,I have written a script to check and debug for every Milli second whether my VPN connection is live or not and that script is running in the background using nohup command. If the connection has lost at any second, am saying it to connect again. So, In nohup.out file I don't want to store pid and i want to keep record of debugging part when connection has lost.
Here is my sample of code if ! pgrep vpnc; then /usr/sbin/vpnc --debug 2 vpn fi And this how i am actually trying to hide pid in my nohup.out file. OUT_PID = $(pgrep vpnc) echo "$OUT_PID" 1>/dev/null if [[ -z "$OUT_PID"]] then /usr/sbin/vpnc --debug 2 vpn fi Correct me if it is wrong and share with me if do you people have better idea than me. Thanks Mali BasileStarynkevitch
1

Just using

$ $OUT

should run the command

1 Comment

Hey Thanks, even that has worked out but when i quoted my command as it suggested by @Mali. Even in this case in my console i can see that some thing has not found. test.sh: 1: test.sh: 20266: not found test.sh: 2: test.sh: $: not found May i know what it has not found exactly. Please

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.