LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-28-2010, 03:08 PM   #1
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 259
Blog Entries: 1

Rep: Reputation: 35
how to store a command in a variable


Hiiiiiiiii,

Well, everybody knows the output of
Code:
ls
Again the command can be stored in a variable and then executed. Like
Code:
var=ls
&var
The above two codes are the same.

The problem occurs when we try to pipeline it. Consider the following problem:
Code:
ls | grep *
works fine...but when we try to store it in a variable and run the command there is an error.
Code:
var="ls|grep *"
$var
So can anybody tell how to store this kind of commands in a variable????
 
Old 01-28-2010, 03:28 PM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
EDIT: The following is NOT what you're looking for:



Code:
var=`ls|grep *`
$var
. . . of the top of my head.

Last edited by mjones490; 01-28-2010 at 03:38 PM.
 
Old 01-28-2010, 03:38 PM   #3
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 259
Blog Entries: 1

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by mjones490 View Post
Code:
var=`ls|grep *`
$var
but then var actually stores the result of
Code:
ls|grep *
I want var to store the command.
 
Old 01-28-2010, 03:58 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
var='ls|grep *'
Single quotes, not backtics...

With backtics you get the complete output from running the command.
 
Old 01-28-2010, 04:00 PM   #5
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Quote:
Originally Posted by pixellany View Post
Code:
var='ls|grep *'
Single quotes, not backtics...

With backtics you get the complete output from running the command.
And then to execute it:
Code:
eval $var
 
Old 01-28-2010, 04:08 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
And to use literal asterisks escape them using a backslash, e.g.
Code:
CMD='grep \* testfile'
eval $CMD
 
Old 01-28-2010, 04:17 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 110Reputation: 110
Gah!

Eval is a common misspelling of "evil". Don't use it if you don't know what you are doing, and even then it's probably a sign that you're Doing Something Wrong.

This topic has been covered by BashFAQ in depth:
http://mywiki.wooledge.org/BashFAQ/050

Also, what is it you're really trying to do? I really can't seriously think that it's an ls+grep, but if it is, you shouldn't try to do anything with the output of ls. Parsing ls is *extremely* error-prone.
 
Old 01-29-2010, 01:20 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by tuxdev View Post
Eval is a common misspelling of "evil". Don't use it if you don't know what you are doing, and even then it's probably a sign that you're Doing Something Wrong.
Honestly that's quite exaggerated. Eval is not that dangerous if you really know how eval and word splitting in variables works, and when to place arguments inside double quotes when already needed. Variable testers like [[ -n ... ]] and [[ -z ... ]] in complicated situations might also be helpful.
 
Old 01-29-2010, 10:47 AM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 110Reputation: 110
Not really. Slip up just a little bit and you've got a vulnerability, and there's lots of hidden gotchas in using eval. Word splitting is only the beginning of how things can go badly. Every time I need to write an eval, I triple-check, and then check it 3 more times while I'm sleeping every night. Almost every time you think about reaching for eval, seriously consider if you need to change your approach.
http://mywiki.wooledge.org/BashFAQ/048
 
Old 01-29-2010, 03:34 PM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by tuxdev View Post
Not really. Slip up just a little bit and you've got a vulnerability, and there's lots of hidden gotchas in using eval. Word splitting is only the beginning of how things can go badly. Every time I need to write an eval, I triple-check, and then check it 3 more times while I'm sleeping every night. Almost every time you think about reaching for eval, seriously consider if you need to change your approach.
http://mywiki.wooledge.org/BashFAQ/048
eval can't be worse than the shell itself: you wouldn't want an outsider in your shell, just as you wouldn't want to eval a line with a variable containing an outsider's text. Other than that, the main vulnerability is the possibility of using special characters where only text was intended, but you wouldn't use eval in that case. Additionally, the same vulnerabilities can be said for a line like this, but people don't rant about the vulnerabilities of ``:
Code:
rm `find . -name '*~'`
Kevin Barry
 
Old 01-29-2010, 05:37 PM   #11
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 110Reputation: 110
The reason for the ranting about eval is because of how much is now "outsider's text". With $(), it's easy to see what you're executing and there's no possibility of any strange results. With eval, depending on how paranoidly it's constructed, you can easily cause the one who used eval to run something they didn't expect by perhaps creating a file with an interesting filename in /tmp.

Using eval safely requires extreme levels of paranoia, and if you're that paranoid, you're going to just avoid it entirely because you can't be paranoid enough to completely guarantee bad things from happening.

Last edited by tuxdev; 01-29-2010 at 05:39 PM.
 
Old 02-01-2010, 02:03 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by tuxdev View Post
Using eval safely requires extreme levels of paranoia, and if you're that paranoid, you're going to just avoid it entirely because you can't be paranoid enough to completely guarantee bad things from happening.
No I don't think so. It's just as simple as re-evaluating a string for a command so no I don't think you need to be paranoid for that. Just common sense.

Edit: Applying a sed command with variables inside double quotes could prove even more complicated (although still simple) or more delicate than using eval.

Last edited by konsolebox; 02-01-2010 at 02:06 AM.
 
  


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
Using variable to store command lines in a pipeline? zizou86 Programming 6 01-29-2010 10:20 AM
Remove a substring and store in variable in shell scripting bharatbsharma Programming 2 09-25-2008 01:28 AM
How to store SQL output in a variable atulsh General 1 09-18-2008 01:22 PM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
Store PATH variable permanently Skute Linux - Software 8 01-15-2004 04:05 PM


All times are GMT -5. The time now is 09:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration