简体   繁体   English

验证bash脚本参数

[英]Validate bash script arguments

I am trying to do something like this to make a script to perform backups if they have failed. 我正在尝试做这样的事情,使脚本在失败时执行备份。 I am taking in the environment as argument to the script. 我把环境作为脚本的参数。

The one thing i am unsure on how to do is that i want to verify $1 to only include some predefined values. 我不确定如何做的一件事是我想验证$ 1只包含一些预定义的值。 The predefined values should be something like tst, prd, qa, rpt. 预定义值应该类似于tst,prd,qa,rpt。 Anyone? 任何人?

#!/bin/bash
ENVIRONMENT=$1 
BACKUPDATE=$(date +"%d_%m_%Y")
BACKUPFILE="$ENVIRONMENT".backup."$BACKUPDATE".tar.gz

if [ $1 ==  "" ] 
 then
 echo "No environment specified"
 exit
elif [ -f "$BACKUPFILE" ]; then
   echo "The file '$BACKUPFILE' exists."
else
   echo "The file '$BACKUPFILE' in not found."
   exec touch "$BACKUPFILE"
fi

You can use case : 你可以使用case

case "$1" in
    tst) echo "Backing up Test style" ;;
    prd)
        echo "Production backup"
        /etc/init.d/myservice stop
        tar czf ...
        /etc/init.d/myservice start
        ;;
    qa) echo "Quality skipped" ;;
    rpt)
        echo "Different type of backup"
        echo "This could be another processing"
        ...
        ;;
    *)
        echo "Unknown backup type"
        exit 2
        ;;
esac

Note the double ;; 注意双;; to end each case, and the convenient use of pattern matching . 结束每个案例,并方便地使用模式匹配

Edit: following your comment and @CharlesDuffy suggestion, if you want to have all valid options in an array and test your value against any of them (hence having the same piece of code for all valid values), you can use an associative array : 编辑:在您的注释和@CharlesDuffy建议之后,如果您想在数组中拥有所有有效选项并针对其中任何一个测试您的值(因此对所有有效值具有相同的代码段),您可以使用关联数组

declare -A valids=(["tst"]=1 ["prd"]=1 ["qa"]=1 ["rpt"]=1)
if [[ -z ${valids[$1]} ]] ; then
    echo "Invalid parameter value"
    # Any other processing here ...
    exit 1
fi
# Here your parameter is valid, proceed with processing ...

This works by having a value (here 1 but it could be anything else in that case) assigned to every valid parameter. 这通过为每个有效参数分配一个值(此处为1但在这种情况下可能是其他任何内容)来实现。 So any invalid parameter will be null and the -z test will trigger. 因此任何无效参数都将为null,并且-z测试将触发。

Credits go to him. 积分归他所有。

Depending on how many different values you have, what about a case statement? 根据你有多少不同的值,一个case陈述呢? It even allows for globbing. 它甚至允许通配。

case $1 in
  (John)   printf "Likes Yoko\n";;
  (Paul)   printf "Likes to write songs\n";;
  (George) printf "Harrison\n";;
  (Ringo)  printf "Da drumma\n";;
  (*)      printf "Management, perhaps?\n";;
esac

On another note, if you can you should avoid unportable bashisms like the [[ test operator (and use [ if you can, eg if [ "$1" = "John" ]; then ...; fi .) 另一方面,如果可以的话,你应该避免像[[测试操作员那样[[测试操作员使用[如果你可以,例如if [ "$1" = "John" ]; then ...; fi不可移植的基本原理if [ "$1" = "John" ]; then ...; fi 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM