シェルパイプラッパー関数
シェルパイプラッパー関数
Linuxシェル内の任意の組み込みコマンド、関数、実行可能プログラムからパイプ関数を作成します.
Shell Pipe Wrapper Functions
Pipe Wrapper Function Simple Example
Pipe Wrapper Function Advanced Example
免責事項
言及する人々のために
xargs
これは余分な論理処理をより簡単にする別の方法です.閉じるこの動画はお気に入りから削除されていますxargs
.このポストはLinuxのコマンドライン、コマンド、シェル関数、std、およびPHYに精通していることを前提としていますが、新しいのであれば理解するのは難しすぎません.
Linuxコマンドラインでの出力
管効率
パイピングの出力は常に最高の方法ではないし、最速の方法ではありませんし、出力の処理の独自のメソッドを持っている場合は、非常に冗長することができます.
find -exec {} \;
) パイプラインの各部分が副殻であるが、多くの場合、この非効率性は無視できる.そして、時々、パイプがほとんど必要であるか、少なくとも出力を処理する最も良い方法である.パイプ有効性
我々はすべてのパイプを使用している可能性が高い
|
Linuxのシェル環境でstdoutを別の組み込みコマンド、関数、実行可能ファイルにパイプし、最終的にはすべてをパイプにパイプすることができないことを認識します.コマンドをパイプに出力するには
stdin
(ユーザ入力として)すべてのプログラムがそうするわけではない.パイプラッパー関数
このポストでは、私はどのようにして、どのようなコマンド、関数、あるいは標準入力をしていないバイナリファイルプログラムに対して一般的なシェル関数を書くかを示します.
これは100 %のチュートリアルではありませんが
documenting comments
そして、私が行くように、説明します.パイプラッパー論理
基本的な論理は以下の通りです.
FD
) が0より大きい場合、( PIPINサブシェルに既にstdinがあるかどうかを調べます):while
入力を渡す入力を読み込み、引数、コマンド、関数、ファイルに読み込みます.else
) 受信stdin
:例
これらの例は全て同じシンプルな構造/概念を共有していますが、内部で必要な論理だけを変更します
if [ ! -t 0 ]; then
ブロックより頻繁に、while
ブロック.ほとんどの場合、我々はここで書かれているものと、余分な論理/式ではないの基礎を必要とするだけです.
function command_pipe {
if [ ! -t 0 ]; then
local input
# input logic here
# while read or otherwise to create $input
command "$@" "$input"
else
command "$@"
fi
}
パイプラッパー機能簡単な例
最も一般的な使用法.
簡単な例statプログラム
これは
stat
コマンドは標準入力を受け入れることができますstdout
同時に、コマンドに通常の引数を渡すことができます.プログラム機能ラッパー
function stat_pipe {
if [ ! -t 0 ]; then # Test if there is input
local input
while read -r input; do # read each input
if [[ -f "$input" ]] || # this is extra logic
[[ -d "$input" ]]; then # more extra logic
stat "$@" "$input" # process input with command and arguments
fi
done
else stat "$@"; fi # if no input then run the command as usual with arguments
}
プログラム機能例
印刷する
.bash_func
ファイルとファイルサイズを取得する $ printf '%s\n' .bash_func*
.bash_funcs
.bash_funcs_nocomments
.bash_funcs.old
$ printf '%s\n' .bash_func* | stat_pipe -c %s
90329
53140
59990
$ stat_pipe -c %s .bash_func*
90329
53140
59990
パイプラッパー関数
もう少し進んだもの.
高度なプログラムRMプログラム
のラッパー
rm
最初の入力以外のユーザー入力を受け入れ、ファイルやフォルダを削除するかどうかを決定するコマンドif the else
ブロックを実行します(パイプからではなく)、通常通りコマンドを実行します.もちろん、注意してください
rm
したがって、余分なセキュリティを追加する余分なテストロジックを追加しました.この例はシェルとしてbashに依存しています.
プログラム関数ラッパー
function rm_pipe {
if [[ ! -t 0 ]]; then
local input input_user
while read -r input; do
if [[ -f "$input" ]] ||
[[ -d "$input" ]]; then
printf 'Would you like to delete: %s: (y/[N])?\n' "$input"
read -u 1 input_user # '-u 1' read from keyboard rather than stdin; defaults to No.
if [[ "$input_user" =~ ^([yY]|[yY][eE][sS])$ ]]; then
rm "$@" "$input" # if user input is yes then process...
fi
fi
done
else rm "$@"; fi
}
プログラム関数の例
$ cd fake
$ printf '%s\n' *
a
b
c
d
$ printf '%s\n' * | rm_pipe -rf
Would you like to delete: a: (y/[N])?
y
Would you like to delete: b: (y/[N])?
Would you like to delete: c: (y/[N])?
no
Would you like to delete: d: (y/[N])?
yes
$ printf '%s\n' *
b
c
$
結論
これにより、コマンドライン、ファンクション、実行可能ファイルプログラムがLinuxコマンドラインでパイプにアクセスできるようになります.私は多くのことのためにこれらのタイプの機能を作ります、そして、彼らは時間と努力の多くを節約するために終わります.
Reference
この問題について(シェルパイプラッパー関数), 我々は、より多くの情報をここで見つけました https://dev.to/thefluxapex/shell-pipe-wrapper-functions-d6テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol