LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-27-2006, 12:43 PM   #1
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Rep: Reputation: 15
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then"


if echo $x | grep ".*"; then
...
fi

Is there a way of getting around this type of stuff without actually printing it out to the screen? The user shouldn't be seeing this. I'm trying to test if the string matches the grep regex.
 
Old 07-27-2006, 01:28 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Then do:
Code:
if [ $( echo $x | grep -c ".*" ) -gt 0 ]
    then echo "Match found."
fi
 
Old 07-27-2006, 01:31 PM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
redirect to /dev/null. Read man bash, maybe?
if echo | grep >/dev/null;
 
Old 07-27-2006, 01:33 PM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
By the way, are you checking for empty string? Read man bash on test ( [ ), it has -z and -n flags to do it.
 
Old 07-27-2006, 01:42 PM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Blog Entries: 2

Rep: Reputation: 68
Use the grep's quiet option.
Code:
if echo $x | grep -q ".*"; then
...
 
Old 07-27-2006, 01:48 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Note that there's a bash built-in to do this. From "man bash":
Quote:
[[ expression ]]
[...]
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)).
 
Old 07-27-2006, 01:53 PM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by spirit receiver
Note that there's a bash built-in to do this. From "man bash":
Ah yes, one of those little known bash tricks.
 
Old 07-27-2006, 02:06 PM   #8
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Well, it's non-POSIX!
 
Old 07-27-2006, 02:12 PM   #9
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by spirit receiver
Note that there's a bash built-in to do this. From "man bash":
I originally tried that, but I had to work around it because my version of bash didn't support it.
 
Old 07-27-2006, 02:14 PM   #10
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by marozsas
Use the grep's quiet option.
Code:
if echo $x | grep -q ".*"; then
...
This worked perfectly.
 
  


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
What does echo alias "char-major-10-224 off" >> /etc/modules.conf do? abefroman Linux - Software 2 09-16-2009 10:10 PM
simple "echo > qbc" cron jobs doesn't run shole Linux - Newbie 3 09-23-2006 02:09 PM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM
(echo -n "standby" > /sys/power/state) only allowed for root? brynjarh Debian 5 02-14-2005 09:08 AM
want full description of "echo num > / path/...." zameer_india Linux - Newbie 5 01-08-2005 03:10 AM

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

All times are GMT -5. The time now is 10:26 AM.

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