2016-05-10 13 views
-5

ich ein BASH-Projekt für die Universität und ich habe zwei Fehler, die ich nichtBASH unerwartetes EOF bei der Suche nach passenden ``‘

hier ist mein Skript BASH verstehen:

#!/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; 

Und hier die beiden Fehler ich habe:

./collecteur_cpu_reseau: line 17: unexpected EOF while looking for matching ``' 
./collecteur_cpu_reseau: line 21: syntax error: unexpected end of file 

Vielen Dank im Voraus für Ihre Hilfe

+2

http://shellcheck.net/ ist dein Freund. –

+1

auch, http://StackOverflow.com/Help/Mcve –

+2

... so viele Male "Lscpu" laufen anstatt nur einmal zu laufen und die Ergebnisse wieder zu verwenden ist furchtbar ineffizient, übrigens; Ich würde vorschlagen, dass Sie Ihren Ansatz überdenken. –

Antwort

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

... braucht ...

# remove the extra backtick 
proc_load_average=`w | head -1 | cut -d" " -f12 | cut -d"," -f1-2 | tr ',' '.'` 

... oder besser zu sein ...

# use parens, not backticks 
proc_load_average=$(w | head -1 | cut -d" " -f12 | cut -d"," -f1-2 | tr ',' '.') 

... oder besser ...

# 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 sagte, dieses Skript als Ganzes ist unausweichlich schrecklich.


Betrachten wir zum Beispiel die folgende Alternative zu der rundum in /proc/cpuinfo Messing:

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']}" 

... ist das nicht einfacher? Sie können dieselbe Strategie implementieren, indem Sie die Ausgabe eines Befehls wie lscpu lesen, indem Sie für </proc/cpuinfo ersetzen.

Verwandte Themen