I am new in the bash language (GNU / linux) and a question has arisen in an exercise: I have to go through all the files in a directory, and for those that are executable, I save a variable 1, or a 0 in case of not being executable.
I tried to do if
with those files whose extension is "* .exe", but later I read that in gnu / linux extensions are not used to avoid manipulations, but "magic numbers". Then I tried the file -z $nombreFichero
command, but it does not work either.
How can I detect if the file is executable or not? This is the code I've been carrying so far:
#!/bin/bash
fichero=$(mktemp)
for nombre in $(find $1 -size +$2)
do
if [ -x $nombre ];
then
ejecutable=1
else
ejecutable=0
fi
echo "$nombre,${#nombre},'stat -c %u $nombre','stat -c %U $nombre','stat -c %h $nombre','stat -c %Y $nombre','stat -c %A $nombre',$ejecutable " >> $fichero
done
if [ -z "$2" ];
then
for nombre in $(find $1 -size +0)
do
if [ -x $nombre ];
then
ejecutable=1
else
ejecutable=0
fi
echo "$nombre,${#nombre},'stat -c %u $nombre','stat -c %U $nombre','stat -c %h $nombre','stat -c %Y $nombre','stat -c %A $nombre',$ejecutable" >> $fichero
done
fi
cat "$fichero" | sort -k1r
rm "$fichero"