如何让 shell 脚本永远循环执行多个 if 语句?

如何让 shell 脚本永远循环执行多个 if 语句?

我根据我读过的一堆代码片段和教程编写了一个 shell 脚本。我就是搞不清楚如何修复语法错误并让它正常工作。终端输出为:

./afkscript.sh: 2 ./afkscript.sh: Syntax error: "(" unexpected ( expecting "done")

从我读到的教程来看,这些括号是必需的。这是我的脚本(替换了窗口名称):

while true 
do
sleep $[ ( $RANDOM % 10 ) + 1 ]s
DIFF=$((46-1+1))
R=$ ( ( $ ( ($RANDOM%DIFF) ) +1) )
if [[ "R" -eq 1 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "a"
elif [[ "R" -eq 2 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type ""
elif [[ "R" -eq 3 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "d"
elif [[ "R" -eq 4 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "f"
elif [[ "R" -eq 5 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "g"
elif [[ "R" -eq 6 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "h"
elif [[ "R" -eq 7 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "j"
elif [[ "R" -eq 8 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "k"
elif [[ "R" -eq 9 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "l"
elif [[ "R" -eq 10 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type ";"
elif [[ "R" -eq 11 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "'"
elif [[ "R" -eq 12 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "\"
elif [[ "R" -eq 13 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "q"
elif [[ "R" -eq 14 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "w"
elif [[ "R" -eq 15 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "e"
elif [[ "R" -eq 16 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "r"
elif [[ "R" -eq 17 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "t"
elif [[ "R" -eq 18 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "y"
elif [[ "R" -eq 19 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "u"
elif [[ "R" -eq 20 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "i"
elif [[ "R" -eq 21 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "o"
elif [[ "R" -eq 22 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "p"
elif [[ "R" -eq 23 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "["
elif [[ "R" -eq 24 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "]"
elif [[ "R" -eq 25 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "1"
elif [[ "R" -eq 26 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "2"
elif [[ "R" -eq 27 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "3"
elif [[ "R" -eq 28 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "4"
elif [[ "R" -eq 29 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "5"
elif [[ "R" -eq 30 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "6"
elif [[ "R" -eq 31 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "7"
elif [[ "R" -eq 32 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "8"
elif [[ "R" -eq 33 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "9"
elif [[ "R" -eq 34 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "0"
elif [[ "R" -eq 35 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "-"
elif [[ "R" -eq 36 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "="
elif [[ "R" -eq 37 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "z"
elif [[ "R" -eq 38 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "x"
elif [[ "R" -eq 39 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "c"
elif [[ "R" -eq 40 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "v"
elif [[ "R" -eq 41 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "b"
elif [[ "R" -eq 42 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "n"
elif [[ "R" -eq 43 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "m"
elif [[ "R" -eq 44 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type ","
elif [[ "R" -eq 45 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "."
elif [[ "R" -eq 46 ]]; then
xdotool search --name "WINDOWNAME" windowactivate --sync
sleep .05
xdotool type "/"
else
break
fi
done

答案1

仅根据您的标题,无限if语句只是while经过相应调整的逻辑。

此外,您的代码非常混乱,您迫切需要使用一些格式。

除此之外你还应该读懂你的错误。它清楚地表明您在某处有一个多余的括号,因此请仔细查看您的代码并进行实验以找到解决方案。为了真正提供答案,我现在会告诉您,您需要将“$”移到括号开始之前。这就是变量的工作方式。

最后,确保您的标题与您以后的问题相关。

相关内容