Syntax error checking whether account number is nu

2019-03-07 03:09发布

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:"

  1. In one file I stored all the transactions details, each line for one transaction.
  2. 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
  3. 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]+$ ]]

  4. It is not showing any error for &{SQL_STATEMENT}, this SQL_STATEMENT is assigned with the value returned by the SELECT query.(total number of transactions).

  5. when I run the script it is showing the below error.

    syntax error: `${account_nr}' missing expression operator.

please help for my problem.

标签: shell
2条回答
倾城 Initia
2楼-- · 2019-03-07 03:48

This line is correct with advanced shells like Bash, Zsh or Ksh.

if [[ ${account_nr} =~ ^[0-9]+$ &&  ${from_account_nr} =~ ^[0-9]+$ ]]

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 the if statement itself but you have to show us what message was exactly shown e.g. bash: syntax error near unexpected token `fi'

查看更多
可以哭但决不认输i
3楼-- · 2019-03-07 03:51

A space is required between if and [:

$ account_num=1234
$ if [[ ${accoun_num} =~ ^[0-9]+$ ]] ; then echo "foo" ; fi
foo
$

Also, this works fine in bash:

$ account_nr=1234
$ from_account_nr=9876
$ if [[ ${account_nr} =~ ^[0-9]+$ && ${from_account_nr} =~ ^[0-9]+$ ]] ; then echo "foo" ; fi
foo
$

You cannot have spaces around your shell variable assignments, either. Below is a correction to your latest version:

jai="CNM"
hanuman="BRK"
if [[ $jai =~ ^[0-9]+$ && $hanuman =~ ^[0-9]+$  ]]
then
  echo "Jai hanuman"
  echo "valid input"
fi

Since neither jai or hanuman are numbers, the above script runs and outputs nothing. If you set them both to a number, then it will display:

Jai hanuman
valid input

Note that if you put a space, like so:

jai = "CNM"

Then the shell (bash) thinks you are executing a command called jai and you get the error indicated.

查看更多
登录 后发表回答