The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking the Empty line in csv file sollins Shell Programming and Scripting 4 05-15-2008 04:48 AM
Checking for the presence of a string within another string hidnana Shell Programming and Scripting 1 03-20-2008 02:38 AM
Check for empty string rahman_riyaz Shell Programming and Scripting 12 01-23-2008 11:13 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-23-2007 10:31 PM
Checking if string contains integer haz Shell Programming and Scripting 7 09-10-2006 11:51 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007
Registered User
 

Join Date: Dec 2007
Posts: 2
checking for empty string in tcsh

Hi,

i'm writing a script in tcsh, and i need to check to see whether a string is empty. i think sh users use the -z function for this, but i'm using it already to check for the emptiness of a file -- surely it's not the same function for both, is it?

is there another one to check for string emptiness, or am i wrong in using it to check for file emptiness?

here a short sniplet of what i've written (which of course doesn't work):

Code:
echo "checking length of nmlfilelist"
if( $nmlfilelist == "" ) then
    echo "nmllist is empty"
    exit( 1 )
endif

this raises a syntax error, which i can retrospectively understand. how should it look like?

thx in advance,

Pagod
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-06-2007
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 880
Your code snippet works for me. If you wish to use some other means of testing for an empty string you can always using the test utility. Man test(1) for more information.

Code:
set nmlfilelist

[ -z $nmlfilelist ] && echo "nmllist is empty"; exit (1)
Reply With Quote
  #3 (permalink)  
Old 12-06-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 496
Hi.

Using other csh / tcsh constructs:
Code:
#!/bin/tcsh

# @(#) s1       Demonstrate csh variable contents test.

echo "checking length of name"
if ( ${?name} == 0 ) then
        echo " Variable name is not set."
endif

#  NOTE - $ % construct tcsh only.

set name
if ( ${%name} == 0 ) then
        echo " Variable name has 0 characters as value."
endif

# if( "$nmlfilelist" == "" ) then
if( "${?nmlfilelist}" == 0 ) then
    echo "nmllist is empty"
    exit( 1 )
endif

exit 0
Produces:
Code:
% ./s1
checking length of name
 Variable name is not set.
 Variable name has 0 characters as value.
nmllist is empty
Best wishes ... cheers, drl

_____
Standard advice: avoid csh family for scripting.
Reply With Quote
  #4 (permalink)  
Old 12-07-2007
Registered User
 

Join Date: Dec 2007
Posts: 2
thx, works great now! :-)
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0