LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   syntax error in ksh script (https://www.linuxquestions.org/questions/linux-newbie-8/syntax-error-in-ksh-script-899117/)

barkha_st 08-24-2011 03:13 AM

syntax error in ksh script
 
Hi all,

I am new to shell scripting and currently stuck at one place and not able to execute my script. Plz help me out

below is my script

Code:

#/usr/bin/ksh
# Test user parameters specified

set -a                # Export all variables upon declaration

export ORACLE_HOME=/opt/oracle/10.2.0
export PATH=$PATH:$ORACLE_HOME/bin


DAT=`date '+%h%d'`
RUNTMS=$(date +'%Y%m%d_%H%M%S')
Day=$1
SYSDAY=`date '+%m%d'`
Environment_Variable=$2

LOGFILE=/opt/GSC82/Logs/MissingFile.log
SQLFILE=/opt/GSC82/Reports/MissingFile.sql
RAWFILE=/opt/GSC82/Reports/MissingFile.raw
OUTFILE=/opt/GSC82/Reports/MissingFile_$RUNTMS.csv

rm $RAWFILE $SQLFILE $OUTFILE 2>/dev/null

#echo "Enter the Date for which missing files should be retrieved in following format:MMDD"
#read Day

if[[ $Day != 0000 ]]; then
ActualDay=${Day}
if

# Display settings as a reassurance check

echo '***************************************************************************** '  >>$LOGFILE 2>&1
echo '***************************************************************************** '  >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1
date >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1
echo ' Started the  MissingFile Report process '$DAT  $RUNTMS >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1

# Call SQL Callout script to populate the output file

/opt/GSC82/Scripts/MissingFileSQL.ksh $ActualDay $2 $RAWFILE > /dev/null &


echo '***************************************************************************** '  >>$LOGFILE 2>&1
echo '***************************************************************************** '  >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1
echo ' Completed the  MissingFile Report process '$DAT  $RUNTMS >>$LOGFILE 2>&1
echo ' '  >>$LOGFILE 2>&1
echo '***************************************************************************** '  >>$LOGFILE 2>&1

wait

echo "set colsep ," > $SQLFILE
echo "set echo off" >> $SQLFILE
echo "set feedback off" >> $SQLFILE
echo "set pagesize 0" >> $SQLFILE
echo "set linesize 10000" >> $SQLFILE
echo "set sqlprompt ''" >> $SQLFILE
echo "set trimspool on" >> $SQLFILE
echo "set scan off" >> $SQLFILE
echo "" >> $SQLFILE
echo "column FILE CODE format a30" >> $SQLFILE
echo "column CONTENT format a30" >> $SQLFILE
echo "column REGION format a30" >> $SQLFILE
echo "column VENDOR format a30" >> $SQLFILE
echo "" >> $SQLFILE
echo "spool $(echo $RAWFILE)" >> $SQLFILE
echo "" >> $SQLFILE


#cat $RAWFILE|sed -e '/^SQL/d' -e "/,'','','',/d" | sed -e "s/ ,'/ ' ','/g" | sed -e 's/\\//g' >> $SQLFILE

#works cat $RAWFILE|sed -e '/^SQL/d' -e "/,'','','',/d" | sed -e "s/'',,'ISSUE'/' ',' ','ISSUE'/g" | sed -e 's/\\//g' >> $SQLFILE

cat $RAWFILE|sed -e '/^SQL/d' -e "/,'','','',/d" | sed -e "s/'',,'ISSUE'/' ',' ','ISSUE'/g" | sed -e 's/\\//g' | sed -e "s/replace(,',',';')/' '/g" >> $SQLFILE

echo "" >> $SQLFILE
echo "spool off" >> $SQLFILE
echo "" >> $SQLFILE
echo "ENDOFSQL" >> $SQLFILE



rm $RAWFILE 2>/dev/null


chmod 755 $SQLFILE

sqlplus $2 @$SQLFILE > /dev/null &
echo $sqlplus
wait

#echo "FILE CODE                  ,CONTENT                  ,REGION                  ,VENDOR                  ," > $OUTFILE
echo "FILE CODE                  ,CONTENT                  ,REGION                  ,VENDOR                  ," > $OUTFILE
cat $RAWFILE >> $OUTFILE
echo "EOF" >> $OUTFILE
echo "EXIT" >> $OUTFILE


but after executing above script I am getting error ""

./startMissingFile.ksh: line 52: syntax error near unexpected token `then'
./startMissingFile.ksh: line 52: `if[[ $Day != 0000 ]]; then'

Plzzzz help me

Thanks,
Barkha

colucix 08-24-2011 03:21 AM

You missed a space between the word if and the square brackets:
Code:

if [[ $Day != 0000 ]]
the shell is very strict about spacing: you need spaces around all the elements of the conditional statements.

barkha_st 08-24-2011 03:31 AM

thanks Colucix

I have added space between if and square brackets.
But now getting another error as below
./startMissingFile.ksh: line 126: syntax error: unexpected end of file

Plz let me know now what is going wrong

colucix 08-24-2011 03:35 AM

You put an extra if two lines after the previous one. Furthermore there is no fi anywhere to close the conditional statement, hence the unexpected end of file.

barkha_st 08-24-2011 03:39 AM

thank you so much Colucix
It worked :)

