I'm a beginner with bash and I don't understand why
#! /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 ]
It's because you have surrounded the path with backticks (``) in the "if" line. 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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.