I am trying to iterate all files in a directory with for loop:
#!/bin/bash
myname="bandit24"
cd /var/spool/$myname
echo "Executing and deleting all scripts in /var/spool/$myname:"
for file in /var/spool/bandit24/*; # <----- here
do
if [ "$file" != "." -a "$file" != ".." ];
then
echo "Handling $file"
owner="$(stat --format "%U" ./$file)"
if [ "${owner}" = "bandit24" ]; then
timeout -s 9 60 ./$file
fi
rm -f ./$file
fi
done
But as a result I get the following:
Executing and deleting all scripts in /var/spool/bandit24:
Handling /var/spool/bandit24/*
stat: cannot stat './/var/spool/bandit24/*': No such file or directory
The program is not trying to iterate files in /var/spool/bandit24/ directory, but it tries to iterate /var/spool/bandit24/* file itself. But I want to iterate files in /var/spool/bandit24/ directory. How to do it?
Additional question
What does cd /var/spool/$myname mean (5 line)? As I understand, it is used for specifying the directory we are going to work in, am I right?