
command1とcommand2という2つの正規表現があり、両方の式を1つの式に結合して|
command1の出力を次の式に渡す必要があります。
コマンド1:
grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6
>> Output : 00:00:01
コマンド2:
head -n1 /sys/bus/pci/devices/00:00:01/resource | cut -d ' ' -f 1 | tail -c 9
command1を使用して(00:00:01)をcommand2に出力し、それを単一の式に結合するにはどうすればよいですか?
答え1
1つのコマンドの出力を2番目のコマンドの引数として使用するメカニズムコマンドの置き換え $()
書くことができる。たとえば、
変える
$ whoami
jimmij
$ ls /home/jimmij/tmp
file1 file2
あなたはできます
$ ls /home/"$(whoami)"/tmp
file file2
場合によっては、単一のコマンドが次のようになります。
head -n1 "/sys/bus/pci/devices/$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)/resource" | cut -d ' ' -f 1 | tail -c 9
私も完全な表現を引用しました。ここを読んでくださいなぜこれを行うべきですか?
答え2
$(command)
構文(または前の構文)を使用してください`command`
。
DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9
ああ。 「式で結合」に似ています。
DEVICE=$(grep 0x017a /sys/bus/pci/devices/*/device | cut -d/ -f6)
OUTPUT=$(head -n1 "/sys/bus/pci/devices/$DEVICE/resource" | cut -d ' ' -f 1 | tail -c 9)
echo "Output is $OUTPUT"