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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-12-2012, 03:36 AM
|
#1
|
LQ Newbie
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 22
Rep:
|
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?
|
|
|
06-12-2012, 03:40 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,515
|
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")
|
|
|
06-12-2012, 04:01 AM
|
#3
|
LQ Newbie
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 22
Original Poster
Rep:
|
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.
|
|
|
06-12-2012, 09:14 AM
|
#4
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,827
|
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.
|
06-12-2012, 10:18 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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 `..`.
|
|
|
06-12-2012, 11:16 PM
|
#6
|
LQ Newbie
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 22
Original Poster
Rep:
|
Thnx rknichols and David.
|
|
|
All times are GMT -5. The time now is 11:54 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|