我有一个数组的客户名称,我想用来做一个目录。下面是我运行的代码:

$ echo "$customerArray=( customer1 customer2 customer3 customer4 customer5 )"

for customerName in $( customerArray );
do
    mkdir -p /home/$customerName
    mkdir -p /home/$customerName/outbound
    mkdir -p /home/$customerName/outbound_backup
    mkdir -p /home/$customerName/dropoff
done

有人能向我解释我做错了什么吗?

最佳答案

不能在字符串文本中设置变量,而且我确信$( customerArray )也无效。请尝试以下操作:

customerArray=(customer1 customer2 customer3 customer4 customer5)
for customerName in ${customerArray[@]}
do
    ...
done

您需要将数组变量引用为${customerArray[@]}${customerArray[*]}bash manual更深入地描述了这一点。

10-07 16:17