我正在尝试使用一个函数来打印文件类型,如果文件是一个目录,它将使用该目录中的文件列表递归调用该函数。
然而当通过CCD_ 1命令发送时,目录CCD_;不能打开‘a1’(没有这样的文件或目录(";尽管文件在那里,而且他是从ls.那里得到的
我的测试目录是:
a1 - a directory file, gives (no such file or directory)
bunzip2test.txt - a text file, gives (no such file or directory for some reason)
dir1.tar.gz - a compressed file, gives the correct info
t.txt.gz - a compressed file, gives the correct info
zipping.zip - a zip, gives the correct info
我的代码是:
#! /bin/bash
function main()
{
f=0
printing $@
}
function printing()
{
cFile=($@)
echo "printing all files:"
for i in ${cFile[@]}
do
echo $i "and it's type is: $(file -i $i)"
done
if ((f == 0))
then
f=1
printing $(cd testing && ls)
fi
}
main $*
输出:
testing and it's type is: testing: inode/directory; charset=binary
printing all files:
a1 and it's type is: a1: cannot open `a1' (No such file or directory)
bunzip2test.txt and it's type is: bunzip2test.txt: cannot open `bunzip2test.txt' (No such file or directory)
dir1.tar.gz and it's type is: dir1.tar.gz: application/gzip; charset=binary
t.txt.gz and it's type is: t.txt.gz: application/gzip; charset=binary
zipping.zip and it's type is: zipping.zip: application/zip; charset=binary
从终端运行cd testing && file -i $(ls)
,它确实按预测工作,并正确识别所有文件
每次调用printing()
时,都会调用/执行cd testing
。。。这就是为什么只有第一次命令有效,然后就不起作用了。。将cd testing
拉到函数之前/外部,或者将$(file -i $i)
内的文件夹名称添加为$(file -i testing/$i)
现在通过在再次调用函数之前调用cd directoryname
来解决问题
这是代码:
#! /bin/bash
function main()
{
f=0
printing $@
}
function printing()
{
cFile=($@)
echo "printing all files:"
for i in ${cFile[@]}
do
echo $i "type is: $(file -i $i)"
done
if ((f == 0))
then
f=1
cd testing
printing $(ls)
fi
}
main $*