【发布时间】:2021-08-29 18:56:42
【问题描述】:
您好,我是一个新手,正在尝试制作一个可以自动安装多个 SBC 的 bash 脚本。
它创建了几个文件、文件夹、cronjobs、tweeks 一些配置。
我在创建其他 shell 脚本时遇到了困难。
我试过了:
cat > cputemp.sh <<EOF
#!/bin/bash
while :
do
x=$( cat /sys/devices/virtual/thermal/thermal_zone0/temp )
y=$( cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq )
z=$(expr $y / 1000)
t=$(expr $x / 1000)
echo -en "\033[1K\r\e[91mTemperature:\e[0m" "\e[93m$tº\e[0m "
echo -en "\e[91mFrequency:\e[0m" "\e[93m$z MHz\e[0m"
sleep 2
done
EOF
但不是将这些行放在文件中,它似乎正在尝试运行它们。我明白了
cat: /sys/devices/virtual/thermal/thermal_zone0/temp: No such file or directory
cat: /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq: No such file or directory
expr: syntax error: unexpected argument ‘1000’
expr: syntax error: unexpected argument ‘1000’
我错过了什么?顺便说一句,它找不到这些位置,因为我正在使用 WSL 而不是 SBC 的 Windows 上测试脚本。
提前致谢。
【问题讨论】:
-
我认为你只需要在变量赋值上转义
$(...)。基本上用\$替换$ -
如果您使用的是 Bash,则不需要
expr;算术工具内置在外壳中。z=$((y / 1000))等(如果您需要分数,请尝试 Awk 或bc;但expr也只是整数)。
标签: bash shell automation heredoc