The Overflow Blog
E.g., The @Thomas's solution didn't work for me for some reason but I had longer string with special characters and whitespaces so I just changed the parameters like this:To subscribe to this RSS feed, copy and paste this URL into your RSS reader. if echo "40922|OPR MO 12345|OPR MO 12345|12345|202|local|LMNO" | grep -q "MO" then echo "FOUND" else echo "NOT FOUND" fi share | improve this answer | follow | answered Oct 15 '10 at 16:19 Quiet; do not write anything to standard output.
You don't need to test the output of grep, you can just use grep -q and call grep directly from if as Thomas does in his answer. Stack Overflow works best with JavaScript enabled Also see the As rightfully pointed out in the comments, the above approach silently treats error cases as if the string was found.
Shell scripts intended to be portable to traditional grep should avoid both -qand -s and should redirect output to /dev/null instead. By using our site, you acknowledge that you have read and understand our
I'd like to check in Bash before I'll add a directory name if that name already exists in the file.The exit status is 0 (true) if the name was found, 1 (false) if not, so: Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
But the exit status is 2 if an error occurred, unless the 1) Short test for a name in a path (I'm not sure this might be your case)grep -c will return the count of how many times the string occurs in the file.If I understood your question correctly, this should do what you need.If you just want to check the existence of one line, you do not need to create a file.
Portability note: unlike GNU grep, traditional grep did not conform to POSIX.2, because traditional grep lacked a -q option and its -s option behaved like GNU grep’s -q option. If you just want to find if your string exists in the file at all, leave that off. – sorpigal Jan 20 '11 at 17:26 Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesTo find all of the strings inside a file, you can run grep in FOR loop: If I execute this command from bash script how to catch 0 or 1 into a variable ? ÖÏí,±¾ÎÄÊÇ×÷ÕßÒ»¸öÐÇÆڵŤ×÷³É¹û,µÃÀ´²»Ò×,ÐèÒªµÄÅóÓÑ¿ÉÒԲο¼ÏÂ
Stack Overflow for Teams is a private, secure spot for you and @ThomWiggers I tried the same and it worked for me. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under If you want to find if your string exists exactly but without matching an entire line necessarily (i.e., as a whole word), use -w.-F does not affect the file processing, it affects how PATTERN is interpreted. your coworkers to find and share information. Free 30 Day Trial
Exit immediately with zero status if any match is found, even if an error was detected.
Select only those matches that exactly match the whole line.
If you want to handle errors in a different way, you'll have to omit the Normally, the exit status is 0 if selected lines are found and 1 otherwise.
您的位置:首页 → 脚本专栏 → linux shell → shell中grep命令 详解 Shell中的grep命令 更新时间:2017年05月20日 10:01:39 转载 作者:SaiW-n_n- Featured on Meta Typically, PATTERN is interpreted as a regex, but with -F it will be interpreted as a fixed string.Why are the spaces necessary before and after the $file_contenet?Plus 1 for the fast solution and a more generalizable oneYou don't need to test the output of grep, you can just use I removed the example script, it didn't add anything to the answer given by Thomas. @Toren Most recent exit status can be accessed using A couple of notes for folks looking at this answer: 1) In bash, 0 is always true and anything else is always false 2) Only use the -x flag if you want the entire line to match exactly. In addition, the question didn't include checking whether the directory exists before adding it to the list (it could be a list of deleted directories, after all).
Dans cet exemple nous utilisons le fichier /etc/passwd qui contient les informations relatives aux comptes utilisateurs existants sur le système, nous faisons appelle à la commande grep pour rechercher si romain est présent ou non dans ce fichier.