从命令行检查并适当更正参数作为脚本的输入

dd_code

我想问您并请求脚本这一特定部分的帮助-我想使其变得更紧凑,因为我觉得它太长了,否则我可能会使用更短的语法...

这部分是从我的脚本中剪切下来的,用于创建X个数量的文件(降序跳1天,因此每个文件比上一个日期早1天),这已经比整个“实现”部分长3倍。

This part's purpose is to maintain, check and correct (appropriately) the arguments defined with the script execution, which looks like
./script.sh X X
whereas script expects two arguments (number indicating amount of files to create and the path to the folder, where it is supposed to create those0:
- if there is just 1 it assumes the user wants to create files in the present folder, therefore it checks, if the one included argument is a number and if it is it will add output from pwd to the path (in this case to not have some sort of "cross-mount" accident with the system variable PATH conveniently named PATHX), so there will be two arguments at the end anyway
- if there are more than 2, it will terminate the script

If there are 2 arguments, it performs additional "tasks:
1. checks if there are just two numbers, if so, script ends
2. checks if there are just two words/letters, if so, script ends
3. if there are, let's say, just two dots as arguments, it will end anyway as there is one if, which always will need one argument to be just number
4. after first steps this will save the arguments to the variables (it is necessary as I have had some problems in the implementation part), this step even coves any possible combination of the positions of the arguments (it does not matter now, if the amount is first or second; same for the path)
5. this is a very last step, which just (for any case) covers the case, the path included has not been inserted with the slash in front (as this is mandatory for bash to recognise it indicates an absolute path) or at the end (important for the touch command in the implementation path as the command looks like "touch -t *TIMESTAMP* "$PATHXfile$i""); together with it it does provide an appropriate action, if the path is defined with ".","..","./","../" - if there would be a full path, not relative one


if [[ $# -eq 2 ]]
then
    if [[ $1 == ?(-)+([0-9]) ]] && [[ $2 == ?(-)+([0-9]) ]]
    then
        echo "Invalid arguments. Did you include a slash at the start of the path (if the file name consists only of the numbers)? Well, try it again. Terminating script..."
        exit
    elif [[ $1 == ?(-)+([a-z]) ]] && [[ $2 == ?(-)+([a-z]) ]]
    then
        echo "Only characters in the arguments. Is this some sort of a joke? If you have tried some sick hexadecimal format of number (just A-F), it will not work, mate. Terminating script..."
        exit
    fi

    if [[ $1 == ?(-)+([0-9]) ]] 
    then
        AMOUNT=$1
        PATHX=$2
    else
        AMOUNT=$2

        if [[ $AMOUNT != ?(-)+([0-9]) ]]            
        then
            echo "No argument with number/numbers-only as an amount of files to create. Terminating script..."
            exit
        fi

        PATHX=$1
    fi

else
    if [[ $# -eq 1 ]]
    then
        if [[ $1 == ?(-)+([0-9]) ]]
        then
            AMOUNT=$1
            PATHX=$(pwd)
        else
            echo "Please, include the argument with an amount of files to create. Terminating script..."
            exit
        fi

    else
        echo "2 Arguments are expected. Terminating script..."
        exit
    fi
fi

if [[ $PATHX != /* ]]
then
    if [[ "$PATHX" == "." ]] || [[ "$PATHX" == ".." ]] || [[ "$PATHX" == "./"* ]] || [[ "$PATHX" == "../"* ]]
    then
        true
    else
        PATHX=$(echo "/$PATHX")     
    fi      
fi

if [[ $PATHX != */ ]]
then
    PATHX=$(echo "$PATHX/")
fi

请原谅这种格式,但是没有在代码示例中添加说明,只是一团糟的文本...总之,谢谢你们,Guys,任何输入。

以撒

它可以简化为:

[[ $# -gt 2           ]] && { echo "Incorrect number of arguments";  exit 1; }
[[ $1 == ?(-)+([0-9]) ]] || { echo "First argument is not a number"; exit 2; }
[[ $2 == ?(-)+([0-9]) ]] && { echo "Both arguments are numbers";     exit 3; }
[[ $2 == +([a-z])     ]] || { echo "File name must be only letters"; exit 4; }
[[ ! -d $2            ]] && { echo "Path does not exist";            exit 5; }

n=$1
p=${2:-$PWD}           # Use the PWD if path is missing.

if [[ $p != */ ]]; then
    echo "Missing trailing slash; correcting";
    p+=/
fi
if [[ $p != /* ]]; then
    echo "Missing leading slash; correcting";
   [[ $p == @(.|..|./*|../*) ]] || p=/"$p"
fi

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从命令行检查Xcode版本

来自分类Dev

如何从命令行检查Spark配置?

来自分类Dev

如何仅从命令行检查安全更新?

来自分类Dev

如何仅从命令行检查安全更新?

来自分类Dev

从命令行检查锁定键状态

来自分类Dev

如何从命令行检查Cakephp版本?

来自分类Dev

从命令行检查PDF文件版本

来自分类Dev

如何从命令行将参数输入到 Jenkins perl 脚本中?

来自分类Dev

从命令行C ++获取文件作为输入

来自分类Dev

从命令行脚本使用PyCharm检查

来自分类Dev

从命令行脚本使用PyCharm检查

来自分类Dev

从命令行将参数传递给python脚本

来自分类Dev

从命令行检查实木复合地板

来自分类Dev

如何仅从命令行检查JavaScript代码是否存在语法错误?

来自分类Dev

如何从命令行检查可用的网络摄像头?

来自分类Dev

如何从命令行检查是否已经登录perforce

来自分类Dev

如何从命令行检查Android开发人员工具版本?

来自分类Dev

如何从命令行检查可用的网络摄像头?

来自分类Dev

如何从命令行检查交换是否处于活动状态?

来自分类Dev

从命令行检查是否少于可用磁盘空间

来自分类Dev

如何从命令行检查音频文件是否有声音

来自分类Dev

如何从命令行检查FFMpeg是否正在积极播放音频?

来自分类Dev

如何从命令行检查Mercurial标签是否已经存在?

来自分类Dev

如何通过传递脚本名称作为参数从命令行运行不同的python脚本

来自分类Dev

从命令行检测VoIP呼叫-Wireshark

来自分类Dev

如何从命令行检索推文?

来自分类Dev

如何从命令行检索推文?

来自分类Dev

从命令行检测“不良”图像

来自分类Dev

如何从命令行检索会话ID?

Related 相关文章

热门标签

归档