Arrays Bash将列表附加到数组的结束元素
我试图将一个列表添加到已经包含元素的bash数组的末尾,该数组包含{a..z},我想将{0..9}添加到该列表的末尾,我尝试了+=但这不起作用。在我的情况下,它会清除该数组Arrays Bash将列表附加到数组的结束元素,arrays,bash,list,append,Arrays,Bash,List,Append,我试图将一个列表添加到已经包含元素的bash数组的末尾,该数组包含{a..z},我想将{0..9}添加到该列表的末尾,我尝试了+=但这不起作用。在我的情况下,它会清除该数组 while [ ! $# -eq 0 ] # Argument selector for CLI input do case "$1" in --num | -n) chars=( {0..9} ) ;;
while [ ! $# -eq 0 ] # Argument selector for CLI input
do
case "$1" in
--num | -n)
chars=( {0..9} )
;;
--char | -c)
chars=( {a..z} )
;;
--upper-char | -C)
chars=( {A..Z} )
;;
--help | -h)
echo "Type the program name with an argument -n for numbers -c for lowercase char and -C for uppercase"
exit
;;
esac
case "$2" in
--num | -n)
chars[${#chars[@]}]=( {0..9} )
;;
--char | -c
chars[${#chars[@]}]=( {a..z} )
;;
--upper-char | -C)
chars[${#chars[@]}]=( {A..Z} )
;;
esac
shift
done
我希望在研究了如何追加列表后,再做第三个case语句,最好的情况是不必每次添加项目时都对数组进行硬编码。追加到数组中确实可以使用+=
。
对您来说,由于代码中的其他错误,它似乎被覆盖了。这应该起作用:
while [ ! $# -eq 0 ] # Argument selector for CLI input
do
case "$1" in
--num | -n)
chars=( {0..9} )
;;
--char | -c)
chars=( {a..z} )
;;
--upper-char | -C)
chars=( {A..Z} )
;;
--help | -h)
echo "Type the program name with an argument -n for numbers -c for lowercase char and -C for uppercase"
exit
;;
esac
case "$2" in
--num | -n)
chars+=( {0..9} )
;;
--char | -c)
chars+=( {a..z} )
;;
--upper-char | -C)
chars+=( {A..Z} )
;;
esac
shift 2
done
当它不适合你的时候,
我怀疑你刚刚换了shift
,而不是shift 2
,
并期望script.sh-c-n
将为您提供a..z
和0..9
,
但它只给出了0..9
。
发生的事情发生在一个班次之后
,
在循环中仍有一个参数需要处理,
因此在下一次迭代中,chars
被替换为chars=(…)
语句
我建议采用以下替代方案:
chars=()
while [ $# != 0 ]; do
case "$1" in
--num | -n)
chars+=( {0..9} )
;;
--char | -c)
chars+=( {a..z} )
;;
--upper-char | -C)
chars+=( {A..Z} )
;;
--help | -h)
echo "Type the program name with an argument -n for numbers -c for lowercase char and -C for uppercase"
exit 1
;;
esac
shift
done
附加到一个数组确实适用于+=
。
对您来说,由于代码中的其他错误,它似乎被覆盖了。这应该起作用:
while [ ! $# -eq 0 ] # Argument selector for CLI input
do
case "$1" in
--num | -n)
chars=( {0..9} )
;;
--char | -c)
chars=( {a..z} )
;;
--upper-char | -C)
chars=( {A..Z} )
;;
--help | -h)
echo "Type the program name with an argument -n for numbers -c for lowercase char and -C for uppercase"
exit
;;
esac
case "$2" in
--num | -n)
chars+=( {0..9} )
;;
--char | -c)
chars+=( {a..z} )
;;
--upper-char | -C)
chars+=( {A..Z} )
;;
esac
shift 2
done
当它不适合你的时候,
我怀疑你刚刚换了shift
,而不是shift 2
,
并期望script.sh-c-n
将为您提供a..z
和0..9
,
但它只给出了0..9
。
发生的事情发生在一个班次之后
,
在循环中仍有一个参数需要处理,
因此在下一次迭代中,chars
被替换为chars=(…)
语句
我建议采用以下替代方案:
chars=()
while [ $# != 0 ]; do
case "$1" in
--num | -n)
chars+=( {0..9} )
;;
--char | -c)
chars+=( {a..z} )
;;
--upper-char | -C)
chars+=( {A..Z} )
;;
--help | -h)
echo "Type the program name with an argument -n for numbers -c for lowercase char and -C for uppercase"
exit 1
;;
esac
shift
done
现在可以在--num |-n)chars+=({0..9})中添加额外的case“$2”;;——char |-c)chars+=({a..z});;--上字符|-C)chars+=({A..Z});;esac案例“$3”在--num |-n)chars+=({0..9})中;;——char |-c)chars+=({a..z});;--上字符|-C)chars+=({A..Z});;esac shift 3 done’我还将第一部分更改为附加部分,而不是您建议的相等部分,谢谢@user3019354您不需要3个大小写
,一个就足够了,就像我上次建议的那样。现在添加额外的大小写就可以了--num |-n)chars+=({0..9})中的“case”$2”;;——char |-c)chars+=({a..z});;--上字符|-C)chars+=({A..Z});;esac案例“$3”在--num |-n)chars+=({0..9})中;;——char |-c)chars+=({a..z});;--上字符|-C)chars+=({A..Z});;esac shift 3 done’我还将第一部分更改为附加部分,而不是您建议的相等部分,谢谢@user3019354您不需要3个案例
,正如我上次建议的那样,一个就足够了。