BASH 在寻找匹配的 ``' 时出现意外的 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37142936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:17:47  来源:igfitidea点击:

BASH unexpected EOF while looking for matching ``'

bashshelleof

提问by Nathan30

I have a BASH project for university and I have two errors that I don't understand

我有一个大学的 BASH 项目,我有两个我不明白的错误

here is my script BASH :

这是我的脚本 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 }' | awk -F ':' '{print }'`;
ip_infos_addr_ipv6=`/sbin/ifconfig eth0 | awk '/adr inet6:/{print }'`;
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

Thanks in advance for your help

在此先感谢您的帮助

回答by Charles Duffy

proc_load_average=`w | head -1 | cut -d" " -f12 | cut -d"," -f1-2` | tr ',' '.'`

...needs to be...

...需要是...

# 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.

也就是说,这个剧本作为一个整体是无可挽回的糟糕。



For instance, consider the following alternative to all the messing around in /proc/cpuinfo:

例如,考虑以下替代 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']}"

...isn't that easier? You can deploy the same strategy reading the output of a command like lscpuby substituting < <(lscpu)for </proc/cpuinfo.

……那不是更容易吗?您可以像部署同样的策略读取命令的输出lscpu通过替换< <(lscpu)</proc/cpuinfo