File Looping - Looking for executable files - Need help

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions File Looping - Looking for executable files - Need help
# 1  
Old 05-12-2011
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.
Image


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


3. 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!!!!

4. 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).
# 2  
Old 05-12-2011
Look at the -R and -F option of ls, it may help you.

Code:
man ls

# 3  
Old 05-13-2011
Quote:
Originally Posted by ctsgnb
Look at the -R and -F option of ls, it may help you.

Code:
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.

Last edited by Bob07; 05-13-2011 at 07:52 PM..
# 4  
Old 05-14-2011
Could you please give us an example of expect output ?
# 5  
Old 05-14-2011
Quote:
Originally Posted by ctsgnb
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
# 6  
Old 05-14-2011
Code:
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 ----------

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

This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 05-14-2011
Quote:
Originally Posted by ctsgnb
Code:
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 ----------

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

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping the files for getting the file size information

Am using Linux 3.10 years.txt is input source file which has the list of year like below 2013 2014 2015 2016 Other Input files are XX_TEST1_YR2016_01012018.csv XX_TEST1_YR2015_01012018.csv XX_TEST1_YR2014_01012018.csv XX_TEST1_YR2013_01012018.csv XX_TEST2_YR2016_01012018.csv... (2 Replies)
Discussion started by: weknowd
2 Replies

2. Shell Programming and Scripting

While or For with looping contructs? to create files from contents of a text file

Hi so far I created this script: vi loop.beta.sh for i in `cat extract.filenames.tabc` do echo $i done>$i === This is the original text file. $ more tabc.txt -rwx------- 1 alice staff 1586 2010-11-05 02:27 request-key .conf -rwx------- 1 ted staff 126 ... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

3. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

4. Shell Programming and Scripting

looping through files

I am writing a ksh which has to load 7 files(.dat files) from input directory into oracle tables using sql loader. The process has to take each file at a time and once if it is loaded succesfully using sql loader into oracle tables then the process has to pick next file and load it into oracle... (2 Replies)
Discussion started by: vpv0002
2 Replies

5. Shell Programming and Scripting

Automatically select records from several files and then run a C executable file inside the script

Dear list its my first post and i would like to greet everyone What i would like to do is select records 7 and 11 from each files in a folder then run an executable inside the script for the selected parameters. The file format is something like this 7 100 200 7 100 250 7 100 300 ... (1 Reply)
Discussion started by: Gtolis
1 Replies

6. UNIX for Dummies Questions & Answers

executable files

hello. My question, basically is: what is the definition of unix/linux exec files, or what makes a file executable? More specifically, must a unix source file that was compiled using gcc have exec permissions in order to be considered executable? Is it right to say that a unix/linux exec file... (1 Reply)
Discussion started by: nadavkri
1 Replies

7. Shell Programming and Scripting

Looping through files...

I posted this in the Solaris forum, but I don't think it's platform specific, so I'm posting it here. Here is the situation. We are a company that has been using a professional publishing system, the software is called "ProType". It runs on Solaris 2.4, however it is no longer supported and we... (6 Replies)
Discussion started by: Fred Goldman
6 Replies

8. UNIX for Dummies Questions & Answers

Executable files

This question always confuses me :- Suppose I write a program and compile it on a machine with operating system A and processor B will the exe file run on a machine with operating system A2 but processor B operating system A but processor B2 operating system A2 and processor B2........ ... (9 Replies)
Discussion started by: nervous
9 Replies

9. Shell Programming and Scripting

looping files

Hi, I have a file a.lst which lists all files. as a.dat b.dat c.dat I want to process these files mentioned in the list file in a loop. Say I want to display only the first line of all the files a.dat , b.dat, c.dat. How can I go about it? Please help. (5 Replies)
Discussion started by: dharmesht
5 Replies
Login or Register to Ask a Question