逆引きシェルスクリプト/while内部の変数が反映されない場合の対処
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* while内部の変数が反映されない場合の対処 [#n5678a02]
パイプを使って、whileに渡し変数を操作すると、whileを抜け...
原因は、whileは別プロセスとして動作するためwhile内であれ...
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* 期待した動作をしないwhileスクリプト [#n80cee7a]
#ref(while1.sh)
#!/bin/bash
# hostsの行をカウントします
i=0
cat /etc/hosts | while read L
do
i=`expr $i + 1`
echo inside: $i
done
echo outside: $i
実行結果
$ ./while1.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside 0
whileを抜けた後に、$iを出力すると、0になっています。
* 期待した動作をするスクリプト [#w95ca955]
パイプを使わず、リダイレクトを使う。
#ref(while2.sh)
#!/bin/bash
# hostsの行をカウントします
i=0
while read L
do
i=`expr $i + 1`
echo inside: $i
done < /etc/hosts
echo outside: $i
実行結果
$ ./while2.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside: 43
意図した動作になっています。~
''しかし、リダイレクトでファイルを指定していますが、stdin...
解決方法は、以下に記します。
* stdin(pipe)による場合の記述方法 [#c142cb44]
Process Substitutionを使うことにより解決します。~
man bashをするとProcess Substitutionについて記述されてい...
$ LANG=C man bash
<snip>
Process Substitution
Process substitution is supported on systems that...
of naming open files. It takes the form of <(list...
input or output connected to a FIFO or some file ...
argument to the current command as the result of t...
ing to the file will provide input for list. ...
argument should be read to obtain the output of li...
When available, process substitution is performed ...
sion, command substitution, and arithmetic expansi...
<snip>
$ man bash
<snip>
プロセス置換
プロセス置換 (process substitution) がサポートされ...
の指定 をサポートしているシステムです。これは <(li...
/dev/fd 中の 何らかのファイルに接続された状態で実...
渡されます。 >(list) の形式を使った場合、 ファイル...
渡されたファイルは list の出力を得るために読み込ま...
利用可能であれば、プロセス置換 (process substituti...
<snip>
実際にstdinから記述したサンプルスクリプトを以下に記します...
関数にしてみました。
#ref(while3.sh)
#!/bin/bash
CountLineStdin() {
if [ ! -p /dev/stdin ]; then
echo "stdin empty!"
return 1
fi
i=0
while read line
do
i=`expr $i + 1`
echo "inside: $i"
done < <(cat -)
return 0
}
CountLineStdin
echo "outside: $i"
実行結果
$ cat /etc/hosts | ./while3.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside: 43
上記出力の通り、期待した動作になっています。
記述に関しての詳細は、man bashおよび、ネット調べてくださ...
重要なのは、while do ... done の <の記述になります。~
''done < <(cat -)''と記述してください。~
スクリプトを簡単に説明すると ! -p /dev/stdin でパイプによ...
cat -はstdinを出力しています。
以上、while内部の変数が反映されない場合の対処方法でした。
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
終了行:
#navi(../)
* while内部の変数が反映されない場合の対処 [#n5678a02]
パイプを使って、whileに渡し変数を操作すると、whileを抜け...
原因は、whileは別プロセスとして動作するためwhile内であれ...
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* 期待した動作をしないwhileスクリプト [#n80cee7a]
#ref(while1.sh)
#!/bin/bash
# hostsの行をカウントします
i=0
cat /etc/hosts | while read L
do
i=`expr $i + 1`
echo inside: $i
done
echo outside: $i
実行結果
$ ./while1.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside 0
whileを抜けた後に、$iを出力すると、0になっています。
* 期待した動作をするスクリプト [#w95ca955]
パイプを使わず、リダイレクトを使う。
#ref(while2.sh)
#!/bin/bash
# hostsの行をカウントします
i=0
while read L
do
i=`expr $i + 1`
echo inside: $i
done < /etc/hosts
echo outside: $i
実行結果
$ ./while2.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside: 43
意図した動作になっています。~
''しかし、リダイレクトでファイルを指定していますが、stdin...
解決方法は、以下に記します。
* stdin(pipe)による場合の記述方法 [#c142cb44]
Process Substitutionを使うことにより解決します。~
man bashをするとProcess Substitutionについて記述されてい...
$ LANG=C man bash
<snip>
Process Substitution
Process substitution is supported on systems that...
of naming open files. It takes the form of <(list...
input or output connected to a FIFO or some file ...
argument to the current command as the result of t...
ing to the file will provide input for list. ...
argument should be read to obtain the output of li...
When available, process substitution is performed ...
sion, command substitution, and arithmetic expansi...
<snip>
$ man bash
<snip>
プロセス置換
プロセス置換 (process substitution) がサポートされ...
の指定 をサポートしているシステムです。これは <(li...
/dev/fd 中の 何らかのファイルに接続された状態で実...
渡されます。 >(list) の形式を使った場合、 ファイル...
渡されたファイルは list の出力を得るために読み込ま...
利用可能であれば、プロセス置換 (process substituti...
<snip>
実際にstdinから記述したサンプルスクリプトを以下に記します...
関数にしてみました。
#ref(while3.sh)
#!/bin/bash
CountLineStdin() {
if [ ! -p /dev/stdin ]; then
echo "stdin empty!"
return 1
fi
i=0
while read line
do
i=`expr $i + 1`
echo "inside: $i"
done < <(cat -)
return 0
}
CountLineStdin
echo "outside: $i"
実行結果
$ cat /etc/hosts | ./while3.sh
inside: 1
inside: 2
inside: 3
inside: 4
inside: 5
inside: 6
inside: 7
inside: 8
<snip>
outside: 43
上記出力の通り、期待した動作になっています。
記述に関しての詳細は、man bashおよび、ネット調べてくださ...
重要なのは、while do ... done の <の記述になります。~
''done < <(cat -)''と記述してください。~
スクリプトを簡単に説明すると ! -p /dev/stdin でパイプによ...
cat -はstdinを出力しています。
以上、while内部の変数が反映されない場合の対処方法でした。
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
ページ名: