複数のシェル結果を異なる変数に分割したいと思います。
以下は、説明するためのセンサーのサンプル出力です。
acpitz-virtual-0
Adapter: Virtual device
temp1: +41.0°C (crit = +95.0°C)
coretemp-isa-0000
Adapter: ISA adapter
Core 0: +36.0°C (high = +90.0°C, crit = +90.0°C)
Core 1: +36.0°C (high = +90.0°C, crit = +90.0°C)
sensors
この場合、出力全体を含む変数(コマンド名)、 sensors_acpitz_virtual_0
出力全体を含む最初のセグメント、およびsensors_coretemp_isa_0000
2番目のセグメントの内容を含む変数が必要です。
センサー_acpitz_virtual_0
acpitz-virtual-0
Adapter: Virtual device
temp1: +41.0°C (crit = +95.0°C)
Sensors_coretemp_isa_0000
coretemp-isa-0000
Adapter: ISA adapter
Core 0: +36.0°C (high = +90.0°C, crit = +90.0°C)
Core 1: +36.0°C (high = +90.0°C, crit = +90.0°C)
sensors_acpitz_virtual_0__Adapter__Virtual_device
次に、最初の段落の内容のみを含む変数があります。
Adapter: Virtual device
temp1: +41.0°C (crit = +95.0°C)
sensors_acpitz_virtual_0__Adapter__Virtual_device__temp1
acpitz-virtual-0仮想デバイスアダプタなどのtemp1温度の変数のみが含まれています。
+41.0°C (crit = +95.0°C)
どのツールを使用する必要があります(ディストリビューションプレートにプレインストールされている一般的なツールが望ましい)、この結果をどのように取得できますか?
ベストアンサー1
私は次のことをします:
eval "$(#"
perl -MString::ShellQuote -00 -lne '
if (/^(.+)\n(.+)/) {
($v1, $v2, $rest) = ("sensors_$1", "$2", $'\'');
# $v1, $v2 contain the first 2 lines, $rest the rest
s/\W/_/g for $v1, $v2;
# replace non-word characters with _ in the variables
print "$v1=" . shell_quote("$1\n$2$rest");
print "${v1}__$v2=" . shell_quote("$2$rest");
# output the variable definition taking care to quote the value
while ($rest =~ /^(.*?):\s*(.*)/gm) {
# process the "foo: bar" lines in the rest
($v3,$val) = ("$1", $2);
$v3 =~ s/\W/_/g;
print "${v1}__${v2}__$v3=" . shell_quote($val)
}
}' < that-file)"
-00
短絡モードの場合。-l
レコードの末尾から段落を除き、print
出力に1つを追加します。
-n
一度に1つのレコード入力を処理します。
あなたの例では、perlコマンドは次のシェルコードを出力します。
sensors_acpitz_virtual_0='acpitz-virtual-0
Adapter: Virtual device
temp1: +41.0°C (crit = +95.0°C)'
sensors_acpitz_virtual_0__Adapter__Virtual_device='Adapter: Virtual device
temp1: +41.0°C (crit = +95.0°C)'
sensors_acpitz_virtual_0__Adapter__Virtual_device__temp1='+41.0°C (crit = +95.0°C)'
sensors_coretemp_isa_0000='coretemp-isa-0000
Adapter: ISA adapter
Core 0: +36.0°C (high = +90.0°C, crit = +90.0°C)
Core 1: +36.0°C (high = +90.0°C, crit = +90.0°C)'
sensors_coretemp_isa_0000__Adapter__ISA_adapter='Adapter: ISA adapter
Core 0: +36.0°C (high = +90.0°C, crit = +90.0°C)
Core 1: +36.0°C (high = +90.0°C, crit = +90.0°C)'
sensors_coretemp_isa_0000__Adapter__ISA_adapter__Core_0='+36.0°C (high = +90.0°C, crit = +90.0°C)'
sensors_coretemp_isa_0000__Adapter__ISA_adapter__Core_1='+36.0°C (high = +90.0°C, crit = +90.0°C)'
eval "$(that-perl-command)"
このコマンドの出力を評価するようにシェルに指示するために使用するコード。