pipe to grep doesn't work in bash script

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
pipe to grep not working in bash script


I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script:

cmnd="gvfs-mount -l | grep -i '${mount}'"
dastring=`${cmnd}` #remove me
echo "Output from ${cmnd}" #remove me
echo "$dastring"

$dastring looks like the output from gvfs-mount (although it's detailed as if I gave it the -i option in addition to -l).

If I copy and paste the ${cmnd} (from where I echo it) and run it directly from the terminal, it display the line of text I'm looking for with grep.

Can anyone help me figure out why the pipe works directly in the terminal but does not appear to be doing so in the script?

Thanks in advance.

Last edited by kcstrom; 02-11-2012 at 01:39 PM..
# 2  
You can either do this:
eval dastring=\`${cmnd}\`

or do this for example:
dastring=$(gvfs-mount -l | grep -i "${mount}")

-or with back ticks:-
dastring=`gvfs-mount -l | grep -i "${mount}"`

# 3  
Originally Posted by Scrutinizer
eval dastring=\`${cmnd}\`

Thanks Scrutinizer - this is what I needed as I want to keep cmnd around for print out later in the script.

I don't understand why backticks in


didn't run the command and assign the output to dastring. It must be that dastring equaled that string including the backticks - but it doesn't quite make sense. Could you explain what's going on here?

Thanks a bunch!
# 4  
When the shell expands the variable holding the command, it recognises that the pipe symbol is special, and quotes it. If you consider this example:

cmd="echo  foo | awk '{print \"bar \"; print }'"

you would expect that when $cmd is executed the string "bar foo" would be assigned to data. However, the string assigned to data is actually:
foo | awk '{print "bar "; print }'

If you execute it with -x you'll see in the trace the quotes that the shell adds and that the pipe symbol is being passed to echo (along with everything else past it), and not interpreted by the shell. The output when I ran it was:

+ cmd=$'echo  foo | awk \'{print "bar "; print }\''
+ echo foo '|' awk $'\'{print' '"bar' '";' print $'}\''
+ data=$'foo | awk \'{print "bar "; print }\''
+ echo $'foo | awk \'{print "bar "; print }\''
foo | awk '{print "bar "; print }'

Notice in the second line the '|' is the clue that the shell has treated the contents of the variable as a single string and has not parsed it as a command.

When you add the eval in front of the expansion, the shell expands the variable and then parses it as a command. The result is that the pipe is then recognised as you intended and both commands are executed as expected.

In your case, the whole string would have been passed to the gvfs-mount command and as that string contained -i, it would have listed details. I suspect it might have also complained about the pipe and/or grep but that would have been to stderr and not captured in your variable.

When things seem strange, I usually put a set -x before the statement that isn't working and set +x ater it to get the details of how the shell is interpreting my code. The output is usually enough to clear up the confusion.

Hope this helps
# 5  
Originally Posted by agama
When things seem strange, I usually put a set -x before the statement that isn't working and set +x ater it to get the details of how the shell is interpreting my code. The output is usually enough to clear up the confusion.
Hope this helps
Thanks agama! Being a bash newb still, I didn't know about the +/-x - that will come in very handy in the future I think. Thanks for that and the detailed explanation regarding pipe!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #960
Difficulty: Medium
The WannaCry ransomware attack was a May 2017 worldwide cyberattack by the WannaCry ransomware cryptoworm, which targeted computers running Windows by encrypting data and demanding ransom payments in the Bitcoin.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash diff date doesn't work

Hi everyone, I've an issue trying to soustracte two dates, e.g: d1=$(date -d "Nov 18, 2017" +%s) d2=$(date +%s) # Today we are 2017-11-16 echo "$(( (d1 - d2) / 86400 ))" Output: 1 I don't understand why it doesn't work. for me, it should give "18 - 16 = 2". Much appreciated... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

2. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

3. Shell Programming and Scripting

Rsync in bash script doesn't work even after placing pub key in target server

Hello Friends, My bash script is like this #!/bin/bash # request Bourne shell as shell for job #$ -S /bin/bash # assume current working directory as paths #$ -cwd #$ -N rsync-copy # # print date and time date rsync -rltD --progress "ssh -i /home/myname/.ssh/id_rsa"... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Grep doesn't work when assigning to variable

Hello, First of all, I'd like to say hello to all members of forum. Can You please help me with the matter described below? I am trying to fetch a data from the file to variable, I am doing this using below script: returned=`tail -50 SapLogs.log | grep -i -E "Error|"` echo $returned ... (2 Replies)
Discussion started by: jedzio
2 Replies

5. Shell Programming and Scripting

two grep in one script doesn't work?

Hi there, the following script doesn't work. the first part works, then the second 'grep' fails with ': not found'. However, if I take out the second part (starting with the grep command) and put in a seperate script, it works. everyone know what's wrong here? no two 'grep' in one script, that... (2 Replies)
Discussion started by: monkey77
2 Replies

6. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

7. Solaris

grep -e doesn't work on solaris

grep -e doesn't work in Soalris. Same script with grep -e worked on AIX/HP/LINUX.. I would like to search a list of patterns on "log.txt" like ... grep -e FATAL -e ERROR log.txt I get the error message as grep: illegal option -- e Usage: grep -hblcnsviw pattern file . . . (3 Replies)
Discussion started by: jmkraja
3 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies

10. Shell Programming and Scripting

grep doesn't work within shell script?

I am trying to run the following code from a script file but it complains that syntax of (both instances of) grep is wrong. When I copy and paste it to the terminal, it is OK. Any idea what the problem might be? set i = `grep -c #define flags.h` while ($i>20) @ i-- my func (`cat... (4 Replies)
Discussion started by: barisgultekin
4 Replies

Featured Tech Videos