if [[ ${account_nr} =~ ^[0-9]+$ && ${from_account_nr} =~ ^[0-9]+$ ]]
This is intended to check whether the account number is numeric or not. I'm getting a syntax error.
An earlier version of this question lacked the space between if
and [[
; the actual code has the required space.
It showing the below error message:
syntax error: `${account_nr}' missing expression operator
I am using bash shell. You people are telling it is working. but I am trying with example like below it is giving error.
jai = "CNM"
hanuman = "BRK"
if [[ $jai =~ ^[0-9]+$ && $hanuman =~ ^[0-9]+$ ]]
then
echo "Jai hanuman"
echo "valid input"
fi
it showing the error like below.
./temp.sh: line 11: jai: command not found
./temp.sh: line 12: hanuman: command not found
In my actual program it is not working: " Now I am givig the problem indetail:"
- In one file I stored all the transactions details, each line for one transaction.
- by using the below while loop each time I am reading one transaction detail while read blank srvrtid blank member_nr blank account_nr blank acct_type blank routing_nr blank amount blank proc_date blank from_account_nr blank from_acct_type blank do
Inside this I want to check account_nr and from_account_nr values are numeric or not the if condition I have given the below
if [[ ${SQL_STATEMENT} -gt 0 && ${account_nr} =~ ^[0-9]+$ && ${from_account_nr} =~ ^[0-9]+$ ]]
It is not showing any error for
&{SQL_STATEMENT}
, thisSQL_STATEMENT
is assigned with the value returned by the SELECT query.(total number of transactions).when I run the script it is showing the below error.
syntax error: `${account_nr}' missing expression operator.
please help for my problem.
This line is correct with advanced shells like Bash, Zsh or Ksh.
It won't work with POSIX shells but -still- it won't show syntax error. Instead it would show that the command
[[
was not found. Other causes may be related to theif
statement itself but you have to show us what message was exactly shown e.g.bash: syntax error near unexpected token `fi'
A space is required between
if
and[
:Also, this works fine in
bash
:You cannot have spaces around your shell variable assignments, either. Below is a correction to your latest version:
Since neither
jai
orhanuman
are numbers, the above script runs and outputs nothing. If you set them both to a number, then it will display:Note that if you put a space, like so:
Then the shell (
bash
) thinks you are executing a command calledjai
and you get the error indicated.