gpt4 book ai didi

linux - 算术表达式 : expecting primary:

转载 作者:IT王子 更新时间:2023-10-29 01:11:26 27 4
gpt4 key购买 nike

我正在尝试在 shell 中执行一个脚本,该脚本会随机休眠一段时间,然后调用 python 脚本。我这样做:

#!/bin/bash

now="$(date)"
printf "Current date and time %s\n" "$now"

maxdelay=25
delay=$(($RANDOM%maxdelay)) # pick an independent random delay for each of the 20 runs
echo $delay;
(sleep $((delay*60)); /usr/bin/python pythonscript.py) &

但是它失败了,这是结果:

Current date and time mar jun  9 00:02:10 CEST 2015
prueba.sh: 7: prueba.sh: arithmetic expression: expecting primary: "%maxdelay"

昨天还可以,今天就不知道为什么了

最佳答案

您似乎正在使用 dash 而不是 bash 运行该脚本,这可能是因为您正在调用脚本作为

sh prueba.sh

代替

# prueba.sh must have exec permissions
# the shebang line is used to select the interpreter
./prueba.sh

bash prueba.sh

RANDOM 是一个 bash 扩展;在 dash 中,它并不特殊,默认情况下不分配。

在算术表达式中,如果使用了$var 并且var 未赋值,那么它会被替换为一个空字符串,这通常会产生语法错误。另一方面,如果您使用 var 并且 var 没有被赋值,则假定为 0。

Debian 和 Ubuntu 安装通常使用 dash 作为 /bin/sh 默认 shell 解释器。

请注意 bashdash 会产生不同的错误消息:

$ bash -c 'unset foo;bar=25;echo $(($foo*$bar))'
bash: *25: syntax error: operand expected (error token is "*25")
$ dash -c 'unset foo;bar=25;echo $(($foo*$bar))'
dash: 1: arithmetic expression: expecting primary: "*25"

关于linux - 算术表达式 : expecting primary:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com