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

这旨在检查帐号是否为数字。我收到语法错误。

这个问题的早期版本缺少if[[;之间的空格。实际代码具有所需的空间。

它显示以下错误消息:

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

我正在使用 bash shell。你们说它有效。但我正在尝试使用下面的示例,它给出了错误。

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

它显示如下错误。

./temp.sh: line 11: jai: command not found
./temp.sh: line 12: hanuman: command not found

在我的实际程序中它不起作用:“现在我正在详细说明问题:”

  1. 在一个文件中,我存储了所有交易细节,每一行代表一笔交易。
  2. 通过使用下面的 while 循环,每次我正在阅读一个交易详细信息,而阅读空白 srvrtid 空白 member_nr 空白 account_nr 空白 acct_type 空白 routing_nr 空白金额 空白 proc_date 空白 from_account_nr 空白 from_acct_type 空白 do
  3. 在这里面我想检查 account_nr 和 from_account_nr 值是数字还是不是我在下面给出的 if 条件

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

  4. 它没有显示任何错误&{SQL_STATEMENT},这SQL_STATEMENT是使用 SELECT 查询返回的值分配的。(事务总数)。

  5. 当我运行脚本时,它显示以下错误。

    语法错误:“${account_nr}”缺少表达式运算符。

请帮助解决我的问题。

4

2 回答 2

3

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.

于 2013-09-14T18:46:37.103 回答
0

此行适用于 Bash、Zsh 或 Ksh 等高级 shell。

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

它不适用于 POSIX shell,但 - 仍然 - 它不会显示语法错误。相反,它会显示该命令[[未找到。其他原因可能与if声明本身有关,但您必须向我们展示确切显示的信息,例如bash: syntax error near unexpected token `fi'

于 2013-09-14T19:17:12.230 回答