LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2011, 07:35 AM   #1
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Rep: Reputation: Disabled
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
 
Old 11-18-2011, 07:44 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 11-18-2011, 07:57 AM   #3
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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.

Last edited by hui; 11-18-2011 at 08:04 AM.
 
Old 11-18-2011, 08:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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>.
 
Old 11-18-2011, 11:10 AM   #5
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.

Last edited by hui; 11-18-2011 at 11:12 AM.
 
Old 11-18-2011, 11:45 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?
 
Old 11-18-2011, 11:53 AM   #7
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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.

Last edited by hui; 11-18-2011 at 11:55 AM.
 
Old 11-18-2011, 12:49 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about:
Code:
MAP=' -> ../../'
By the way, you still have not told us what you are really trying to do with this poor example?
 
Old 11-18-2011, 12:59 PM   #9
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
it seems to work when I execute it manually though.
 
Old 11-18-2011, 01:10 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by hui View Post
it seems to work when I execute it manually though.
Please, can you post the command working from the command line? Thanks.
 
Old 11-18-2011, 01:20 PM   #11
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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.

Last edited by hui; 11-18-2011 at 01:39 PM.
 
Old 11-18-2011, 01:57 PM   #12
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-18-2011, 02:33 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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"
 
1 members found this post helpful.
Old 11-18-2011, 02:57 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 11-18-2011 at 03:04 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to configure dash jdkaye Debian 4 08-06-2009 11:05 AM
Need help with BASH and DASH cosmicbrat Linux - Software 1 03-12-2009 08:39 PM
Type en-dash r-t Linux - General 5 04-04-2008 01:57 PM
Dash ignoring PATH? BuilderQ Linux - Software 3 12-18-2007 06:00 PM
dash logout script? Coviti Debian 2 09-24-2007 01:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration