LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-04-2008, 11:16 AM   #1
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Rep: Reputation: 15
Problem executing a command with pipes


Hello, I am making a script which looks for a field in a text file and assigns it to a variable, for example:
1:23:34:45:45:banner "hola" | write userx pts/x
As you can see, the sixth field corresponds to the command I want to execute. I use the following command for the assingment:

command=`awk -F: '{print $6} file'`

As a result the variable command is correctly assigned the value:
banner "hola" | write userx pts/x
I try to execute it on the script with the following instruction:
$command
But the shell only executes the banner command, wich takes as an argument the rest of the value of command variable, printing a banner with the string " hola | write userx pts/x ", instead of passing the ouput of the banner "hola" command to the command write userx pts/x, to send it to userx. I have tried using "$command", but it doesn´t work either. If the command doesn´t have pipes, it works ok.
I am using Red Hat Linux.
Thanks for the help.

Last edited by DeepSeaNautilus; 08-04-2008 at 07:16 PM.
 
Old 08-04-2008, 11:22 AM   #2
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Shell meta characters are not interpreted inside quotes when executed directly. Try:

Code:
eval "$command"
 
Old 08-04-2008, 07:17 PM   #3
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks

Quote:
Originally Posted by smoked kipper View Post
Shell meta characters are not interpreted inside quotes when executed directly. Try:

Code:
eval "$command"
Thanks for your help. It worked fine.
 
Old 08-04-2008, 07:51 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by smoked kipper View Post
Shell meta characters are not interpreted inside quotes when executed directly.
This isn't quite correct. Redirection and pipes are done before parameter expansion, but file name expansion is still performed:

Code:
$ touch a b c d
$ ls ?
a  b  c  d
$ set -x
line 21: set -x
$ files='?'
line 22: files='?'
$ $files
line 23: a b c d
bash: a: command not found
As you can see, the shell did not perform pathname expansion inside the single quotes, but it did perform parameter expansion, and then file name expansion (wildcarding) and then command execution.

The important point here is knowing the order of command line expansion.
 
Old 08-05-2008, 11:55 AM   #5
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Zsh doesn't do that:

Code:
# touch a b c d
# ls ?
a b c d
# files='?'
# echo $files
?
# files="?"
# echo $files
?
# files=?
# echo $files
?
edit:
In zsh, you would do it like so:

Code:
# files=(?)
# echo $files
a b c d

Last edited by smoked kipper; 08-05-2008 at 12:00 PM.
 
Old 08-05-2008, 12:15 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I'm not sure what point you are making. I was making a point about expansion order of bash command lines.
 
Old 08-05-2008, 12:29 PM   #7
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
My point was just that I don't know about expansion of bash command lines since I don't use it, thus your statement:

Quote:
This isn't quite correct.
is incomplete since it is shell dependent (and you did not specify a particular shell).
 
Old 08-05-2008, 12:33 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I think we can assume standard defaults unless otherwise indicated. zsh is pretty fringe.
 
Old 08-05-2008, 12:57 PM   #9
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
I don't see your point. I'm simply pointing out that bash's behaviour is not universal, as was your implication.
 
Old 08-05-2008, 01:58 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
This is silly. I've made my point clear: "The important point here is knowing the order of command line expansion."
 
Old 08-05-2008, 04:38 PM   #11
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
But the order is not relevent for the OPs problem. I see your point that wildcards are not expanded upon assignment, only when the parameter is dereferenced, but that does not affect the pipe character, since it is not related to expansion. In order for the | to be handled properly within quotes, one must use eval.

Quote:
Redirection and pipes are done before parameter expansion
I also don't follow this, all expansion must be done before any pipeline/redirection.
 
Old 08-05-2008, 07:42 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I'll try to make clear.

You said: "Shell meta characters are not interpreted inside quotes when executed directly."
I say: This is not correct - some meta chars ARE interpreted inside (double) quotes when executed directly.

Quote:
Originally Posted by Mr. C.
Redirection and pipes are done before parameter expansion
Quote:
Originally Posted by smoked kipper
I also don't follow this, all expansion must be done before any pipeline/redirection.
No, I'm saying the shell parses a command line for pipes and redirection symbols *before* any other type of expansion. The command line is then expanded next. This is why pipe and redirection is not subject to parameter expansion, while file name expansion is.
 
Old 08-08-2008, 01:28 PM   #13
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
I'll try to make clear.

You said: "Shell meta characters are not interpreted inside quotes when executed directly."
I say: This is not correct - some meta chars ARE interpreted inside (double) quotes when executed directly.
Sorry, I was a bit sleep deprived the other day (I should probably refrain from posting when I haven't slept for 24+ hours), I think I went off on a bit of a tangent. I'd forgotten that most shells do filename globbing in parameter expansion (as mentioned, zsh doesn't do this by default, though there's an option to enable it).

Quote:
No, I'm saying the shell parses a command line for pipes and redirection symbols *before* any other type of expansion. The command line is then expanded next. This is why pipe and redirection is not subject to parameter expansion, while file name expansion is.
Ah, parsing, I'm with you now (I was a bit confused when you said 'order of expansion', since pipes/redirections are not related to expansion). You mean the shell parses the command line and builds the syntax tree, registering any redirections along the way, then it traverses the syntax tree, performing any expansion/substitution. Gotcha.

Thus, e.g.

Code:
cmd='echo * > file'
parses as a single "word", hence no redirection is indicated in the syntax tree. And since the shell performs filename matching in parameter expansion, it expands the '*' when you dereference the variable, but the '>' is not treated specially at the execution stage.
 
  


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
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
Write command in named pipes sahel Programming 7 12-28-2005 06:05 AM
Problem executing this piped command: JAVA randomx Programming 1 07-16-2004 07:39 PM
Problem executing from command line jleyba213 Linux - General 4 04-09-2004 12:43 PM
Executing command lines in C/C++ ReverseLogic Programming 2 05-28-2002 02:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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