MTK358 08-24-2011 08:19 AM

Quote:

Originally Posted by colucix (Post 4451867)
You missed a space between the word if and the square brackets:
Code:

if [[ $Day != 0000 ]]
the shell is very strict about spacing: you need spaces around all the elements of the conditional statements.

That's because they are commands, not some kind of special expression syntax. So they need spaces between arguments just like any other command.

colucix 08-24-2011 10:52 AM

Quote:

Originally Posted by MTK358 (Post 4452092)
That's because they are commands, not some kind of special expression syntax. So they need spaces between arguments just like any other command.

That's not really true. Actually the if and the double square brackets [[ are shell keywords. You probably refer to the single square bracket [ which is either a shell built-in and an external command (provided by coreutils). Anyway, command or keywords doesn't make so much difference: they all require to be separated by space to let the shell correctly parse them.

Instead, one of the more common mistakes in an if statement is the misuse of the relational operator. If you don't put spaces around the == sign, the shell interprets the whole expression as a single string and it evaluates always as true:
Code:

$ a=2    # note: no spaces in assignments
$ [[ $a == 2 ]] && echo yes
yes
$ [[ $a==2 ]] && echo yes
yes
$ [[ $a==3 ]] && echo yes
yes
$ [[ $a == 3 ]] && echo yes
$

Therefore the usage of spacing in bash requires careful attention.

MTK358 08-24-2011 01:02 PM

Quote:

Originally Posted by colucix (Post 4452207)
That's not really true. Actually the if and the double square brackets [[ are shell keywords.

I know.

David the H. 08-24-2011 01:13 PM

Instead of simply asking others to proofread your script, try to understand what the shell is already telling you:

"syntax error"

This means that you didn't type a command properly. And that means that you have to go back through the script and find the incorrect command. Usually it will give you a line number or other hint as to where to look. Check the mentioned location carefully.

In addition, when it also says "unexpected end of file", that generally means you're missing a closing argument of some kind, a quotation mark, an "fi", or similar. It reached the the end of the script without finding the closing character or string.

Unfortunately, the given line numbers in EOF errors may not match the actual location of the mistake. The script will pair up whatever quotation marks it finds, for example, and will only give an error when the last unpaired quotation mark is found. Unmatched quotes can be some of the hardest errors to find.

Now if you've gone though you script with a fine-toothed comb and still can't spot the error, then it's ok to ask others to help you. But you should at least make some effort to debug it yourself first.

chrism01 08-24-2011 09:04 PM

One very useful cmd to help debug (if you can't work it out) is
Code:

set -xv
as the 2nd line. This shows you exactly what the script is doing, including before and after var translations.
http://ss64.com/bash/set.html

barkha_st 08-25-2011 01:03 AM

Hi Guys,

I again need ur help.
This time I need to send multiple values as paramater. how can I achieve this.
eg: In my shell script I accept Region parameter as below

if [[ $Region != 0 ]]; then
ActualRegion=$Region
else
ActualRegion='1','2','3'
fi

Now when the user send Region = 0 in this case I want ActualRegion to be set as '1','2','3'

which then I would use in my sql as
select * from vendor_table where region in ($ActualRegion)
which should be replaced by parameter as below
Select * from vendor_table where region in ('1','2','3')

Thanks in advance
Barkha

colucix 08-25-2011 01:57 AM

As you should already know, the single quotes are special characters used by the shell to protect strings from shell expansion. If you want to protect the single quote itself, you can either enclose it in double quotes or escape it using bacslashes. In the first case your assignment would be:
Code:

ActualRegion="'1','2','3'"
Use always double quotes when you evaluate the variable:
Code:

select * from vendor_table where region in ("$ActualRegion")
Is this what you're looking for?

barkha_st 08-25-2011 03:24 AM

yes thanks it worked :)


All times are GMT -5. The time now is 02:54 AM.