File Looping - Looking for executable files - Need help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

Hello all, I have posted here before and really was blown away by the amount of assistance I received. I was able to finish my homework without a problem! But, yet again, our instructor has given us an assignment a lot of agreed was a bit too advanced for us. I'm in need of some help.

  1. The problem statement, all variables and given/known data:

                                                       Create a shell script to loop through all the files and display 
    

Sequence # Name Size Flag
========== ====================== ========= ==============

Flag should be "Yes" for executable files and "No" for non-executable files.

  1. Relevant commands, code, scripts, algorithms:
    ls, grep, printf, while loop

  2. The attempts at a solution (include all code and scripts):

Here's my sad attempt at it:

s=1

while [ $s -le 10 ]
do
printf "Sequence\n========\n%4d\n" $s
name=`cut -d" " -f1 RevFile | rev`
printf "Name\n===========\n%4s" $name

size=`ls -l | awk -F" " '{print $5}'`
printf "Size\n===========\n$4s" $size

printf "Flag\n==========\n"

let s++

done

WHAT WORKS:

The Sequence has no problem incrementing to the number I indicated. I set it up as a test because I knew the directory we were using contained hundreds of files and I wanted to keep it at a low number. File names are also showing up, but that's it, and even they are having problems lining up correctly under the "=======".

The "rev" command is in place because I actually had a file already set up with the directory in reverse so if I needed file names all I had to do was cut that first field, reverse that, and I had it. I'm sure there's an easier and better method out there, such as awk, but I've allowed frustration of not knowing what to do to get to me.

What is NOT working:

File size isn't showing up as intended and I don't have the flag portion set up. I need to get this thing straightened out first before I start working on the actual fields because right now it's printing as one whole single line and seperating into columns like the instructor is wanting us to do. I gotta be honest here, the guy HAS NOT covered enough material for us to be able to pull something like this off.

So basically I'd like to know what I should be doing in order to line the columns up the way they need to be. I'd be extremely grateful for any and all help!!!!

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Baker College of Jackson, Jackson MI, U.S., Instructor S. Sadiq, LUX 211

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Look at the -R and -F option of ls , it may help you.

man ls

Thanks for the reply.

I did check out those commands and do see how they would be helpful. The problem I am having is getting the columns to line up side by side. This is not something our class has been taught to do ( Our instructor has this tendency to give us homework beyond our knowledge ) so if there are any guides out there that demonstrate how to do something of this nature, I'd love to see them. I've been searching google constantly with no luck.

Could you please give us an example of expect output ?

For some reason this menu/whatever you want to call it isn't lining up no matter what I do, but the output is supposed to be:

Sequence #-------- Name--------- Size---------- Flag (yes or no)
=======-------========----=======-----============
1----------------- -File A--------- --1396---------- --Yes
2-------------------File B--------------300-------------No

This is the best I could do. The -'s there are supposed to be white space. The script needs loop through our home directory, fetch the file name, file size, and determine if the file is executable or not, flagging it as either yes or no. For each file loop the sequence # must also be provided.

The commands aren't the issue now, I've straightened those out, but I cannot get the columns to line up side by side like that. printf was the suggested command here to do all that but everything I write is being output into a single line or the file names or sizes are getting mixed up with the column name.

An example of what I'm writing is:

printf "Sequence\n=========\n%5d" $SEQ
printf "Name\n============\n%5s" $NAME
printf "Size\n=============\n%5s" $SIZE

ls -RFl | grep ^- | sed 's/$/ NO/;s/\* NO/ YES/' | awk '{print NR,$(NF-1),$5,$NF}' OFS="\t"

---------- Post updated at 04:18 PM ---------- Previous update was at 04:15 PM ----------

ls -RFl | sed '/^-/!d;s/$/ NO/;s/\* NO/ YES/' | awk '{print NR,$(NF-1),$5,$NF}' OFS="\t"
1 Like

Awesome. Plugged that in and got them to line up the way they need to be. Thanks so much, will be using this as a future reference.