进入子目录的数组

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
numbchild
帖子: 107
注册时间: 2010-03-16 12:34

进入子目录的数组

#1

帖子 numbchild » 2011-01-30 15:42

假设A目录下面有1-10个子目录,分别名字为1-10,我要在这十个目录下分别建立一个文件,所以要做的其中一个步骤是读取当前目录的子目录列表,然后cd 1/ ; touch file ; cd .. ;
但是这个array数组我不知道怎么弄的,请高手帮忙。
SivaCoHan
帖子: 101
注册时间: 2010-12-11 12:09

Re: 进入子目录的数组

#2

帖子 SivaCoHan » 2011-01-30 17:11

我一点也不会,留名,帮顶
头像
astolia
论坛版主
帖子: 6386
注册时间: 2008-09-18 13:11

Re: 进入子目录的数组

#3

帖子 astolia » 2011-01-30 18:49

使用for循环

代码: 全选

for d in *;do
touch "$d"/file
done
头像
numbchild
帖子: 107
注册时间: 2010-03-16 12:34

Re: 进入子目录的数组

#4

帖子 numbchild » 2011-01-30 20:59

astolia 写了:使用for循环

代码: 全选

for d in *;do
touch "$d"/file
done
可是要怎么读取子目录并排成一个数组呢?我试了ls和find -type d -print都不行,
就是要在${dir_arr[2]}这样应用单个的
头像
astolia
论坛版主
帖子: 6386
注册时间: 2008-09-18 13:11

Re: 进入子目录的数组

#5

帖子 astolia » 2011-01-30 22:05

代码: 全选

a=(*)
echo ${a[0]}
echo ${a[1]}
头像
numbchild
帖子: 107
注册时间: 2010-03-16 12:34

Re: 进入子目录的数组

#6

帖子 numbchild » 2011-01-31 11:53

astolia 写了:

代码: 全选

a=(*)
echo ${a[0]}
echo ${a[1]}
yeah,我会这个引用,但是怎么把子目录变成一个一个的数组元素不会,ls是变成了只有一个元素的,
find好像也不行:
array[*]=`find /path -type d -print `
但是这个只有一个元素
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 进入子目录的数组

#7

帖子 tusooa » 2011-01-31 12:00

代码: 全选

arr=() ; for i in * ; do arr+=("$i") ; done

代码: 全选

arr=() ; while read line ; do arr+=("$line") ; done <<< "$(find -type d)"
都可以的。

代码: 全选

] ls -ld //
回复