LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-22-2007, 05:17 AM   #1
furqan_sindhu
LQ Newbie
 
Registered: May 2006
Posts: 25
Blog Entries: 1

Rep: Reputation: 15
Question if condition


hello people !!!

i have a variable named "word"

what would be the condition to check whether that word is empty, i mean to return true if word=""



????
 
Old 09-22-2007, 05:28 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
In Bash the test is -z, as in
Code:
if [ -z $pippo ] ; then
   echo empty variable: pippo
fi
See man test for details and any other available expression.
 
Old 09-22-2007, 05:40 AM   #3
furqan_sindhu
LQ Newbie
 
Registered: May 2006
Posts: 25
Blog Entries: 1

Original Poster
Rep: Reputation: 15
great, that worked!!

What's the difference in your solution and the following
Code:
if [ $word != "" ]

and please also tell how can i check whether a file contains some specific words ???

Any link, any idea for that ???
 
Old 09-22-2007, 06:16 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

If word is not set, then if [ $word != "" ] will resolve to: if [ != "" ]. As a result bash will throw an error ([: !=: unary operator expected) because there is nothing to check against.
If word is set, then there are values to the left and to the right of the check (!= in this case) and bash can do its thing.

The -z option is specially made to check empty (zero) variables.


You can use grep to check if a string is present in a file: grep "something" infile. See grep's manpage for details.

Hope this helps.
 
Old 09-22-2007, 06:50 AM   #5
RenRen
LQ Newbie
 
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0
I don't understand what you're all talking about . . . . . .
 
Old 09-22-2007, 06:56 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

@RenRen: And the question is??

If you have a specific question we can help you, but these comments don't add to this (or any) thread.
 
  


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
using OR condition in if condition Fond_of_Opensource Linux - Newbie 2 10-20-2006 12:34 AM
cedega and CS: condition zero bojann Linux - Games 8 06-27-2005 01:49 PM
What condition I don't need initrd? Chowroc Linux - General 3 05-22-2005 02:59 AM
simple if condition sonesay Programming 10 05-21-2004 10:34 PM
CounterSrike: Condition Zero Comarez Linux - Games 2 04-20-2004 02:08 PM


All times are GMT -5. The time now is 03:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration