I'm currently working on a homework assignment and I need to take a given directory path and from that list the files and directories in it. While also including if it's executable or not. I'm also restricted by not being allowed to use any other languages except bash.
My original thought was to use ll
and cut
to get what I needed but I couldn't seem to get it to work. Then I was thinking I could use something like (not working, just an idea)
read input
for f in $input
do
if [[ -x "$f" ]]
then
echo "$f is executable"
else
echo "$f is not executable"
fi
done
I need the output to be something like and I'm not sure how to get there
file-name1 is executable
file-name2 is not executable
directory1 is executable
2 Answers 2
Try like
my=($(ls -la $dr |awk {'print 9ドル'}))
echo ${my[@]}
for i in "${my[@]}"
do
if [[ -x "$i" ]]
then
echo "File '$i' is executable"
else
echo "File '$i' is not executable or found"
fi
done
-
$dr you can replace with your directory.Piyush Jain– Piyush Jain2016年02月16日 18:23:50 +00:00Commented Feb 16, 2016 at 18:23
-
Don't parse the output of
ls
.DopeGhoti– DopeGhoti2016年02月16日 18:28:52 +00:00Commented Feb 16, 2016 at 18:28 -
tmp]$ ./test.sh . .. 1 2 3 4 5 a b c d e File '.' is executable File '..' is executable File '1' is not executable or found File '2' is not executable or found File '3' is not executable or found File '4' is not executable or found File '5' is not executable or found File 'a' is not executable or found File 'b' is not executable or found File 'c' is not executable or found File 'd' is not executable or found File 'e' is not executable or foundPiyush Jain– Piyush Jain2016年02月16日 18:34:32 +00:00Commented Feb 16, 2016 at 18:34
-
No, seriously, don't parse
ls
. mywiki.wooledge.org/ParsingLsDopeGhoti– DopeGhoti2016年02月16日 18:36:18 +00:00Commented Feb 16, 2016 at 18:36
You're taking a directory and then checking to see whether the directory itself is executable, and not looking into its contents as you want to.
read input
for f in ${input}/*; do
echo -n "$f is "
type=""
if [[ -x "$f" ]]; then
type="executable"
else
type="non-executable"
fi
if [[ -d "$f" ]]; then
type="$type directory"
fi
echo "$type"
done
Making sure the value of $input
is a readable directory is an exercise I shall leave for you.
[[ -x filename ]]
is a perfectly legitimate construct in bash. Would you be using some other shell may be ?for f in $input
, tryfor f in ${input}/*
. Making sure the value of$input
is a readable directory is an exercise I shall leave for you.