ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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>.
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.
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.
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.
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):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.