LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   error while storing output in variable (https://www.linuxquestions.org/questions/linux-newbie-8/error-while-storing-output-in-variable-4175447789/)

project.linux.proj 01-29-2013 11:25 PM

error while storing output in variable
 
Hi,

I am facing error while I try to store output in the variable in bash script.

If i run the script without storing it to variable it runs fine. But if i try to store output in variable then it gives errors.

[root@localhost ~]# for i in localhost ; do echo $i ; ssh $i "/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":" | xargs " ; done
localhost
root@localhost's password:
CentOS


[root@localhost ~]# for i in localhost ; do echo $i ; ssh $i " ver=`/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":" | xargs` ; echo $ver " ; done
localhost
root@localhost's password:
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
bash: line 1: Distributor: command not found
bash: -c: line 2: syntax error near unexpected token `('
bash: -c: line 2: `Description: CentOS release 5.5 (Final)'



Thanks,

shivaa 01-29-2013 11:54 PM

Quote:

Originally Posted by project.linux.proj (Post 4880413)
Hi,
[root@localhost ~]# for i in localhost ; do echo $i ; ssh $i " ver=`/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":" | xargs` ; echo $ver " ; done
localhost
root@localhost's password:
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
bash: line 1: Distributor: command not found
bash: -c: line 2: syntax error near unexpected token `('
bash: -c: line 2: `Description: CentOS release 5.5 (Final)'

Consider 2nd command, it means you do ssh on $i i.e. on some server and invoke following two commands on it:

Code:

ver=`/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":"  | xargs`
echo $ver

So problem is with first line, because it's not storing command value in ver variable. Infact, it all depends upon what shell is there on remote server i.e. on $i.
So once invoke:
Code:

~$ for i in localhost ; do echo $i ; ssh $i "echo $SHELL" ; done
And then try:
Code:

~$ for i in localhost ; do echo $i ; ssh $i " ver=$(/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":"  | xargs) ; echo $ver " ; done

project.linux.proj 01-30-2013 05:41 PM

Thanks Shivaa, it worked . Can you please give me the logic/explaination of the command ver=$(/usr/bin/lsb_release -a | /bin/grep Distributor | cut -f2 -d ":" | xargs).

shivaa 01-30-2013 10:18 PM

This is bash style variable declaration. It's recommended to use $(...) over backticks (see here).
Go through the link mentioned.
It was a problem because your remote machine was not storing output of command in a variable (or it was not accepting backticks convention).

Anyway, if it's solved, then mark the thread as solved. :)

project.linux.proj 02-13-2013 11:51 PM

thanks Shivaa, I'll go through the link .


All times are GMT -5. The time now is 02:45 PM.