简体   繁体   English

检测参数数量和传递给bash的值

[英]Detect number of argument and the value passed into the bash

I want to make sure my bash script can correctly detect user's input argument. 我想确保我的bash脚本可以正确检测用户的输入参数。 Specifically, user can only pass 1 or 2 or 3 into the script, otherwise the script will be terminated. 具体来说,用户只能将1或2或3传递给脚本,否则脚本将被终止。 I wrote the following code: 我写了以下代码:

#!/bin/bash

for args in $@
do
    echo $args
done

if [ "$#" != 1 ] && [ "$#" -ne 1 ]; then
        echo "Illegal number of parameters"
        exit 1
fi

This script can only capture when user does not give any input, but cannot check whether user indeed input the number 1 not other values. 该脚本只能在用户不输入任何内容时捕获,而不能检查用户是否确实输入了数字1而不输入其他值。

By the way, I am not sure how to express "input argument can accept number 1 or 2 or 3". 顺便说一下,我不确定如何表达“输入参数可以接受数字1或2或3”。

$# is an integer, so you have to use integer comparison. $#是整数,因此您必须使用整数比较。 You can for example say: 例如,您可以说:

if [ "$#" -ne 1 ]; then
   echo "illegal number of parameters"
   exit 1
fi

To check that the parameter is either 1, 2 or 3, you can use this regular expression (see something related ): 要检查参数是1、2还是3,可以使用以下正则表达式(请参阅相关内容 ):

if [[ ! $1 =~ ^(1|2|3)$ ]]; then
    echo "the number is not 1, 2 or 3"
    exit 1
fi

To express "input argument can accept number 1 or 2 or 3" I would for example say "we can just accept the argument being either 1, 2 or 3". 为了表达“输入参数可以接受数字1或2或3”,我可以说“我们可以接受参数是1、2或3”。

First off you can detect if the string is null or empty simply by doing the following: 首先,您可以通过执行以下操作来检测字符串是空还是空:

if [ -z "$1" ]
  then
  echo "Argument $1 contains nothing"
fi

That I would say is your first step, and will allow you to filter out args that have no content. 我想说这是您的第一步,它将允许您过滤出不包含任何内容的args。

Following on from that, you'd most likely need to do some comparison work on $1, $2 & $3 然后,您很可能需要对$ 1,$ 2和$ 3进行一些比较工作

I'll just check something and come back to this in a moment. 我将检查一下,稍后再返回。

Update 更新

Just had to go find one of my scripts and check something... :-) 只是不得不去找到我的脚本之一并检查一下... :-)

One way I've handled the checking of parms in the past is something like the following 我过去处理过Parm检查的一种方式是如下所示

#!/bin/sh
while [ $# -gt 0 ] || [ "$#" -le 4 ]; do
  case "$1" in
    *[!1-9]*)   echo "Text: $1";;
    *)          echo "Number: $1"
  esac

  case "$2" in
    *[!1-9]*)   echo "Text: $2";;
    *)          echo "Number: $2"
  esac

  case "$3" in
    *[!1-9]*)   echo "Text: $3";;
    *)          echo "Number: $3"
  esac

  shift
done

Basically a simple regex, if I have more than 0 parameters or less than 4 parameters then I allow it through to a case statement, which then checks the content of each parameter. 基本上是一个简单的正则表达式,如果我有0个以上的参数或4个以下的参数,则可以将其允许通过case语句,然后检查每个参数的内容。

This one just has an echo in, but you could just as easy set some flags, and then decide how to continue based on those flags. 这个只是回声,但是您可以轻松设置一些标志,然后根据这些标志决定如何继续。

For simple range checking however, you might just want to use a one liner similar to the following: 但是,对于简单的范围检查,您可能只想使用类似于以下内容的一根衬管:

if [[ $# -gt 0 && $# -lt 4 ]]; then echo "Correct number of parameters"; fi

Again setting a flag to use later rather than echoing the results. 再次设置标志以供以后使用而不是回显结果。

I assume you mean the input can only be 1, 2 or 3, so using a case statement is the best way. 我假设您的意思是输入只能是1、2或3,所以使用case语句是最好的方法。 $1 is the variable that stores your argument, if it is equal to 1 case will execute the code in the block corresponding to the value 1 and so on. $ 1是存储参数的变量,如果等于1,则将在与值1对应的块中执行代码,依此类推。

 case "$1" in
        1) ...
        ;;
        2) ...
        ;;
        3) ...
        ;;
        *) echo "Invalid argument"
        ;;
     esac

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

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