Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to test for non existence of a string in ksh

Posted on 2014-09-25
2
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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.:
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.

722 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