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
How to check for empty file in Perl? deepakwins UNIX for Dummies Questions & Answers 1 03-04-2008 12:00 PM
Check for Empty Command Argument Nysif Steve UNIX for Dummies Questions & Answers 6 09-19-2007 12:59 PM
check if file is empty stolz Shell Programming and Scripting 6 07-24-2007 12:24 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-23-2007 10:31 PM
How can I make the for command check to see if a file is empty before executing? chrchcol Shell Programming and Scripting 3 07-29-2006 12:14 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
Smile Check for empty string

Hello All,

I have written shell script whcih at the max 3 parameters.

When only one commandline argument and other two command line arguments are passed as empty string like

eg : archive ' ' ' '

Then i need to check whether the commandline arguments are empty or not.

i have use -n and ! -z but both of them fail for the above scenario.

Please let me know what is the alternative to solve this problem.

Thanks in anticipation
Reply With Quote
Forum Sponsor
  #2  
Old 01-23-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Look for the number of command line parameters. If 1, then consider the other 2 empty.
Reply With Quote
  #3  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
Quote:
Originally Posted by vino View Post
Look for the number of command line parameters. If 1, then consider the other 2 empty.
Actually i need to check with one paramter and 3 paramters .

if it is one paramter then it is ok no need to check for empty strings.

But if it is 3 parameters then i need to check whether the other two are

emty since i know that there are 3 paramters.
Reply With Quote
  #4  
Old 01-23-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
See this.

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh


if [[ -n "$1" ]] ; then
    echo '$1'="[$1]"
else
    echo '$1' is empty
fi
if [[ -n "$2" ]] ; then
    echo '$2'="[$2]"
else
    echo '$2' is empty
fi
if [[ -n "$2" ]] ; then
    echo '$3'="[$3]"
else
    echo '$3' is empty
fi
[/tmp]$ ./try.ksh
$1 is empty
$2 is empty
$3 is empty
[/tmp]$ ./try.ksh 12 " " "  "
$1=[12]
$2=[ ]
$3=[  ]
[/tmp]$
Reply With Quote
  #5  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
Quote:
Originally Posted by vino View Post
See this.

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh


if [[ -n "$1" ]] ; then
    echo '$1'="[$1]"
else
    echo '$1' is empty
fi
if [[ -n "$2" ]] ; then
    echo '$2'="[$2]"
else
    echo '$2' is empty
fi
if [[ -n "$2" ]] ; then
    echo '$3'="[$3]"
else
    echo '$3' is empty
fi
[/tmp]$ ./try.ksh
$1 is empty
$2 is empty
$3 is empty
[/tmp]$ ./try.ksh 12 " " "  "
$1=[12]
$2=[ ]
$3=[  ]
[/tmp]$

Hello vino,

thanks a lot for the quick reply.

The above code works if send the parameters as



sampleshellscript purge '' ''

i.e when i send null parameters.

but when i send parameters as

sampleshellscript purge ' ' ' ';

Then it does not work.

I wonder why ?that is why i was looking for an alternative to -n.
Reply With Quote
  #6  
Old 01-23-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by rahman_riyaz View Post
Hello vino,

thanks a lot for the quick reply.

The above code works if send the parameters as



sampleshellscript purge '' ''

i.e when i send null parameters.

but when i send parameters as

sampleshellscript purge ' ' ' ';

Then it does not work.

I wonder why ?that is why i was looking for an alternative to -n.
Do you mean like this ?

Code:
[/tmp]$ ./try.ksh 12 ' ' '  '
$1=[12]
$2=[ ]
$3=[  ]
[/tmp]$
Reply With Quote
  #7  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
Quote:
Originally Posted by vino View Post
Do you mean like this ?

Code:
[/tmp]$ ./try.ksh 12 ' ' '  '
$1=[12]
$2=[ ]
$3=[  ]
[/tmp]$
yeah exactly

when we give empty paramters as

sampleshellscript archive ' ' ' ' ;
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:51 AM.


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

Content Relevant URLs by vBSEO 3.2.0