Define function in unix/linux command line (e.g. BASH)

后端 未结 5 1321
时光说笑
时光说笑 2021-01-04 00:21

Sometimes I have a one-liner that I am repeating many times for a particular task, but will likely never use again in the exact same form. It includes a file name that I am

5条回答
  •  天涯浪人
    2021-01-04 01:04

    You can also count files without find. Using arrays,

    numresults () { local files=( "$1"/* ); echo "${#files[@]}"; }
    

    or using positional parameters

    numresults () { set -- "$1"/*; echo "$#"; }
    

    To match hidden files as well,

    numresults () { local files=( "$1"/* "$1"/.* ); echo $(("${#files[@]}" - 2)); }
    numresults () { set -- "$1"/* "$1"/.*; echo $(("$#" - 2)); }
    

    (Subtracting 2 from the result compensates for . and ...)

提交回复
热议问题