Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page simple grep in a shell script
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-23-2008   #1 (permalink)
sandeep t
Just Joined!
 
Join Date: Apr 2007
Posts: 32
simple grep in a shell script

I have a following shell script on Linux 64bit machine.
Code:
If [ `uname -a | grep 64` ]
then
echo 64bit
fi
The above script doesn't work properly. Why?
Also, the following script works fine. What's the difference?
Code:
if [ `uname | grep Linux` ]
then
echo Linux
fi
sandeep t is offline   Reply With Quote
Old 06-23-2008   #2 (permalink)
ghostdog74
Linux User
 
Join Date: Aug 2006
Posts: 334
capital If?
ghostdog74 is offline   Reply With Quote
Old 06-23-2008   #3 (permalink)
sandeep t
Just Joined!
 
Join Date: Apr 2007
Posts: 32
Sorry, that was a typo while writing here.. It is 'if'
sandeep t is offline   Reply With Quote
Old 06-23-2008   #4 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 719
post the error you get when you run the script
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 06-23-2008   #5 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 1,016
The key is in the output.

This:

Code:
uname -a | grep 64
produces an output which contains spaces, so, if will complain that there are too many arguments. Quoting when working with strings is a good thing to do.

On the contrary:

Code:
uname | grep Linux
This other command produces a single word as output. So, it will work even if you don't quote it correctly. In any case, it wouldn't hurt either to quote it. So, let's modify the code this way:

Code:
If [ "`uname -a | grep 64`" ]
then
echo 64bit
fi
However, I think that you should be parsing "uname -m" instead. There are lots of ways that a 64 could appear even if you are not using a 64 bits kernel so "uname -m" might be more accurate. It should say "x86_64".
i92guboj is offline   Reply With Quote
Old 06-23-2008   #6 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 1,016
Please, delete this post, I posted the same thing twice by accident. Sorry.
i92guboj is offline   Reply With Quote
Old 06-23-2008   #7 (permalink)
sandeep t
Just Joined!
 
Join Date: Apr 2007
Posts: 32
Great!
Thanks, This is the reply I was expecting..
sandeep t is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:02 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0