繁体   English   中英

Bash权限被拒绝

[英]Bash permission denied

我是bash的初学者,我不明白为什么

#! /bin/sh
if ! [ -d $1 ]; then echo This is not a directory && exit 1; fi

for file in `ls $1`; 
do 
    if [ -f `$1/$file` ]; then
        echo $file is a file
    elif [ -d $file ]; then
        echo $file is a directory
    fi
done

给我[ -f $ 1 / $ file ]权限被拒绝

这是因为您已在if的行中用反引号(``)包围了路径。 这告诉外壳程序执行文件(毫无疑问,文件夹中的许多文件都不可执行,因此是错误)。 切换为普通报价。

顺便说一句,在“ for”循环中使用反引号捕获“ ls”的输出是一个坏主意-它会破坏包含空格的文件名(这是完全合法的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM