シェルパイプラッパー関数


シェルパイプラッパー関数


Linuxシェル内の任意の組み込みコマンド、関数、実行可能プログラムからパイプ関数を作成します.

  • Shell Pipe Wrapper Functions
  • Disclaimer
  • Piping Output In The Linux Command Line
  • Pipe Efficiency
  • Pipe Availability
  • Pipe Wrapper Functions
  • Pipe Wrapper Logic
  • Examples

  • Pipe Wrapper Function Simple Example
  • Simple Example STAT Program
  • STAT Program Function Wrapper
  • STAT Program Function Example

  • Pipe Wrapper Function Advanced Example
  • Advanced Example RM Program
  • RM Program Function Wrapper
  • RM Program Function Example
  • Conclusion
  • 免責事項



    言及する人々のために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コマンドラインでパイプにアクセスできるようになります.私は多くのことのためにこれらのタイプの機能を作ります、そして、彼らは時間と努力の多くを節約するために終わります.