1833875 Members
1963 Online
110063 Solutions
New Discussion

Re: Script error

 
SOLVED
Go to solution
John Flanagan
Regular Advisor

Script error

Until
----
-----
read option
[ !-n $option ]
do
----
----
done

This works properly but gives the following error
iq.sh[23]: test: argument expected
If I hit enter the script is exited otherwise a serial number is entered and used in an sql script. The results are correct except for the error being displayed.

Redgards,

John.
8 REPLIES 8
T G Manikandan
Honored Contributor

Re: Script error

if [ !-n $option]
do
--
--
done
fi

OR

Jean-Luc Oudart
Honored Contributor

Re: Script error

I think option is not set in the 1st place.
Hence the error message.
May be the logic should be reviewed
(inititialise option with an "invalid" option value)

regards,
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: Script error

I checked the construction :
Until

The until construct is basically the same as the while construct except that the commands in the loop are executed until the conditions are true (instead of false like in the while loop). Here is the format:

until command-list1
do
command-list2
done

If the last command in command-list1 is unsuccessful, then the commands in command-list2 are executed. When the last command in command-list1 is successful, the until loop is terminated. Let's use the same operation in the while section to illustrate:

until [ ! -r $1 ]
do
cat $1 >> composite
done
Notice the subtle difference with the while loop. The ! negates the test conditions. We execute the loop until the condition is true (or successful). The while loop executes the commands while a condition is true (or successful).

http://docs.hp.com/hpux/onlinedocs/B2355-90046/B2355-90046.html

Regards,
Jean-Luc

fiat lux
Mark Grant
Honored Contributor
Solution

Re: Script error

You often get this kind of problem with test when the variable you think you have isn't set. And it won't be set if you just hit enter.

A classic though ugly solution would be to change the test to something like

[ "A"$option != "A" ]

Other than that, it might just be that you need a space between the ! and the "-n"
Never preceed any demonstration with anything more predictive than "watch this"
Jeroen Peereboom
Honored Contributor

Re: Script error

John,

you should test [ -n "$option" ].
This will result in testing for "" in stead of nothing.

Another hint:

put in the beginning of the script:
set -u # unset variable -> exit.

(This will cause problems in this specific case though).

JP.

Mark Grant
Honored Contributor

Re: Script error

The more I look at this, the more I'm sure that you just want that little space between the "!" and the "-n"
Never preceed any demonstration with anything more predictive than "watch this"
John Flanagan
Regular Advisor

Re: Script error

Changed the format of the test

[ "A"$option = "A" ]

This works.

Regards,

John.
Bill Hassell
Honored Contributor

Re: Script error

This is a common situation when testing for null options. If nothing was entered, then option is null and your statement:

[ ! -n $option ]

is really

[ ! -n ]

which will be the case if you trace the execution using set -x. To tell the shell that there really is something to look at (including nothing), use the " characters around $option as in "$option" which now makes a null entry look like this:

[ ! -n "" ]

and that is a valid test condition. Similarly, you could test something like this:

if [ "$option" -ne "" ]
then
...real option was entered
else
...nothing entered
fi

Note that option="" is different than undefined. The default for POSIX shells is typically that an undefined variable looks like a null variable, but this can have grave consequences in sysadmin scripts (like when a spelling error causes the removal of an entire directory). Most script writers will put: set -u at the start of every script and thus, any spelling errors will stop the script when an undefined variable is used.

There is even a way to handle unset variables when set -u is used:

set -u
UNSET=IamNOTset
DT=${DT:-$UNSET}
VUE=${VUE:-$UNSET}

if [ $VUE -ne $UNSET ]
then
...process code for VUE...
fi

The :- construct will leave the variable untouched if it had been previously defined but change it to whatever follows :- if it was never defined. If necessary (such as in profile scripts), return the variable back to it's original state:

[ $VUE = $UNSET ] && unset VUE

The text for UNSET is arbitrary, I just use IamNOTset since it is unlikely to be found in most variables.


Bill Hassell, sysadmin