LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do I escape dash when using grep (https://www.linuxquestions.org/questions/programming-9/how-do-i-escape-dash-when-using-grep-914235/)

hui 11-18-2011 07:35 AM

how do I escape dash when using grep
 
Code:

MAP={" -> ../../"}
id_1=`ls -l /dev/disk/by-id | grep $MAP`

==
Code:

grep: invalid option -- '>'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

Code:

\
doesn't work

colucix 11-18-2011 07:44 AM

Code:

MAP=" -> ../../"
id_1=`ls -l /dev/disk/by-id | grep -- $MAP`

in many GNU commands the double -- is a signal to terminate the options, so that you can pass an hyphen as first character of the argument.

hui 11-18-2011 07:57 AM

Quote:

Originally Posted by colucix (Post 4527503)
Code:

MAP=" -> ../../"
id_1=`ls -l /dev/disk/by-id | grep -- $MAP`

in many GNU commands the double -- is a signal to terminate the options, so that you can pass an hyphen as first character of the argument.

Code:

MAP={"-- -> ../../"}
id_1=`ls -l /dev/disk/by-id | grep $MAP`

==

Code:

grep: invalid option -- '>'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.


grail 11-18-2011 08:22 AM

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
grep (GNU grep) 2.9
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.


hui 11-18-2011 11:10 AM

Quote:

Originally Posted by grail (Post 4527527)
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:

GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Code:

MAP={" \-> ../../"}
==
Code:

grep: \->: No such file or directory
grep: ../../}': No such file or directory

Code:

id_1=`ls -l /dev/disk/by-id | grep \'$MAP\'`
==
Code:

grep: invalid option -- '>'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.


colucix 11-18-2011 11:45 AM

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?

hui 11-18-2011 11:53 AM

Quote:

Originally Posted by colucix (Post 4527667)
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?

to be honest, no idea. some page mentioned they were a good idea.
as for the triggering part, that is incorrect. outputs all the same errors, brackets or no brackets.

grail 11-18-2011 12:49 PM

How about:
Code:

MAP=' -> ../../'
By the way, you still have not told us what you are really trying to do with this poor example?

hui 11-18-2011 12:59 PM

it seems to work when I execute it manually though.

colucix 11-18-2011 01:10 PM

Quote:

Originally Posted by hui (Post 4527727)
it seems to work when I execute it manually though.

Please, can you post the command working from the command line? Thanks.

hui 11-18-2011 01:20 PM

Quote:

Originally Posted by colucix (Post 4527741)
Please, can you post the command working from the command line? Thanks.

Code:

ls | grep ' -> ../../'
no escape chars, no nothing.
additional:
this same code also works from a script, but only if it's not in a variable.

hui 11-18-2011 01:57 PM

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
id_1=`ls -l /dev/disk/by-id | grep 'ata-ST3250823AS_5ND145H9 -> ../../'`
id_2=`ls -l /dev/disk/by-id | grep 'ata-Maxtor_2B020H1_B1LJZS9E -> ../../'`
id_3=`ls -l /dev/disk/by-id | grep 'ata-Maxtor_2B020H1_B1JDS2RE -> ../../'`

#extract specific disks
disk_1=/dev${id_1##*..}
disk_2=/dev${id_2##*..}
disk_3=/dev${id_3##*..}

#shutdown
hdparm -Y $disk_1
hdparm -Y $disk_2
hdparm -Y $disk_3


David the H. 11-18-2011 02:33 PM

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:


grep -- '-foo'        # works
grep -e '-foo'        # works
grep '-foo'        # does not work

As for this:
Code:

ls | grep ' -> ../../'
It works because your hard-quoted string starts with a space, not a "-". Thus you don't get any errors.


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--'
grep -e "$pattern"


David the H. 11-18-2011 02:57 PM

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'
disk_2='ata-Maxtor_2B020H1_B1LJZS9E'
disk_3='ata-Maxtor_2B020H1_B1JDS2RE'

hdparm -Y "/dev/disk/by-id/$disk_1"
hdparm -Y "/dev/disk/by-id/$disk_2"
hdparm -Y "/dev/disk/by-id/$disk_3"

***
Edit: even shorter...

Code:


disks=(
        ata-ST3250823AS_5ND145H9
        ata-Maxtor_2B020H1_B1LJZS9E
        ata-Maxtor_2B020H1_B1JDS2RE
)

hdparm -Y "${disks[@]/#//dev/disk/by-id/}"

***

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"
Again, if you'd explained what you actually wanted to do, rather than what you thought you should do, this could've been solved much earlier.


All times are GMT -5. The time now is 08:24 PM.