简体   繁体   English

使用Getopts传递多个强制性标志的问题

[英]Issues Passing Multiple Mandatory Flags Using Getopts

I'm currently working on a script and I'm having a tough time wrapping my head around using multiple flags with mandatory arguments. 我目前正在编写脚本,现在很难使用多个带有强制参数的标志来结束我的工作。 For the sake of brevity, I have scaled it down to two options (even though its still pretty long) 为了简洁起见,我将其缩小为两个选项(即使它仍然很长)

#THE NAME OF THE SCRIPT IS testfind.sh
#!/bin/bash

while getopts :f:d: opt
do
case $opt in
f)                                #With the flag (-f), we run a find command to search
    fOption=$OPTARG               #for all files. The $OPTARG is the directory the user
    find ${fOption} -type f       #gives the command to start the search. Same concept
    ;;                            #with flag (-d) except it runs find on directories.
d)                   
    dOption=$OPTARG
    find ${dOption} -type d
    ;;
esac
done
shift $((OPTIND -1))

If I were to run the command with only one of the two flags, it runs fine. 如果仅使用两个标志之一运行该命令,它将运行正常。 I am piping to wc -l for testing. 我正在管道上wc -l进行测试。 For clarity, I will use the number of files and directories my system shows: 为了清楚起见,我将使用系统显示的文件和目录数:

./testfind.sh -f /home | wc -l            #WORKS FINE, puts out a count of 342
./testfind.sh -d /home | wc -l            #WORKS FINE, puts out a count of 1996
./testfind.sh -d /home -f /home | wc -l   #WORKS FINE, puts out count of 2338, (total of d and f)

Now here's for the flag of 1 and 2 现在是1和2的标志

./testfind.sh -df /home | wc -l           #FAILS, RUNS FIRST FLAG BUT NOT 
                                          #THE SECOND. It will only run whichever
                                          #flag I put in first.

I'm trying to make the last line above ^ work so that it put out the output of d and f at the same time (-df). 我试图使^上方的最后一行起作用,以便它同时输出d和f的输出(-df)。 Is there any way to do this in GetOpts? GetOpts中有什么方法可以做到这一点吗?

Getopts supports taking multiple statements, however it will not take multiple statements with mandatory statements. Getopts支持采用多个语句,但是不会采用强制性语句采用多个语句。 As we saw above, the following did not work: 正如我们在上面看到的,以下内容无效:

./testfind.sh -df /home | wc -l

When we look at all of the flag options we fed getopts, we see the following 当我们查看给getopts提供的所有标志选项时,我们看到以下内容

getopts :f:d: opt

Note: This is where I made the mistake. 注意:这是我犯错的地方。 Both the f and the d flag are expecting a mandatory statement at the end. f和d标志都在结尾处期待强制性语句。 Since getopts does not support this, instead of reading f as a second flag, it is reading it as if it were the mandatory statement itself. 由于getopts不支持此功能,因此与其读取f作为第二个标志,不如将其作为强制性语句本身读取。 To bypass this, I did the following: 为了绕过这一点,我做了以下工作:

#!/bin/bash

while getopts :fd opt  #NOTE: ONLY USE SHORT FLAGS! THIS WAY, GET OPTS
do                     #CAN EASILY CALL ON WHATEVER YOU THROW AT IT.
                       #MULTIPLE MANDATORY ARGUMENTS NOT SUPPORTED BY GETOPTS.
case $opt in
f)                        
    fOption=$OPTARG    #The variable ${2} is where all the power in this
    find ${2} -type f  #Script lies. By default, getOpts is taking every
    ;;                 #Argument you throw at it after the name of the
                       #Script. In this case It's everything after
d)                     #./testfind.sh . For Example, it took -df and set
    dOption=$OPTARG    #-df to $1. The same way, it takes /home and set
    find ${2} -type d  #/home itself to $2. 
    ;;
esac
done
shift $((OPTIND -1))

Everything needed is explained in the comments (to the right of the #). 注释(在#的右侧)中说明了所需的所有内容。 This same concept will now work with any directory and flags you throw at it. 现在,这个相同的概念将适用于您向其抛出的任何目录和标志。 As long as the flags are valid and the directory is valid, it will find whatever you need and print it out in any order you want. 只要标志有效且目录有效,它就会找到您需要的任何内容并按照您想要的任何顺序将其打印出来。 You can even throw ./testfind -dfdfdfdfdfdfdfdf /home and it will print every single flag in the directory you fed it. 您甚至可以抛出./testfind -dfdfdfdfdfdfdfdf / home,它将在您输入的目录中打印每个标志。 Neato aye! Neato赞成!

-Credits to Mark Plotnick for his comment highlighting how getopts was reading the flags with mandatory statements. -感谢Mark Plotnick的评论,强调了getopts如何读取带有强制性语句的标志。 Thanks! 谢谢!

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

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