Validating $1 and $2 before using

Hi there,

I'm trying to validate my $1 and $2 before I use them in the sqlplus update, I think I have the validation rules set correctly??? however when I test my script, it jumps straight into the sql. It doesn't seem to validate the vars....even when I know they are incorrect.

if [ $1 = '44*' ]; then
TEMP='echo $1 | cut -c2-'
MSISDN=0$TEMP
else
$1=MSISDN
fi

if [ $2 -ne '4[0-9]\{7,7\}$' ];then
echo "Please check $2 is a Cust_ref number'
else
$2=CUSTREF
fi

sqlplus -s ########/########## <<!
update tbl_entry set status='LOCKED' where msisdn='$MSISDN' and cust_ref='$CUSTREF' .......rest of sql;
commit;
exit;
!

Many thanks,

Should these lines of code:

$1=MSISDN
$2=CUSTREF

really be:

MSISDN=$1
CUSTREF=$2

?

Do you want $1 = '44*' or $1 -eq "44*"? Remember, single quotes mean the asterisk will be interpreted as an asterisk instead of a wildcard meaning any number of characters.

And I agree with mbb. At the very least, you can't assign values to $1, $2, etc...

I may be wrong but I don't think

if [ $1 = 44* ]; then

Will work with wildcards

You may need to use the ksh version

[[ $1 = 44* ]] && ....

Matt.

For troubleshooting purposes I would add several echo statements... in your script...so you can see the values of your variables as they are checked and assigned.

Im assuming $1 and $2 have values before this portion of your script?

Also, You assign CUSTREF to $2 but never assign a value to CUSTREF ???

ie:

echo $MSISDN and echo $CUSTREF both before during and after your IF/FI statements... so you can see what they are set to...

also put in a few "Sleep" statements so that it will run slower so you can read what is happening.

if [ $1 = '44*' ]; then
TEMP='echo $1 | cut -c2-'
MSISDN=$TEMP
echo $MSISDN
else
$1=MSISDN
echo $1
fi

sleep 10

if [ $2 -ne '4[0-9]\{7,7\}$' ];then
echo "Please check $2 is a Cust_ref number'
echo $CUSTREF
else
$2=CUSTREF
echo $2
fi

sleep 10
echo "Starting SQL portion"
sleep 10

sqlplus -s ########/########## <<!
update tbl_entry set status='LOCKED' where msisdn='$MSISDN' and cust_ref='$CUSTREF' .......rest of sql;
commit;
exit;
!

Thanks for the replies.....My first problem, where I though the script was going starig into the slqplus was that the orignal script had been set in the PATH and that was executing before mine which is being updated.

I'm still having problems with the string validations though. When I try and run the script with two very wrong values I et syntax errors.....please help!!!!

./manually_lock_port_in_entry.sh 441111111111111111111111 412345678
./manually_lock_port_in_entry.sh[29]: 07[0-9]\{9,9\}: syntax error
./manually_lock_port_in_entry.sh[37]: 4[0-9]\{7,7\}: syntax error
441111111111111111111111 412345678

# Validate Script options, MSISDN and CUST_REF

if [ $1 = 44* ]
then
TEMP='echo $1 | cut -c2-'
MSISDN=0$TEMP
else
if [ $1 -ne "07[0-9]\{9,9\}" ]
then
echo "Please check MSISDN is valid"
else
MSISDN=$1
fi
fi

if [ $2 -ne "4[0-9]\{7,7\}" ];then
echo "Please check $2 is a Cust_ref number"
else
CUSTREF=$2
fi

echo $MSISDN $CUSTREF

Hi nhatch,

The syntax errors are caused by the -ne being used with a value in quotes.

-ne shoudl be used with numbers.
If you are comping strings use !=

e.g.

if [ $1 != "07[0-9]\{9,9\}" ]

Hope this helps,

Matt.