创建一个参数来查找以linux中特定字母开头的文件

时间:2014-06-10 17:27:41

标签: linux bash

我目前正在为我的Linux决赛学习,而我正在努力练习:

所以我的脚本看起来像这样:

    #!/bin/bash

if [ "$1" ==  "-e" ]
  then
fullfilename=$(basename $2)
extension=${fullfilename##*.}
filename=${fullfilename%.*}

path="$3"
#string=$( find . -type f -name "*.$extension" )
string=$( find $path -type f -name "*.$extension" ) 

for FILE in $string
do
nam=$( basename $FILE )
DIR=$( dirname $FILE )
#DIR=$( ls -d -1 $PWD/*.$extension )
echo -e "$nam \t $DIR "

done 



 elif [ x$1 = "x-h" ] || [ x$1 = "x--help" ] 
then
echo -e "\nusage: bash filename.sh [parameter] [extension] [directory] \n
     example : bash filename.sh -e txt /home/user/ \n
         Parameters :\n
     \t -e\t \t Default parameter \n
         \t -h or --help\t \t show help \n
     \t -nr \t\t find with no recursion "


elif [ $1 == "-nr" ]
 then
fullfilename=$(basename $2)
extension=${fullfilename##*.}
filename=${fullfilename%.*}

path="$3"
#string=$( find . -type f -name "*.$extension" )
string=$( find $path -maxdepth 1 -type f -name "*.$extension" )

for FILE in $string
do
nam=$( basename $FILE )
DIR=$( dirname $FILE )
#DIR=$( ls -d -1 $PWD/*.$extension )
echo -e "$nam \t $DIR "

done

else
echo "use -h or --help for help"
fi

此脚本的目标是查找具有特定扩展名的文件并显示他们所在的目录。

我现在要做的是添加一个参数“-fl”,它只搜索以你放在参数后面的字母开头的文件(例如./filename.sh -fl m txt / home / user只会搜索以字母“m”开头的txt文件

知道如何在我的脚本中实现这个吗?

我认为这与这个

有关
#!/bin/bash

if [ $1 = "-fl" ] 
then
echo [$(echo "$2")]*
fi

但不知道如何添加它而不会弄乱。

1 个答案:

答案 0 :(得分:2)

实际上很难尝试修复代码,因此我只是为了好玩而制作了一个版本。你可以参考这个。对于你不会理解的任何事情,阅读bash manual会给出很多解释。有了它,如果你非常勤奋地信任我,你甚至不需要wiki或教程。

#!/bin/bash

CONFIG_EXT=''
CONFIG_FIRST_LETTER=''
CONFIG_NO_RECURSION=false
CONFIG_PATH=''

function error {
    echo "$1" >&2
    exit 1
}

function show_help_info {
    echo "Usage: $0 -e ext [-fl letter] [-nr] [--] directory
Example: $0 -e txt -fl c -- /home/user/
Options:
  -e          Specifies extension.
  -fl         Specifies first letter.
  -nr         Find with no recursion.
  -h, --help  Show help."
}

while [[ $# -gt 0 ]]; do
    case "$1" in
    -e)
        [[ -n $2 ]] || error "Option '-e' doesn't have an argument."
        CONFIG_EXT=$2
        shift
        ;;
    -nr)
        CONFIG_NO_RECURSION=true
        ;;
    -fl)
        [[ -n $2 ]] || error "Option '-fl' doesn't have an argument."
        [[ $2 == [[:alpha:]] ]] || error "Argument to option '-fl' must be a single letter."
        CONFIG_FIRST_LETTER=$2
        shift
        ;;
    -h|--help)
        show_help_info
        exit 1
        ;;
    -*)
        error "Invalid option: $1"
        ;;
    --)
        CONFIG_PATH=$2
        ;;
    *)
        CONFIG_PATH=$1
        ;;
    esac
    shift
done

if [[ -z $CONFIG_EXT ]]; then
    error "Extension was not specified. Please use -h or --help for usage info."
elif [[ -z $CONFIG_PATH ]]; then
    error "Target path was not specified. Please use -h or --help for usage info."
fi

FL_ARGS=()
[[ -n $CONFIG_FIRST_LETTER ]] && FL_ARGS=('-name' "${CONFIG_FIRST_LETTER}*")

MAXDEPTH_ARGS=()
[[ $CONFIG_NO_RECURSION == true ]] && MAXDEPTH_ARGS=('-maxdepth' 1)

while IFS= read -r FILE; do
    printf "%s\t%s\n" "${FILE##*/}" "${FILE%/*}"
done < <(exec find "$CONFIG_PATH" "${MAXDEPTH_ARGS[@]}" -type f "${FL_ARGS[@]}" -name "*.${CONFIG_EXT}")

# Similar:
#
# find "$CONFIG_PATH" "${MAXDEPTH_ARGS[@]}" -type f "${FL_ARGS[@]}" -name "*.${CONFIG_EXT}" -printf '%f\t%H\n'

示例:

bash temp.sh /var/tmp/tar-1.27.1/ -e m4 -fl c

输出:

configmake.m4   /var/tmp/tar-1.27.1/m4
codeset.m4      /var/tmp/tar-1.27.1/m4
closeout.m4     /var/tmp/tar-1.27.1/m4
closedir.m4     /var/tmp/tar-1.27.1/m4
close.m4        /var/tmp/tar-1.27.1/m4
close-stream.m4 /var/tmp/tar-1.27.1/m4
clock_time.m4   /var/tmp/tar-1.27.1/m4
chown.m4        /var/tmp/tar-1.27.1/m4
chdir-long.m4   /var/tmp/tar-1.27.1/m4
canonicalize.m4 /var/tmp/tar-1.27.1/m4