繁体   English   中英

命令行参数困难

[英]Difficulty with command line parameters

欢迎大家,我需要提示用户输入“ all,long,human或alh”。 我已经开发了一些代码,但是有错误。 我已经使用shellcheck.net来检查我的代码的语法是否合适。

程序本身应提供文件和目录列表(基于收到的命令行参数),并且如果未选择上述选项之一,则还应显示错误消息。

  • “全部”-不要隐藏以开头的条目。
  • “长”-使用长列表格式
  • “人类”-使用长列表格式和人类可读格式的打印尺寸
  • “ alh” –完成以上所有操作

这是我的代码:

read -p "Please enter all, long, human or alh: " userInput

if [ -z "$userInput" ];
then
print f '%s/n' "Input is not all, long human or alh"
exit 1
else 
printf "You have entered %s " "$UserInput"
fi

该代码本身可以工作,但是不会根据所选参数显示任何目录列表。

输出:

请输入所有(长,长或人类): all
您已输入全部

我会用case命令

#!/bin/bash

read -p "Please enter all, long, human or alh: " userInput

case "$userInput" in
    (all) flags=a;;
    (long) flags=l;;
    (human) flags=lh;;
    (alh) flags=alh;;
    (*) printf '%s\n' "Input is not all, long human or alh"
        exit 1;;
esac

ls -$flags

或者,您可以将标志存储在关联数组中:

#!/bin/bash

read -p "Please enter all, long, human or alh: " userInput

declare -A flags
flags=([all]=a
       [long]=l
       [human]=lh
       [alh]=alh
)

flag=${flags[$userInput]}
if [[ -z $flag ]] ; then
   printf '%s\n' "Input is not all, long human or alh"
   exit 1
fi

ls -$flag

暂无
暂无

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

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