Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Check if a variable is having value

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-12-2013
anil510 anil510 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 2:50 AM EDT
Posts: 100
Thanks: 47
Thanked 2 Times in 2 Posts
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 Unix and Linux 02-12-2013
kg_gaurav kg_gaurav is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 17 September 2014, 6:39 AM EDT
Posts: 45
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 Unix and Linux 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 Unix and Linux 02-12-2013
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 7:53 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:08 AM.