我正在嘗試使用一個函式來列印檔案的型別,如果該檔案是一個目錄,它將使用該目錄中的檔案串列遞回呼叫該函式。
然而,當通過ls命令發送時,目錄內的檔案串列$(file -i filename)給了我“無法打開‘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)從終端運行它確實按預期作業并正確識別所有檔案
uj5u.com熱心網友回復:
每次你打電話時printing(),你可以呼叫/執行cd testing......這就是為什么只有第一次的命令作品,那么它不..拉cd testing前/函式外部,或添加內的檔案夾名稱$(file -i $i)為$(file -i testing/$i)
uj5u.com熱心網友回復:
現在通過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 $*
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377409.html
