如何在Shell脚本中添加-h选项

时间:2017-03-31 23:25:13

标签: bash shell

我正在尝试在我的shell脚本上实现一条帮助消息,以便在以“-h”作为参数运行脚本时获取一个用法示例。我想方设法在网上做,但它只是没有用......

这是我的代码:

#!/usr/bin/env sh

usage="$(basename "$0") [-h] -- program to change all files from one extension to another extension

where:
    -h      show this help text
    ext1    extension to be changed
    ext2    new desired extension

    usage example: ./chage_entension.sh .cpp .c"

while [[ getopts ':hs:' option ]]; do
    case "$option" in
        h) echo "$usage"
            ;;
    esac
done
shift $((OPTIND - 1))

ext1 = "$1"
ext2 = "$2"

for file in *$1; do
    mv "$file" "`basename "$file" $1`$2"
done

这是我在尝试运行时遇到的错误./change_extension.sh

/home/andre/src/scripts/change_extension.sh: line 12: conditional binary operator expected
/home/andre/src/scripts/change_extension.sh: line 12: syntax error near `':hs:''
/home/andre/src/scripts/change_extension.sh: line 12: `while [[ getopts ':hs:' option ]]; do'

0 个答案:

没有答案