![](/img/trans.png)
[英]How do I write a bash script that goes through each directory executes a command in each directory
[英]Write a Bash script which will take a single command line argument (a directory) and will print each entry in that directory
問題:文件夾被視為文件
我的代碼:
#!/bin/bash
for var in $(ls)
do
echo $var
if [ -e $var ]
then
echo "This is a file"
else
echo "This is not a file"
fi
done
echo All Done
根文件夾的當前內容:
-rw-r--r-- 1 stibo stibo 401 Sep 17 2015 id_rsa.pub drwxrwxr-x 24 stibo stibo 4096 Jul 26 09:25 step -rwxr-xr-x 1 stibo stibo 51 Jul 27 12:51 test.txt drwxrwxrwx 2 stibo stibo 4096 Aug 2 10:32 deletionFile -rwxrw-r-- 1 stibo stibo 225 Aug 2 12:32 deletionScript.vi -rw-rw-r-- 1 stibo stibo 235 Aug 2 12:33 logdetails.txt -rw-rw-r-- 1 stibo stibo 123 Aug 2 12:42 path1.txt -rw-rw-r-- 1 stibo stibo 285 Aug 2 16:18 path2.txt drwxrwxr-x 2 stibo stibo 4096 Aug 3 10:14 archival -rw-rw-r-- 1 stibo stibo 0 Aug 3 13:42 ls -rw-rw-r-- 1 stibo stibo 164732 Aug 3 14:11 messages -rw-rw-r-- 1 stibo stibo 164732 Aug 3 14:11 wtmp drwxrwxrwx 2 stibo stibo 4096 Aug 3 14:21 backup -rwxrwxr-x 1 stibo stibo 160 Aug 4 15:34 newScript.vi -rw-rw-r-- 1 stibo stibo 160 Aug 4 15:41 Code.txt -rw-rw-r-- 1 stibo stibo 0 Aug 4 15:43 Details.txt
那里有4個文件夾和12個文件 。
但是,當我運行腳本時,即使其中包含文件夾,我也可以看到所有內容都被視為文件。
您能告訴我我要去哪里了嗎?
您已經測試了-e是否存在給定的條目。 使用-d測試目錄,使用-f測試文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.