gpt4 book ai didi

linux - Bash 权限被拒绝

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:44 24 4
gpt4 key购买 nike

我是 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”行中用反引号 (``) 包围了路径。这告诉 shell 执行文件(毫无疑问,文件夹中的许多文件都不可执行 - 因此出现错误)。切换到普通引号。

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

关于linux - Bash 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com