how do I escape dash when using grep
Code:
MAP={" -> ../../"} Code:
grep: invalid option -- '>' Code:
\ |
Code:
MAP=" -> ../../" |
Quote:
Code:
MAP={"-- -> ../../"} Code:
grep: invalid option -- '>' |
My question would be what are you actually trying to achieve? If I am correct you are trying to parse ls which is fraught with danger as outlined here
I would also question your version of grep as simply placing the data in single quotes or in fact using the backslash to escape both seem to work fine for me. Code:
$ grep --version |
Quote:
Code:
GNU grep 2.6.3 Code:
MAP={" \-> ../../"} Code:
grep: \->: No such file or directory Code:
id_1=`ls -l /dev/disk/by-id | grep \'$MAP\'` Code:
grep: invalid option -- '>' |
The question is: why do you use brackets to assign the MAP variable? They trigger all the errors you have shown. What is their meaning?
|
Quote:
as for the triggering part, that is incorrect. outputs all the same errors, brackets or no brackets. |
How about:
Code:
MAP=' -> ../../' |
it seems to work when I execute it manually though.
|
Quote:
|
Quote:
Code:
ls | grep ' -> ../../' additional: this same code also works from a script, but only if it's not in a variable. |
script is done, although not as I imagined it due to my little understanding/strange behavior of shell variables. Also the original intention: shut down all disks that aren't mounted as /. Appreciate the help.
Code:
#get IDs |
1) As colucix mentioned before, using "--" as an option generally signals the end of options, so that the next value will be treated as an argument. grep also has the "-e" option for specifying expressions, which also tells it to ignore leading dashes in the pattern.
Code:
Code:
ls | grep ' -> ../../' 2) As grail pointed out, trying to extract information from the output of ls is not usually recommended. If you could please explain what your real purpose is, we may be able to give you a better solution. So please tell us your actual scripting goal. 3) Just to make it clear, if you want to use a variable for the pattern in grep, put the exact pattern you want to search for inside single-quotes. Then, in the grep command, put the variable name surrounded by double quotes (And don't forget -e or --, if necessary): Code:
pattern='--foo--' |
Ah, seeing your final script, it's just as I thought. You're going through lots of unnecessary steps to do what you want.
The entries in /dev/disk/by-id are simply symlinks to the actual device node. So why not just use them directly? Code:
disk_1='ata-ST3250823AS_5ND145H9' Edit: even shorter... Code:
When you really need to get the target location of a link, don't grep it, use readlink. Code:
readlink -f "/dev/disk/by-id/ata-ST3250823AS_5ND145H9" |
All times are GMT -5. The time now is 08:24 PM. |