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

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.

See this.

[/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]$ 
1 Like

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 ?

[/tmp]$ ./try.ksh 12 ' ' '  '
$1=[12]
$2=[ ]
$3=[  ]
[/tmp]$ 

yeah exactly

when we give empty paramters as

sampleshellscript archive ' ' ' ' ;

Fine. So what is it that you are facing ?

if [[ "${option1}" == "purge" && ${option_count} -eq 3 && -n "${option2}" && -n "${option3}" ]]
then

echo " in the purge if block"

else

echo " in the else part of purge since parametrs are empty"

when there are 3 parameters and 2 of them are empty then it needs to go the else part and display the echo message.But it shows the scho message in the first if block.

The condition is not getting statisfied.

Can't you just rely on option_count and forget the quote, i mean
just sampleshellscript archive instead of sampleshellscript archive ' ' ' ' ;
Isnt that the simplest way ?

i need to call a Oracle stored procedure from shell script.

So if i know there are 3 parameters that are to be passed i want to make

sure that these are not empty.based on count i can only know there are 3

parameters but how can i make sure they are not empty.

hummmm... how about this one

purge_block()
{
echo "I'm purging ..."
}
[[ $# -eq 3 && -n $1 && -n $2 && -n $3 ]] && purge_block && exit

echo here goes else where ...

the above conditon is for null values but it does not work for empty strigs

like ' ' i.e single qote start and then ten spaces then end single quote.