我是shell编程的新手。我使用的是一个shell脚本,它从一个Raspberry 4编译并上传一个.ino (此处:tb_20200930.ino)文件到Controllino自动化(基于Arduino)。
#!/bin/bash
echo "compile"
arduino-cli compile -v --fqbn CONTROLLINO_Boards:avr:controllino_maxi_automation ./tb_20200930.ino
echo "workaraound a bug in arduino-cli"
rm -rf ./tb_20200930.CONTROLLINO_Boards.avr.controllino_maxi_automation.hex
cp ./tb_20200930.ino.CONTROLLINO_Boards.avr.controllino_maxi_automation.hex ./tb_20200930.CONTROLLINO_Boards.avr.controllino_maxi_automation.hex
echo "liberate the serial port for upload"
sudo systemctl stop testbench.service
echo "upload to the arduino"
arduino-cli upload -v -p /dev/ttyACM0 --fqbn CONTROLLINO_Boards:avr:controllino_maxi_automation
echo "start the program on the raspberry pi"
sudo systemctl start testbench.service我想改进这个脚本,这样我就不用再改变它了。我希望脚本搜索.ino文件并将其作为参数传递。如果超过一个.ino文件,脚本将询问需要编译哪个文件。如果没有找到.ino文件,请打印错误消息。我试过了
INOFILE="*.ino"
#echo $INOFILE
stringarray=($INOFILE)
a=0
while [ ${stringarray[$a]} -ge 0 ]
do
done
echo ${stringarray[0]}
echo ${stringarray[1]}如何检查stringarray[$a]是否为空?INOFILE的类型是什么?
发布于 2020-11-20 15:35:19
试试这个,只需设置变量:
#!/bin/bash
declare -a stringarray
stringarray="$(ls -1 | grep .ino\$)"
if [ ${#stringarray[@]} -ne 0 ]; then
for inofile in "${stringarray[@]}"
do
ino="$(realpath "$inofile")"
arduino-cli compile -v --fqbn CONTROLLINO_Boards:avr:controllino_maxi_automation "$ino"
done
fi if [ ${#stringarray[@]} -eq 0 ];检查数组是否为空stringarray[@]:包含数组的所有元素
for inofile in "${stringarray[@]}":遍历数组并将当前数组值设置为inofile变量
realpath "$inofile" :获得ino文件的绝对路径。
https://unix.stackexchange.com/questions/620686
复制相似问题