LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-30-2004, 06:35 AM   #1
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

Rep: Reputation: 15
Pipe inside variable isn't working in bash


Hi, folks!

See the examples:

bash$ X="ls"; $X
file1 file2 file3
*** Works correctly!

bash$ X="ls | wc"; $X
ls: |: No such file or directory
ls: wc: No such file or directory
*** Doesn't work!

How to make the last command work correctly as first?

Thanks in advance!

Reginald0
 
Old 01-30-2004, 07:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Both don't use correct syntax, although bash does know how to interpret the first example.

X="ls"; $X (X=ls, then execute $X, which will execute ls)
should be:
X="`ls`" ; echo $X (X is filled with the output of ls, then $X is echoed.

X="ls | wc"; $X (X= ls | wc, $X is executed. ls tries to list |, and wc is not understood.
should be:
X="`ls | wc`" ; echo $X (X is filled with the result of ls | wc, then $X is echoed)
 
Old 01-30-2004, 07:56 AM   #3
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

Original Poster
Rep: Reputation: 15
It works like you said, but the results can be odd in some cases:

bash$ X="ls -al"; $X
total 20
drwxr-xr-x 2 root root 4096 Jan 30 11:31 .
drwxr-x--- 18 root root 4096 Jan 30 11:31 ..
-rw-r--r-- 1 root root 24 Jan 30 11:31 file1
-rw-r--r-- 1 root root 40 Jan 30 11:31 file2
-rw-r--r-- 1 root root 56 Jan 30 11:31 file3
*** Normal

bash$ X="`ls -al | grep root`"; echo $X
drwxr-xr-x 2 root root 4096 Jan 30 11:31 . drwxr-x--- 18 root root 4096 Jan 30 11:31 .. -rw-r--r-- 1 root root 24 Jan 30 11:31 file1 -rw-r--r-- 1 root root 40 Jan 30 11:31 file2 -rw-r--r-- 1 root root 56 Jan 30 11:31 file3
*** Odd

In fact I need a general solution for this case, cause the commands inside $X can vary.
 
Old 01-30-2004, 08:10 AM   #4
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
The reason it's working differently, as far as I can tell, is because you are doing two different things here, or doing basically the same thing but in two different ways.

In the first example: X="ls -al"; $X
X is equal to the string "ls -al"
using the command $X is just telling bash to replace $X with "ls -a" and it runs the string as a command.

In the second example: X="`ls -al | grep root`"; echo $X
X is equal to the output of "ls -al" (notice the ` which tells bash to run its contents as a command)
In this case, $X is not the command, but rather an argument to "echo"
In addition, the first example puts out ls -al and stops there. The second example pipes the output to grep, which outputs only lines containing "root"
 
Old 01-30-2004, 08:21 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
The behaviour is correct in both cases.

X="ls -al" ; $X - $X will execute a 'normal' ls -al. 'Normal' being the way you are used to when entering the ls -al command on the command line, all output lines by itself.

X="`ls -al`" - $X is one string. The echo command will display the string as is (one line). If you want to break up this line you could use printf (see man 3 printf or a book: unix/linux in a nutshell or a good book about C). But this might not work for you. You need to tailor printf for specific output (ls looks different then say ls -latr).

The following sollution works, but I only use it when absolutely neccesary:

X="ls -al | grep root"; eval $X
 
Old 01-30-2004, 09:43 AM   #6
Reginald0
LQ Newbie
 
Registered: Dec 2002
Location: Brazil
Posts: 26

Original Poster
Rep: Reputation: 15
druuna,

You're absolutely right! You pointed me two ways to do the same thing with different results, but the last way works exactly like I want.

Many thanks!

Reginald0
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM
Read the output from a pipe with bash ? fluppi Linux - Software 3 01-13-2004 12:59 PM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM


All times are GMT -5. The time now is 02:48 AM.

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