Script throwing error “=: not found” [duplicate]
Script throwing error “=: not found” [duplicate]
This question already has an answer here:
Consider:
#!/bin/ksh
db2 connect to MKTETLPS user ....... using ........
db2 "select count(*) from etl.IDM_COLLAPSE_ORG_DEE c where c.IDM_PROCESS_STEP = 'I' and priority in ( '1','2','3','4','5') and c.update_ts < (current timestamp - 60 minutes) with ur" > l.txt
$a = /is115/idm/dsproj/scripts/l.txt
if [ $a -gt 0 ];
then
db2 "update etl.idm_collapse_org_dee
set idm_process_step = NULL where priority in (
'1','2','3','4','5')
and idm_process_step ='I'"
else
echo "All is well"
fi
I am running above the script and am receiving the below error. How can I fix it?
./CORCleanup1.sh[8]: =: not found.
./CORCleanup1.sh[10]: test: 0403-004 Specify a parameter with this command.
All is well
DB20000I The SQL command completed successfully.
DB20000I The TERMINATE command completed successfully.
db2 connect reset
db2 terminate
exit
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2 Answers
2
Variable assignments must not include $
and spaces around the =
. I also would double quote the assignment. So the variable assignment should look like as follows.
$
=
a="/is115/idm/dsproj/scripts/l.txt"
From further reading the script, it looks like you rather want to store the content of the file 1.txt
in $a
rather than the file path itself. For that purpose you could use the assignment as follows.
1.txt
$a
read -r a < /is115/idm/dsproj/scripts/l.txt
(read -r
reads the first line of the file, strips the leading and trailing spaces and tabs (assuming the default value of $IFS
) and stores it in the supplied variable)
read -r
$IFS
You also may want to double quote the $a
variable in the if
statement.
$a
if
if [ "$a" -gt 0 ];
You can also use https://www.shellcheck.net/ to check the syntax of your script.
a=$( db2 "select count(*) from tbl where cond and cond etc" )
Here's why you're seeing that error:
$a = /is115/idm/dsproj/scripts/l.txt
At this point in the code, the variable a
is unset. ksh will substitute the variable with the empty string, resulting in:
a
= /is115/idm/dsproj/scripts/l.txt
Then ksh attempts to execute the line, tries to locate the command =
, fails to find it, and produces the 1st error you see.
=
As @Thomas points out, the syntax for variable assignment is
varname=value
with no $
on the left-hand side, and no spaces around =
. https://www.shellcheck.net/ will point out these errors.
$
=
Then you have
if [ $a -gt 0 ];
Since a
has no value, ksh performs the substitution and tries to do
a
if [ -gt 0 ];
The [
command (yes, it is a command, aliased to the test
command) does not understand the ‑gt
operator without a left-hand operand, and you get the 2nd error message.
[
test
‑gt
The [
command exits with non-zero status, the if
statement then executes the else
block, and you get the "all is well" message.
[
if
else
This is why it's important to quote all variables within single brackets [ ... ]
[ ... ]
if [ "$a" -gt 0 ];
More generally, always quote variables unless you understand specifically when to omit the quotes. See also Security implications of forgetting to quote a variable in bash/POSIX shells
Or if the file was meant as a temporary,
a=$( db2 "select count(*) from tbl where cond and cond etc" )
-- though slightly less basic, command substitution is a very useful tool to learn– dave_thompson_085
Sep 2 at 1:23