Solved

How to test for non existence of a string in ksh

Posted on 2014-09-25
2
307 Views
Last Modified: 2014-09-25
HI, I have written a shell script that has the following line in it:

if [ `grep ${HOST} ${HOME_DIR}/.dump_locations.txt` ]
then
......A
else
.....B
fi

Basically I am trying to find the value of HOST in a text file, and then either do A or B. This works perfectly.

However, if I try for a non existence test it gives me "argument expected":

i.e. if I do:

if [ ! `grep ${HOST} ${HOME_DIR}/.dump_locations.txt` ]
then
......B
else
.......A
fi

I get the error:

ksh: test: argument expected

I tried using -z to see if the string value of $HOST is 0, but it still gives me the same error.
0
Comment
Question by:BadHatHarry
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40344477
If "grep" doesn't find anything ksh will just see "if [ ! ]" which is syntactically wrong.

Omit the square brackets and the backticks, so the test will be against the return code instead of the (found or not found) string:

if ! grep -q ${HOST} ${HOME_DIR}/.dump_locations.txt
...

"-q" instructs "grep" to suppress all output and to just set the return code.

If you insist in testing for strings you can well use the "-z" test:

if [[ -z `grep ${HOST} ${HOME_DIR}/.dump_locations.txt` ]]
...


"-z" returns a "true" value if the lenght of the following string is zero.

Please note the double square brackets!

That's the reason why "-z" didn't work for you. Use either double brackets or enclose the command substitution part in double quotes:

if [ -z "`grep ${HOST} ${HOME_DIR}/.dump_locations.txt`" ]
...
0
 

Author Closing Comment

by:BadHatHarry
ID: 40344606
Both solutions worked great.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now