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:
$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 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?
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:
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:
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:
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.
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!
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)