Solved

How to test for non existence of a string in ksh

Posted on 2014-09-25
2
317 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

809 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