在寻找匹配的``'时发生BASH意外的EOF [英] BASH unexpected EOF while looking for matching ``'
本文介绍了在寻找匹配的``'时发生BASH意外的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大学的BASH项目,但有两个我不理解的错误
I have a BASH project for university and I have two errors that I don't understand
这是我的脚本BASH:
here is my script BASH :
#!/bin/bash
proc_name=`cat /proc/cpuinfo | grep 'model name' | cut -d':' -f2 |cut -d'@' -f1 | uniq`;
proc_freq=`cat /proc/cpuinfo | grep 'model name' | cut -d':' -f2 |cut -d'@' -f2 | uniq`;
proc_core=`cat /proc/cpuinfo | grep 'cpu cores' | cut -d':' -f2 | uniq`;
proc_hyperthreading=`cat /proc/cpuinfo | grep 'siblings' | cut -d':' -f2 | uniq`;
proc_architecture=`lscpu | grep '64-bit' | cut -d',' -f2 | cut -d'-' -f1`;
proc_cache_L1=`lscpu | grep 'Cache L1i' | cut -d':' -f2 | sed "s/\ \ */\ /g"`;
proc_cache_L2=`lscpu | grep 'Cache L2' | cut -d':' -f2 | sed "s/\ \ */\ /g"`;
proc_cache_L3=`lscpu | grep 'Cache L3' | cut -d':' -f2 | sed "s/\ \ */\ /g"`;
proc_virtualisation=`lscpu | grep 'Virtualisation' | cut -d':' -f2 |sed "s/\ \ */\ /g"`;
proc_load_average=`w | head -1 | cut -d" " -f12 | cut -d"," -f1-2` | tr ',' '.'`
ip_infos_addr_ipv4=`/sbin/ifconfig eth0 | awk '/inet adr:/{print $2}' | awk -F ':' '{print $2}'`;
ip_infos_addr_ipv6=`/sbin/ifconfig eth0 | awk '/adr inet6:/{print $3}'`;
ip_publique_addr=`dig +short myip.opendns.com @resolver1.opendns.com`;
carte_reseau=`lspci |grep Ethernet | cut -d":" -f3`;
echo -e "$proc_name\n$proc_freq\n$proc_core\n$proc_hyperthreading\n$proc_architecture\n$proc_cache_L1\n$proc_cache_L2\n$proc_cache_L3\n$proc_virtualisation\n$proc_load_average\n$ip_infos_addr_ipv4\n$ip_infos_addr_ipv6\n$ip_publique_addr\n$carte_reseau" > Collecteur/collecteur_cpu_reseau.txt;
这是我遇到的两个错误:
And here is the two errors I have :
./collecteur_cpu_reseau: line 17: unexpected EOF while looking for matching ``'
./collecteur_cpu_reseau: line 21: syntax error: unexpected end of file
预先感谢您的帮助
推荐答案
proc_load_average=`w | head -1 | cut -d" " -f12 | cut -d"," -f1-2` | tr ',' '.'`
...需要...
# remove the extra backtick
proc_load_average=`w | head -1 | cut -d" " -f12 | cut -d"," -f1-2 | tr ',' '.'`
...甚至更好...
...or better...
# use parens, not backticks
proc_load_average=$(w | head -1 | cut -d" " -f12 | cut -d"," -f1-2 | tr ',' '.')
...或更好...
...or better...
# read the content straight from procfs without the big silly pipeline
read -r loadavg_1min loadavg_5min loadavg_10min _ </proc/loadavg
echo "1-minute load average: $loadavg_1min"
就是说,这个脚本整体上是很难挽救的。
That said, this script as a whole is unsalvageably awful.
例如,考虑以下替代方案在 / proc / cpuinfo
:
For instance, consider the following alternative to all the messing around in /proc/cpuinfo
:
declare -A cpuinfo=( ) # create an associative array
# read line-by-line; see http://mywiki.wooledge.org/BashFAQ/001
while IFS=$'\t:' read -r k v || [[ $k ]]; do
[[ $v ]] || continue
cpuinfo[$k]=${v# } # trim leading space; see http://wiki.bash-hackers.org/syntax/pe
done </proc/cpuinfo
echo "Model name: ${cpuinfo['model name']%@*}"
echo "Model freq: ${cpuinfo['model name']#*@}"
echo "Actual frequency: ${cpuinfo['cpu MHz']}"
echo "Siblings: ${cpuinfo['siblings']}"
...不是这样容易吗?您可以通过替换<来部署相同的策略,以读取
表示 lscpu
之类的命令的输出。 <(lscpu)< / proc / cpuinfo
。
...isn't that easier? You can deploy the same strategy reading the output of a command like lscpu
by substituting < <(lscpu)
for </proc/cpuinfo
.
这篇关于在寻找匹配的``'时发生BASH意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文