i am finding some pattern this way..
fname=`grep -w "^$index" $HOME/UnixCw/backup/Path.txt`
how to check that fname is empty i.e. if pattren doesnt found then i want to do other operations....
i am finding some pattern this way..
fname=`grep -w "^$index" $HOME/UnixCw/backup/Path.txt`
how to check that fname is empty i.e. if pattren doesnt found then i want to do other operations....
look at the -s file test operator
I tried but its not working...can you please elaborate more..I am very new to unix...sorry for trouble
if [ -z $fname ]
then
# do something
fi
Read the man page for test.
ignore my post about -s. I misunderstood what you were trying to do. there is no need to have a $fname variable if your just testing for a match.
if grep -q -w "^$index" $HOME/UnixCw/backup/Path.txt
then
#do something
fi
if [ -z $variable ]
This will check whether the $variable is Null.
You could also do:
grep -q -w "^$index" $HOME/UnixCw/backup/Path.txt
if [ ! $? = "0" ]
then
#dowhatever
fi
If your grep finds the string the exit status will be 0 and you can branch based upon that, or the reverse.
you can also use grep's exit status $? to check if there is a pattern or not. if there is a pattern exit value is 0, otherwise 1.
mo@mo-laptop:~$ ls | grep scripts
scripts
mo@mo-laptop:~$ echo $?
0
mo@mo-laptop:~$ ls | grep nofilehere
mo@mo-laptop:~$ echo $?
1
just put it in an if-then statement.
in bash:
grep something
if [ $? == 1 ]; then
do something
fi
---------- Post updated at 11:35 PM ---------- Previous update was at 11:34 PM ----------
oops, someone beat me to it