Solved

How to test for non existence of a string in ksh

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

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 …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

615 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