Bash脚本-getopts中的最后一个案例未被读取

Bash脚本-getopts中的最后一个案例未被读取,bash,shell,arguments,getopts,Bash,Shell,Arguments,Getopts,我有以下bash脚本 #!/bin/bash id="" alias="" password="" outputDirectory="" extension="" function ParseArgs() { while getopts "t:a:p:f:r:o:e" arg do case "$arg" in t) id=$OPTARG;; a) alias="$OPTARG";; p) password="$OPTARG";; f) folderPath="$OPTARG";; r) r

我有以下bash脚本

#!/bin/bash

id=""
alias=""
password=""
outputDirectory=""
extension=""

function ParseArgs()
{
while getopts "t:a:p:f:r:o:e" arg
do
case "$arg" in
t)
id=$OPTARG;;
a)
alias="$OPTARG";;
p)
password="$OPTARG";;
f)
folderPath="$OPTARG";;
r)
relativeFolderPath="$OPTARG";;
o)
outputDirectory="$OPTARG";;
e)
extension="$OPTARG";;
-)      break;;
esac
done
}

ParseArgs $*

echo "Getting all input files from $folderPath"
inputFiles=$folderPath/*

echo "Output is $outputDirectory"
echo "Extension is $extension"
if [[ $extension != "" ]]
then
    echo "Get all input files with extension: $extension"
    inputFiles = $folderPath/*.$extension
fi

for file in $inputFiles
do
    echo "Processing $file"
done
出于某种原因,如果我使用最后一个参数(-e),它就不会被读取。例如,我在下面得到了相同的输出,不管有没有最后一个参数(-e xml),我通过包括outputDirectory来测试它,以确保它确实被读取

sh mybashscript.sh -t 1 -a user -p pwd -o /Users/documents -f /Users/documents/Folder -r documents/Folder/a.xml -e xml
Getting all input files from /Users/dlkc6428587/documents/ResFolder
Output is /Users/documents
Extension is 
Processing /Users/documents/Folder/a.xml
Processing /Users/documents/Folder/b.xml

真奇怪,有人知道我做错了什么吗?谢谢。

您没有指出,
-e
在调用
getopts
时在参数后面加一个冒号:

while getopts "t:a:p:f:r:o:e:" arg

另外,您应该像这样调用函数

ParseArgs "$@"
以确保正确处理任何包含空格的参数


最后,
inputFiles
应该是一个数组:

inputFiles=( "$folderPath"/*."$extension" )

for file in "${inputFiles[@]}"
do
    echo "Processing $file"
done

您没有指出,
-e
在调用
getopts
时在参数后面加上冒号来接受参数:

while getopts "t:a:p:f:r:o:e:" arg

另外,您应该像这样调用函数

ParseArgs "$@"
以确保正确处理任何包含空格的参数


最后,
inputFiles
应该是一个数组:

inputFiles=( "$folderPath"/*."$extension" )

for file in "${inputFiles[@]}"
do
    echo "Processing $file"
done

学习使用
set-x
,以便查看变量使用的值。祝你好运。学会使用
set-x
,这样你就可以看到变量使用了什么值。祝你好运。谢谢你,额外的:成功了。关于数组,我得到的输出中,$file只是字符串…/folder/*.xml,而不是实际的xml文件名。关于数组,我得到的输出中,$file只是字符串…/folder/*.xml,而不是实际的xml文件名。