如何快速生成一个shell脚本?

脚本如下

#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail

useage(){
  cat <<HELP
USAGE:
    $(basename $0) scriptpath scriptargs...
HELP
}

exit_err() {
   echo >&2 "${1}"
   exit 1
}

if [ $# -lt 1 ];then
    useage
    exit 1
fi

SCRIPTPATH=$1
SCRIPTNAME=$(basename "${SCRIPTPATH}")
shift 1
SCRIPTARGS=$*
SCRIPTARGNUM=$#
cat > "${SCRIPTPATH}" <<EOFFF
#!/usr/bin/env bash
[[ -n \$DEBUG ]] && set -x
set -eou pipefail

useage(){
  cat <<HELP
USAGE:
    ${SCRIPTNAME} ${SCRIPTARGS}
HELP
}

exit_err() {
   echo >&2 "\${1}"
   exit 1
}

if [ \$# -lt ${SCRIPTARGNUM} ];then
    useage
    exit 1
fi

EOFFF

i=1
for ARG in ${SCRIPTARGS};do
cat >> "${SCRIPTPATH}" <<EOFF
${ARG}=\$$i
EOFF
(( i++ ))
done
chmod +x "${SCRIPTPATH}"

使用方法

假设要新建一个脚本名为test 参数分别为a, b ,c

new ./test.sh a b c
posted @ 2019-12-06 22:00  futuretea  阅读(1283)  评论(0编辑  收藏  举报