0

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

4
  • can you tell how this code is failing ? [[ -x filename ]] is a perfectly legitimate construct in bash. Would you be using some other shell may be ?
    – MelBurslan
    Commented Feb 16, 2016 at 17:23
  • Rather than for f in $input, try for f in ${input}/*. Making sure the value of $input is a readable directory is an exercise I shall leave for you.
    – DopeGhoti
    Commented Feb 16, 2016 at 18:04
  • why are you looping over $input? On the assumption that the user is entering "a (single) directory path"...
    – Jeff Schaller
    Commented Feb 16, 2016 at 18:10
  • As I see it, the OP's intent is to loop over the contents of the directory specified as the input.
    – DopeGhoti
    Commented Feb 16, 2016 at 18:29

2 Answers 2

0

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                   
4
  • $dr you can replace with your directory. Commented Feb 16, 2016 at 18:23
  • Don't parse the output of ls.
    – DopeGhoti
    Commented 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 found Commented Feb 16, 2016 at 18:34
  • No, seriously, don't parse ls. mywiki.wooledge.org/ParsingLs
    – DopeGhoti
    Commented Feb 16, 2016 at 18:36
0

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.

You must log in to answer this question.