1

我有一组客户姓名,我想用它们来制作目录。下面是我正在运行的代码:

$ 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

谁能向我解释我做错了什么?

4

2 回答 2

3

这将是使用 大括号扩展的可行替代方案

customerArray=(customer1 customer2 customer3 customer4 customer5)
for customerName in ${customerArray[*]}
do
  mkdir -p /home/$customerName/{outbound,outbound_backup,dropoff}
done
于 2013-04-17T20:26:50.893 回答
3

您不能在字符串文字内设置变量,我很确定这$( customerArray )也是无效的。请尝试以下操作:

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

您需要将数组变量引用为${customerArray[@]}${customerArray[*]}bash 手册对此进行了更深入的描述。

于 2013-04-17T20:36:34.390 回答