LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-12-2012, 03:36 AM   #1
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 20

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, 03:40 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
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, 04:01 AM   #3
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 20

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, 09:14 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
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, 10:18 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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-12-2012, 11:16 PM   #6
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 20

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


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

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

All times are GMT -5. The time now is 08:05 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
Open Source Consulting | Domain Registration