简体   繁体   English

Bash权限被拒绝

[英]Bash permission denied

I'm a beginner with bash and I don't understand why 我是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

gives me permission denied on [ -f $1/$file ] 给我[ -f $ 1 / $ file ]权限被拒绝

It's because you have surrounded the path with backticks (``) in the "if" line. 这是因为您已在if的行中用反引号(``)包围了路径。 That's telling the shell to execute the file (and no doubt many of the files in the folder aren't executable - hence the error). 这告诉外壳程序执行文件(毫无疑问,文件夹中的许多文件都不可执行,因此是错误)。 Switch to ordinary quotes. 切换为普通报价。

As an aside, using backticks to capture the output of "ls" in the "for" loop is a bad idea - it will break on filenames that contain spaces (which are perfectly legal). 顺便说一句,在“ for”循环中使用反引号捕获“ ls”的输出是一个坏主意-它会破坏包含空格的文件名(这是完全合法的)。

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

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