Check if a variable is having value | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Check if a variable is having value

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-12-2013
anil510 anil510 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 28 June 2014, 3:33 PM EDT
Posts: 76
Thanks: 38
Thanked 1 Time in 1 Post
Check if a variable is having value

I have a script /root/asas with following contents.


Code:
#!/bin/bash

ha=`cat /etc/passwd | grep sandra`

if [ -n $ha ]; then
        echo "Sandra is in /etc/passwd"
        echo "variable ha is $ha"
else
        echo "Sandra is NOT in /etc/passwd"
        echo "variable ha is $ha"

fi

What I need is when "cat" does not find sandra in /etc/passwd, it should print "Sandra is NOT in /etc/passwd". But this is not giving correct result. See below.

Code:
[root@server ]# grep sandra /etc/passwd
#sandra

[root@server ]# sh /root/asas
Sandra is in /etc/passwd
variable ha is #sandra

[root@server ]# grep sandra /etc/passwd  --> sandra removed from /etc/passwd

[root@server ]# sh /root/asas
Sandra is in /etc/passwd  --> supposed to be "Sandra is NOT in /etc/passwd"
variable ha is

Sponsored Links
    #2  
Old 02-12-2013
kg_gaurav kg_gaurav is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 27 May 2014, 1:55 AM EDT
Posts: 44
Thanks: 3
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by anil510 View Post
I have a script /root/asas with following contents.


Code:
#!/bin/bash

ha=`cat /etc/passwd | grep sandra`

if [ -n $ha ]; then
        echo "Sandra is in /etc/passwd"
        echo "variable ha is $ha"
else
        echo "Sandra is NOT in /etc/passwd"
        echo "variable ha is $ha"

fi

What I need is when "cat" does not find sandra in /etc/passwd, it should print "Sandra is NOT in /etc/passwd". But this is not giving correct result. See below.

Code:
[root@server ]# grep sandra /etc/passwd
#sandra

[root@server ]# sh /root/asas
Sandra is in /etc/passwd
variable ha is #sandra

[root@server ]# grep sandra /etc/passwd  --> sandra removed from /etc/passwd

[root@server ]# sh /root/asas
Sandra is in /etc/passwd  --> supposed to be "Sandra is NOT in /etc/passwd"
variable ha is




If you just want to print Sandra is NOT in /etc/passwd" then why are you using " echo "variable ha is $ha" in else of your script?
Sponsored Links
    #3  
Old 02-12-2013
MKR MKR is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 6 July 2013, 12:22 AM EDT
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
Try this

#!/bin/bash
more /etc/passwd | grep "sandra" 2>/dev/null
if [ "$?" = "0" ]; then
echo "FOUND"
else
echo "NOT FOUND"
fi
    #4  
Old 02-12-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts
Change

Code:
if [ -n $ha ]; then

to
either

Code:
if [ -n "$ha" ]; then

or

Code:
if [[ -n $ha ]]; then

.
There are at least a couple of improvements needed in your "script".
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check the value of a variable neeraj617 Shell Programming and Scripting 7 04-06-2012 06:25 PM
Check if a variable is zero soujiv Shell Programming and Scripting 8 12-15-2010 04:41 PM
Check if a variable has a value assigned? sumeet Shell Programming and Scripting 3 05-03-2009 11:48 PM
How to check if a variable contains a . gekkos Shell Programming and Scripting 3 11-27-2008 02:00 PM
to check variable if its non numeric sachin.gangadha Shell Programming and Scripting 3 12-06-2007 04:33 PM



All times are GMT -4. The time now is 09:51 AM.