My Korn shell script below is giving me the following error: ./test.ksh[6]: 0403-057 syntax error at line 7 : 'then' is not matched.
Can anyone provide a quick solution as to why the error is occurring? Thanks.
#!/usr/bin/ksh
typeset -i RecCount
typeset -i RecCount2
RecCount=`db2 -x "select count() from schema.table"`
RecCount2='db2 -x "select count( ) from schema.table2"`
if [[ ${RecCount} -ne 0 && ${RecCount2} -ne 0 ]]
then
print $RecCount
print $RecCount2
fi
era
April 7, 2008, 3:14pm
2
You can't have && inside [[...]]. I believe -a is what you are looking for.
(Caveat: bash bigot here.)
Making the change you suggested resulted in the following error:
./test.ksh[6]: 0403-057 syntax error at line 6: '-a' is not expected.
Should not be the problem:
[Chapter 5] Flow Control
What is the output of the SQL statements?
Regards
In ksh you can have && inside of double brackets.
There might be non-printing characters embedded in your shell script. Run it thru od to see if there are any control characters in it.
od -c shell_script
I just ran my script through od -c shell_script, and did not see any control characters.
My two sql statements give me results of:3500000 and 5578000 respectively.
If you only want to check for the value 0 you can try a string test, remove the typeset commands and try this:
if [[ ${RecCount} == "0" && ${RecCount2} == "0" ]]
Regards
is the ' a typo? should be `instead...
Does "==" mean "not equal"?
no... it's more like this "!=". but read "man test" for better explanation...
After all the suggestions, my Korn shell script now looks like the following:
!#/usr/bin/ksh
RecCount=`db2 -x "select count() from schema.table"`
RecCount2=`db2 -x "select count( ) from schema.table2"`
if [ ${RecCount} == "0" && ${RecCount2} == "0" ]
then
print $RecCount
print $RecCount2
Sadly, I am still getting the following error message:
0403-057 Syntax error at line 5 : 'then' is not matched.
pupp
April 8, 2008, 6:54pm
13
you forgot the "fi" at the end.
After adding fi at the end, I now get the following error:
0403-21 A ] character is missing.
pupp
April 8, 2008, 7:21pm
15
!#/usr/bin/ksh
RecCount=`db2 -x "select count(*) from schema.table"`
RecCount2=`db2 -x "select count(*) from schema.table2"`
if [[ ${RecCount} == "0" && ${RecCount2} == "0" ]]
then
print $RecCount
print $RecCount2
fi
exit 0
pupp
April 8, 2008, 7:24pm
16
sorry... should have explained.
if [ ${RecCount} == "0" && ${RecCount2} == "0" ]
&& will not work in the [......] construct. therefore, you need [[ $condition1 && $condition2 ]]
if [ ${RecCount} == "0" -a ${RecCount2} == "0" ]
this will work.
My script almost works. Now, I'm not getting any errors, but the values for RecCount and RecCount2 are not printing. I tried running the following commands outside of the shell and got results of 3000000 and 5000000, so I know that the tables contain data:
>db2 -x "select count() from schema.table"
>db2 -x "select count( ) from schema.table2"
pupp
April 8, 2008, 8:34pm
18
ok i see what went wrong here.
!#/usr/bin/ksh
RecCount=`db2 -x "select count(*) from schema.table"`
RecCount2=`db2 -x "select count(*) from schema.table2"`
if [[ ${RecCount} != "0" && ${RecCount2} != "0" ]]
then
print $RecCount
print $RecCount2
fi
exit 0
you can also use echo instead of print. its up to you. however, it was not printing to screen because the condition was failing. it was doing what it was supposed to do.