I am beginner to the Unix scripting, i am writing a script to call the DB through Unix script. I have to read the files from the given path and in each file the first column i have to pick up and delete the row in the table based on that column.
#!/bin/sh
set -x
# Check to see if source file path is given
if [ -z "$1" ]
then
echo "Usage: Source Path is required in position 1"
exit 1
else
SRCFILEPATH=$1
echo "Source path: $SRCFILEPATH"
fi
# Check to see if the file name pattern is given
if [ -z "$2" ]
then
echo "Usage: File Name Pattern is required in position 2"
exit 1
else
FILENAMEPAT=$2
echo "File Name Pattern: $FILENAMEPAT"
fi
#Moving to src directory
cd $SRCFILEPATH
pwd
FILECOUNT=`ls "$FILENAMEPAT"*|wc -l`
echo "$FILECOUNT"
if [ "$FILECOUNT" -ge 1 ]
then
for file in "$FILENAMEPAT"*;
do
RECCOUNT=`cat ${file}| wc -l`
echo "$RECCOUNT"
if [ "$RECCOUNT" -gt 1 ]
then
#!/bin/sh
user="GLOBALDATA"
pass="duVuqi50"
Search_Term = $(cut -d'|' -f1 ${file}|sed 1d)
sqlplus -S $user/$pass <<EOF
DELETE FROM SEARCH_TERM WHERE SEARCH_TERM_TX=$Search_Term;
exit;
EOF
else
echo "file has no records to process"
fi
done
else
echo "No files exists with pattern "$FILENAMEPAT" in the path to process"
exit 1
fi
#End Of Script
I am getting below error and correct me if the script is wrong
: invalid optionte.sh: line 12: set: -
set: usage: set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
'earch_Term_Delete.sh: line 38: syntax error near unexpected token `
'earch_Term_Delete.sh: line 38: ` for file in "$FILENAMEPAT"*;
Without seeing how far it gets, it will be difficult to spot the problem.
Can you confirm if the parameters contain any embedded spaces or other unusual characters?
I also notice that your quoting might be a little awry, but I don't think you've got that far yet.
What else is in the current directory when you run this? It might be that a wild-card character is getting misinterpreted because of misplaced quoting.
I was also expecting the same out put as you quoted, but i have posted the whole output. Tht's where i was also confused. Could you please correct the script for others.
Thanks RudiC!!
I have modified the script little bit, now its failing in the DB connection part.I am able to connect to the DB through UNIX when i run the only DB script part(in another script) but i am unable to execute that in the entire script,Please look into the code and correct me
#!/bin/sh
set -x
# Check to see if source file path is given
if [ -z "$1" ]
then
echo "Usage: Source Path is required in position 1"
exit 1
else
SRCFILEPATH=$1
echo "Source path: $SRCFILEPATH"
fi
# Check to see if the file name pattern is given
if [ -z "$2" ]
then
echo "Usage: File Name Pattern is required in position 2"
exit 1
else
FILENAMEPAT=$2
echo "File Name Pattern: $FILENAMEPAT"
fi
#Moving to src directory
cd $SRCFILEPATH
pwd
FILECOUNT=`ls *"$FILENAMEPAT"*|wc -l`
echo $FILECOUNT
for file in "$FILENAMEPAT"*;
do
if [ "$FILECOUNT" -gt 1 ]
then
user="GLOBALDATA"
pass="duVuqi50"
ServiceName="xxxx"
sqlplus -S $user/$pass@ServiceName <<_EOF_
SELECT * FROM SEARCH_TERM ;
exit;
_EOF_
else
echo "file has no records to process"
exit 1
fi
done
#End of Script
Error I am getting
etluser@avocado1:/var/local/ei/etl/Scripts>sh test_2.sh var/local/ei/etl/Scripts Search_Term
+ '[' -z var/local/ei/etl/Scripts ']'
+ SRCFILEPATH=var/local/ei/etl/Scripts
+ echo 'Source path: var/local/ei/etl/Scripts'
Source path: var/local/ei/etl/Scripts
+ '[' -z Search_Term ']'
+ FILENAMEPAT=Search_Term
+ echo 'File Name Pattern: Search_Term'
File Name Pattern: Search_Term
+ cd var/local/ei/etl/Scripts
test_2.sh: line 32: cd: var/local/ei/etl/Scripts: No such file or directory
+ pwd
/var/local/ei/etl/Scripts
++ wc -l
++ ls Search_Term_Delete.sh Search_Term_Manual.txt
+ FILECOUNT=2
+ echo 2
2
test_2.sh: line 52: warning: here-document at line 43 delimited by end-of-file (wanted `_EOF_')
test_2.sh: line 53: syntax error: unexpected end of file
Now this is a totally different situation - the errors mentioned in post#1 have disappeared. Is this by chance or did you correct / modify anything?
The error shown occurs because in the here document as used the shell is missing the delimiter which it expects at the beginning of the line. To deploy an indented delimiter, c.f. man bash :
If your script is working, then you can ignore this. However, shellcheck run across your script (on file z4) produces:
$ shellcheck z4
In z4 line 22:
cd $SRCFILEPATH
^-- SC2086: Double quote to prevent globbing and word splitting.
In z4 line 24:
FILECOUNT=`ls *"$FILENAMEPAT"*|wc -l`
^-- SC2006: Use $(..) instead of deprecated `..`
^-- SC2012: Use find instead of ls to better handle non-alphanumeric filenames.
^-- SC2035: Use ./*${VAR}* so names with dashes won't become options.
In z4 line 25:
echo $FILECOUNT
^-- SC2086: Double quote to prevent globbing and word splitting.
In z4 line 26:
for file in "$FILENAMEPAT"*;
^-- SC2034: file appears unused. Verify it or export it.
In z4 line 32:
ServiceName="xxxx"
^-- SC2034: ServiceName appears unused. Verify it or export it.
In z4 line 36:
_EOF_
^-- SC1039: Use <<- instead of << if you want to indent the end token.