LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-29-2013, 11:25 PM   #1
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 80

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-29-2013 at 11:28 PM.
 
Old 01-29-2013, 11:54 PM   #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, 05:41 PM   #3
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 80

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, 10: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-13-2013, 11:51 PM   #5
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 80

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


Reply



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 12:26 AM
[SOLVED] storing grep output to feed awk to retrieve entire records matching variable chargaff Programming 8 08-13-2010 06:10 AM
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 07:13 AM
Storing table in variable DeepSeaNautilus Linux - Software 5 04-04-2009 01:45 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 PM.

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
Open Source Consulting | Domain Registration