我收到以下错误:
Path to the shell file:line 6: ++++: syntax error: operand expected (error token is "+")
和
Path to the shell file:line 13: ((: i <= : syntax error: operand expected (error token is "<= ")\
这是我的脚本:
#!/bin/bash
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
file="$SCRIPTPATH/android/sdcard.img"
file2="$SCRIPTPATH/android/devices.txt"
TOTALDEVICES=$(($1+$2+$3+$4+$5))
ANDROID4=0
ANDROID5=0
ANDROID5_1=0
ANDROID6=0
ANDROID7=0
echo $TOTALDEVICES
for ((i = 1; i <= $TOTALDEVICES; i++));
do
if (($1 > 0 && $ANDROID4 < $1))
then
echo "Device$i PACKAGE(avd4.4) 1"
ANDROID4=$((ANDROID4 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-19;google_apis;armeabi-v7a' --name "avd4" --tag 'google_apis' -p $SCRIPTPATH/android/avd4
fi
if (($2 > 0 && $ANDROID5 < $2 && $ANDROID4 == $1 && $i > $ANDROID4))
then
echo "Device$i PACKAGE(avd5.0) 2"
ANDROID5=$((ANDROID5 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-21;google_apis;armeabi-v7a' --name "avd5" --tag 'google_apis' -p $SCRIPTPATH/android/avd5
fi
if (($3 > 0 && $ANDROID5_1 < $3 && $ANDROID5 == $2 && $i > $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd5.1) 3"
ANDROID5_1=$((ANDROID5_1 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-22;google_apis;x86' --name "avd5.1" --tag 'google_apis' -p $SCRIPTPATH/android/avd5.1
fi
if (($4 > 0 && $ANDROID6 < $4 && $ANDROID5_1 == $3 && $i > $ANDROID5_1 + $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd6) 4"
ANDROID6=$((ANDROID6 + 1))
echo "no" | ~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-23;google_apis;x86' --name "avd6" --tag 'google_apis' -p $SCRIPTPATH/android/avd6
fi
if (($5 > 0 && $ANDROID7 < $5 && $ANDROID6 == $4 && $i > $ANDROID6 + $ANDROID5_1 + $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd7) 5"
ANDROID7=$((ANDROID7 + 1))
echo "no" | ~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-24;google_apis;x86' --name "avd7" --tag 'google_apis' -p $SCRIPTPATH/android/avd7
fi
done
AVDMANAGEROUTPUT=$(~/Android/Sdk/tools/bin/avdmanager list avds | grep "Name:")
AVDMANAGEROUTPUT=${AVDMANAGEROUTPUT//$'\n'/} # Remove all newlines.
AVDMANAGEROUTPUT=${AVDMANAGEROUTPUT%$'\n'} # Remove a trailing newline.
DEVICES=()
i=0
IFS=' ' read -r -a array <<< "$AVDMANAGEROUTPUT"
for index in "${!array[@]}"
do
rem=$(( $index % 2 )) #check for odd number to avoid Name:
if [ $rem -eq 1 ]
then
echo "${array[index]}" #Now put values into an array
DEVICES[$i]=`echo @"${array[index]}"`
i=$((i+1))
fi
done
# Check if the sdcard is available
if [ -f "$file" ]
then
echo "$file found."
~/Android/Sdk/emulator/emulator ${DEVICES[0]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[1]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[2]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[3]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[4]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
sleep 110
else
# if there is none,here we are creating one
echo "$file not found."
~/Android/Sdk/emulator/mksdcard 10G "$SCRIPTPATH/android/sdcard.img"
sleep 5
~/Android/Sdk/emulator/emulator ${DEVICES[0]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 &
~/Android/Sdk/emulator/emulator ${DEVICES[1]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[2]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[3]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[4]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
sleep 110
fi
答案1
脚本的第 6 行要求存在 1 到 5 个参数。如果运行脚本时不带任何参数,$1+$2+$3+$4+$5
则会出现+++++
错误信息。(第二条错误信息也有类似的解释。)也许您应该检查是否提供了 5 个参数,如果测试失败,则退出并显示错误信息,例如:
if [ $# != 5 ]
then
echo "Usage: scriptname num1 num2 num3 num4 num5"
exit 2
fi
或者,你可以使用以下方式为缺失的参数提供默认值:Bash 的参数扩展在以下表达式中,前 5 个命令行参数(若没有则为 0)被添加到总和中:
$((${1-0} + ${2-0} + ${3-0} + ${4-0} + ${5-0}))
请注意,在尝试获取它们的总和之前,您还应该检查 5 个参数中的每一个是否都是数字。
答案2
谢谢你的回答,你让我知道我做错了什么。
我如何修复它:
然后我运行 sh 文件,如“file.sh” 1 1 1 1 1,这样就可以运行模拟器了。1 决定了我想要运行每个模拟器的数量。