function create_path_do()
{
if [ ! -d $1 ]; then
mkdir $1
fi
}
function create_path()
{
IFS='/'
arr=($1)
for s in ${arr[@]}
do
if [ "$s" = "" ]; then
continue;
else
p=$p/$s
create_path_do $p
fi
done
}
m=$(date +%Y%m)
t=$(date +%Y%m%d%H%M%S)
filename=$t.sql
filepath=/home/wwwbaks/aps/$m
filepath_name=$filepath/$filename
echo $filepath_name
create_path $filepath
echo $filepath_name
输出结果为:
/home/wwwbaks/aps/201412/20141203043459.sql
home wwwbaks aps 201412 20141203043459.sql
感谢修改之后的函数
function create_path()
{
oldifs=$IFS
IFS=/
arr=($1)
IFS=$oldifs
for s in ${arr[@]}
do
if [ "$s" = "" ]; then
continue;
else
p=$p/$s
if [ ! -d $p ]; then
mkdir $p
fi
fi
done
}
原因在于你这个 IFS
由于调用了 create_path
这个 function, IFS
仍然为 "/"
,因此解决办法就是恢复原来的 IFS
:
# Save
local oldifs=$IFS
# function body here
# ...
# Restore
IFS=$oldifs