#!/bin/bash
#遍历目录下的第一层目录,
dirc="/home"
foreachd(){
for file in $dirc/*
do
if [ -d $file ]
then
echo $file"是目录"
fi
done
}
foreachd $dirc
-------------------------------------------------
#!/bin/bash
# 直接编译目录下的所有目录以及文件
foreachd(){
# 遍历参数1
for file in $1/*
do
# 如果是目录就打印处理,然后继续遍历,递归调用
if [ -d $file ]
then
echo $file"是目录"
foreachd $file #递归调用
elif [ -f $file ]
then
echo $file #输出文件
fi
done
}
# 执行,如果有参数就遍历指定的目录,否则遍历当前目录
if [[ "x$1" == 'x' ]]
then
foreachd "."
else
foreachd "$1"
fi