问题描述
问题:我需要使用declare -a DIR
将用户cmd-line参数(在本例中为目录路径)放置到关联数组中。目录路径将存储在数组中,以供以后在脚本中使用。但是,当用户提供正确的选项&arg时,nothig将存储在DIR
数组中。脚本以./selectDir -d <dir_path> [file1] [file2]
期望的输出:我希望用户选择的目录选择存储在数组“ DIR”中。然后,我希望能够在存储数组的内容后将其打印出来,以便验证存储。
当前代码:
!/bin/bash -x
#
# Description:
# Usage:
#
# Issue: current problem seems to be with the execution of the scipt
# --Syntax ./parseargs -d [$path] [file1] [file2] ... but this is keeps snagging on error
#
totalArgs="$#"
function usageErr()
{
echo 'usage: baseline.sh [ -d path ] file1 [file2]'
echo 'creates or compares a baseline from path '
echo 'default for path is /'
exit 2
} >&2 #rename the error output file
function parseArgs ()
{
# need to figure out how to trigger the conditional
while getopts "c:" MYOPT
do
case "${MYOPT}" in
d) # provide a custom directory
selectPath=YES
DIR+=( "$OPTARG" )
;;
*) # any other option provided will result in error
usageErr
;;
esac
done
shift $((OPTIND-1))
#no arguments? too many?
(( $# == 0 || $# > 2 )) && usageErr
(( ${#DIR[*]} == 0 )) && DIR=( "/" )
}
declare -a DIR
parseArgs # see about passing the cmd args at this point
echo "DIR: ${DIR[*]}"
echo "TEST: $test"
echo "PATH: $selectPath"
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)