Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:如何正确使用getops解析参数中的数据?_Bash_Shell_Getopts - Fatal编程技术网

Bash:如何正确使用getops解析参数中的数据?

Bash:如何正确使用getops解析参数中的数据?,bash,shell,getopts,Bash,Shell,Getopts,我需要创建一个脚本 脚本必须接受以下命令行选项:-n account name,以及-r accounts of accounts。 执行后,脚本必须根据帐户名和帐户数创建一批用户帐户 例如: /usr/local/bin/batch_user-n bob-r 5应创建用户帐户bob1、bob2、bob3、bob4、bob5 这是我第一次使用getopts,在理解如何使用它时遇到了一些问题,以下是我到目前为止所写的内容: #!/bin/bash usage () { $0 -n usernanm

我需要创建一个脚本

脚本必须接受以下命令行选项:
-n account name
,以及
-r accounts of accounts
。 执行后,脚本必须根据帐户名和帐户数创建一批用户帐户

例如:
/usr/local/bin/batch_user-n bob-r 5
应创建用户帐户bob1、bob2、bob3、bob4、bob5

这是我第一次使用
getopts
,在理解如何使用它时遇到了一些问题,以下是我到目前为止所写的内容:

#!/bin/bash
usage () { $0 -n usernanme -r number of creations };
while getopts ":n:r:" arg; do
case "${arg}" in
n)
n=${OPTARG}
;;
r)
r=${OPTARG}
;;
esac
done
首先,我不确定如何在第一个选项(-n)之后解析数据,然后如何在编号(-r)上运行以创建用户? 提前感谢

您可以使用:

#!/bin/bash

usage () { echo "$0 -n usernanme -r number of creations"; }

while getopts ":n:r:" arg; do
   case "${arg}" in
      n) n=${OPTARG} ;;
      r) r=${OPTARG} ;;
   esac
done

for ((i=0; i<r; i++)); do
   echo "Adding user: ${n}${i}"
done
#/bin/bash
用法(){echo“$0-n usernanme-r创建数”;}
而getopts“:n:r:”arg;做
中的大小写“${arg}”
n) n=${OPTARG};;
r) r=${OPTARG};;
以撒
完成
对于((i=0;i