Sponsored Content
Top Forums Shell Programming and Scripting pipe to grep doesn't work in bash script Post 302597726 by agama on Saturday 11th of February 2012 07:52:02 PM
Old 02-11-2012
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:


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

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:
Code:
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:


Code:
+ 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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Devel::BeginLift(3pm)					User Contributed Perl Documentation				     Devel::BeginLift(3pm)

NAME
Devel::BeginLift - make selected sub calls evaluate at compile time SYNOPSIS
use Devel::BeginLift qw(foo baz); use vars qw($i); BEGIN { $i = 0 } sub foo { "foo: $_[0] "; } sub bar { "bar: $_[0] "; } for (1 .. 3) { print foo($i++); print bar($i++); } no Devel::BeginLift; print foo($i++); outputs - foo: 0 bar: 1 foo: 0 bar: 2 foo: 0 bar: 3 foo: 4 DESCRIPTION
Devel::BeginLift 'lifts' arbitrary sub calls to running at compile time - sort of a souped up version of "use constant". It does this via some slightly insane perlguts magic. import use Devel::BeginLift qw(list of subs); Calls Devel::BeginLift->setup_for(__PACKAGE__ => @list_of_subs); unimport no Devel::BeginLift; Calls Devel::BeginLift->teardown_for(__PACKAGE__); setup_for Devel::BeginLift->setup_for($package => @subnames); Installs begin lifting magic (unless already installed) and registers "${package}::$name" for each member of @subnames to be executed when parsed and replaced with its output rather than left for runtime. teardown_for Devel::BeginLift->teardown_for($package); Deregisters all subs currently registered for $package and uninstalls begin lifting magic is number of teardown_for calls matches number of setup_for calls. setup_for_cv $id = Devel::BeginLift->setup_for_cv(&code); Same as "setup_for", but only registers begin lifting magic for one code reference. Returns an id to be used in "teardown_for_cv". teardown_for_cv Devel::BeginLift->teardown_for_cv($id); Deregisters begin lifting magic referred to by $id. AUTHOR
Matt S Trout - <mst@shadowcatsystems.co.uk> Company: http://www.shadowcatsystems.co.uk/ Blog: http://chainsawblues.vox.com/ LICENSE
This library is free software under the same terms as perl itself perl v5.14.2 2012-04-22 Devel::BeginLift(3pm)
All times are GMT -4. The time now is 02:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy