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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-27-2006, 12:43 PM
|
#1
|
Member
Registered: Jul 2006
Posts: 57
Rep:
|
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.
|
|
|
07-27-2006, 01:28 PM
|
#2
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
Then do:
Code:
if [ $( echo $x | grep -c ".*" ) -gt 0 ]
then echo "Match found."
fi
|
|
|
07-27-2006, 01:31 PM
|
#3
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
redirect to /dev/null. Read man bash, maybe?
if echo | grep >/dev/null;
|
|
|
07-27-2006, 01:33 PM
|
#4
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
By the way, are you checking for empty string? Read man bash on test ( [ ), it has -z and -n flags to do it.
|
|
|
07-27-2006, 01:42 PM
|
#5
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Rep:
|
Use the grep's quiet option.
Code:
if echo $x | grep -q ".*"; then
...
|
|
|
07-27-2006, 01:48 PM
|
#6
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
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)).
|
|
|
|
07-27-2006, 01:53 PM
|
#7
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
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. 
|
|
|
07-27-2006, 02:06 PM
|
#8
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
Well, it's non-POSIX!
|
|
|
07-27-2006, 02:12 PM
|
#9
|
Member
Registered: Jul 2006
Posts: 57
Original Poster
Rep:
|
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.
|
|
|
07-27-2006, 02:14 PM
|
#10
|
Member
Registered: Jul 2006
Posts: 57
Original Poster
Rep:
|
Quote:
Originally Posted by marozsas
Use the grep's quiet option.
Code:
if echo $x | grep -q ".*"; then
...
|
This worked perfectly.
|
|
|
All times are GMT -5. The time now is 10:26 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|