LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2013, 12:25 AM   #1
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Rep: Reputation: Disabled
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,

Last edited by project.linux.proj; 01-30-2013 at 12:28 AM.
 
Old 01-30-2013, 12:54 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by project.linux.proj View Post
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
 
1 members found this post helpful.
Old 01-30-2013, 06:41 PM   #3
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Original Poster
Rep: Reputation: Disabled
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).
 
Old 01-30-2013, 11:18 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 02-14-2013, 12:51 AM   #5
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 75

Original Poster
Rep: Reputation: Disabled
thanks Shivaa, I'll go through the link .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 01:26 AM
[SOLVED] storing grep output to feed awk to retrieve entire records matching variable chargaff Programming 8 08-13-2010 07:10 AM
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 08:13 AM
Storing table in variable DeepSeaNautilus Linux - Software 5 04-04-2009 02:45 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 04:57 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration