I have a script /root/asas with following contents.
#!/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.
[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