为 shell 脚本提供参数和帮助部分 [英] Giving a parameter and help section to shell script

查看:36
本文介绍了为 shell 脚本提供参数和帮助部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想手动确定文件夹名称.例如,如果用户按如下方式运行此脚本,

I have a problem that I want to determine folder name manually. For example, if user runs this script as below,

./scriptname -k 

他们可以获取结果,但文件夹名称是在脚本中确定的.如何手动确定文件夹名称,如下所示?

They can take the results, but folder names are determined in script. How can I determine folder name manually as below?

推荐答案

你可以把帮助写成一个函数:

You could write the help as a function:

usage(){
cat <<EOT
Usage: ${0##*/} dir1 dir2
EOT
}

if [ $# -ne 2 ]; then
    usage
    echo 'please enter two folder names'
    exit 1
fi

if [ ! -d "$1" ]; then
    usage
    echo "$1 is not a folder"
    exit 1
fi

if [ ! -d "$2" ]; then
    usage
    echo "$2 is not a folder"
    exit 1
fi

# continue script

这篇关于为 shell 脚本提供参数和帮助部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