LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-24-2011, 03:13 AM   #1
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Rep: Reputation: Disabled
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

Last edited by colucix; 08-24-2011 at 03:18 AM. Reason: Added CODE tags
 
Old 08-24-2011, 03:21 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
1 members found this post helpful.
Old 08-24-2011, 03:31 AM   #3
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-24-2011, 03:35 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 08-24-2011, 03:39 AM   #5
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
thank you so much Colucix
It worked
 
Old 08-24-2011, 08:19 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by colucix View Post
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.
 
Old 08-24-2011, 10:52 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 08-24-2011, 01:02 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by colucix View Post
That's not really true. Actually the if and the double square brackets [[ are shell keywords.
I know.
 
Old 08-24-2011, 01:13 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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.
 
1 members found this post helpful.
Old 08-24-2011, 09:04 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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
 
Old 08-25-2011, 01:03 AM   #11
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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

Last edited by barkha_st; 08-25-2011 at 01:04 AM.
 
Old 08-25-2011, 01:57 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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?

Last edited by colucix; 08-25-2011 at 01:58 AM.
 
1 members found this post helpful.
Old 08-25-2011, 03:24 AM   #13
barkha_st
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
yes thanks it worked
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KSH - Script error; invalid command - boyd98 Programming 4 07-19-2012 10:22 AM
ksh syntax error: `(' unexpected SentralOrigin Programming 5 11-10-2010 02:57 PM
[SOLVED] Script syntax error fw12 Programming 3 07-10-2010 01:31 AM
why am getting error ksh: syntax error: `fi' unexpected while running script deb4you Linux - Newbie 4 09-06-2008 08:37 AM
Porting ksh script from Solaris to Linux Gives unmatched if error markatharvestinfotec Programming 4 08-28-2008 01:51 PM


All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration