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 06-12-2012, 04:36 AM   #1
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 18

Rep: Reputation: 1
shell command in variable


Code:
# a="xx : yy : cat /some/file | grep -i string"
# b=`echo $a | cut -d":" -f3` 

# echo "$b"
cat /some/file | grep -i string
I want to execute the command "cat /some/file | grep -i string"

Code:
# $b
cat: invalid option -- i
Try `cat --help' for more information.
#
#
# c=`$b`
cat: invalid option -- i
Try `cat --help' for more information.
how to get a command in a variable?
 
Old 06-12-2012, 04:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,119

Rep: Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269Reputation: 2269
what shell do you use?
You need only: grep -i string filename, you do not need cat at all








_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-12-2012, 05:01 AM   #3
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 18

Original Poster
Rep: Reputation: 1
Using /bin/bash (version: 3.2.25(1); OS is RHEl 5)
The problem is with | (pipe).
If i do
Code:
# grep -i 'someString' /some/file.xml
         <someString>[ipAddress]:port</someString>
#
# grep -i 'someString' /some/file.xml | sed 's/.* <someString>\(.*\)<\/someString>.*/\1/'
[ipAddress]:port
#
#a="grep -i 'someString' /some/file.xml | sed 's/.* <someString>\(.*\)<\/someString>.*/\1/'"
# $a
grep: |: No such file or directory
grep: sed: No such file or directory
grep: 's/.*: No such file or directory
grep: <someString>\(.*\)<\/someString>.*/\1/': No such file or directory
Pls help.
 
Old 06-12-2012, 10:14 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Your problem is that bash performs variable expansion only after the pipelining has been parsed. The best solution is usually to build a shell function with the desired commands and pipelining instead of trying to store that in a variable:
Code:
function doit() {
    grep -i 'someString' /some/file.xml | sed 's/.* <someString>\(.*\)<\/someString>.*/\1/'
}
doit
 
1 members found this post helpful.
Old 06-12-2012, 11:18 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
rknichols has provided the correct answer, but here's the documentation to back it up:


I'm trying to put a command in a variable, but the complex cases always fail!
http://mywiki.wooledge.org/BashFAQ/050


In a nutshell, variables are for storing data, not code. Use functions.

And BTW, $(..) is highly recommended over `..`.
 
Old 06-13-2012, 12:16 AM   #6
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 18

Original Poster
Rep: Reputation: 1
Thnx rknichols and David.
 
  


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
Execute shell command in a variable hankta Linux - Newbie 4 08-25-2011 02:06 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 11:12 AM
Output a C variable with a shell command paliga Programming 4 07-20-2010 07:10 AM
cd command not working through variable in shell script neha_singhal Linux - Newbie 5 02-24-2010 10:04 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 07:12 PM


All times are GMT -5. The time now is 06:32 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