Solved

How to test for non existence of a string in ksh

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pauing printer deamon in AIX 10 57
LastLogonTimeStamp Attribute 7 48
Searching File Names and Eliminating Duplicates in Linux 4 64
unix example issues 18 75
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now