Help with looping question

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with looping question
# 1  
Old 05-03-2011
Help with looping question

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

1. The problem statement, all variables and given/known data:
I need to write a script that accepts 3 filenames as command line parameters and test if they are executable and print a message saying whether or not they are, and I need to use a loop.


2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
My first attempt works great... but I didn't use a loop

I check to see if you have entered more or less then 3 files to test, displaying errors if you didn't and then test if each file exsists, then test if it is executable.

what my real question is how do I display a different parameter in a loop? I missed the "looping" class as my kid was sick, and we don't use the course book as it's crap.

any help would be great

Code:
if [ $# -le 2 ]
then
        echo "You need to enter 3 files, you only entered $# "
        exit
fi
if [ $# -ge 4 ]
then
        echo "Enter 3 files only, you entered $# "
        exit
fi
if [ -e "$1" ]
then
        echo > /dev/null
else
        echo $1 DOES NOT EXSIST!
fi
if [ -e "$2" ]
then
        echo > /dev/null
else
        echo $2 DOES NOT EXSIST!
fi
if [ -e "$3" ]
then
        echo > /dev/null
else
        echo $3 DOES NOT EXSIST!
exit
fi
if [ -x "$1" ]
then
        echo $1 is executable
else
        echo $1 is not executable
fi
if [ -x "$2" ]
then
        echo $2 is executable
else
        echo $2 is not executable
fi
if [ -x "$3" ]
then
        echo $3 is executable
else
        echo $3 is not executable
fi



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

Baker college, Flint, MI, USA, VScott, intro to shell scripting
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).

---------- Post updated at 10:43 PM ---------- Previous update was at 09:46 PM ----------

i figured it out, just used a for loop, works like a charm

Code:
if [ $# -le 2 ]
then
        echo "You need to enter 3 files, you only entered $# "
        exit
fi
if [ $# -ge 4 ]
then
        echo "Enter 3 files only, you entered $# "
        exit
fi
for i in $1 $2 $3
do
if [ -e "$i" ]
then
        echo > /dev/null
else
        echo ONE OR MORE FILES DO NOT EXSIST!
        exit
fi
done
for j in $1 $2 $3
do
if [ -x "$j" ]
then
        echo $j is executable
else
        echo $j is not executabe
fi
done

Moderator's Comments:
Mod Comment edit by bakunin: Congrats on figuring that one out. To celebrate the occasion i ordered a ton of brand new CODE-tags and used two pairs to decorate your code with them. A few more (a gazillion or so) i left here for your own usage as a present - use them wisely. ;-))

Last edited by bakunin; 05-04-2011 at 04:59 AM.. Reason: Code tags, *sighs*
# 2  
Old 05-04-2011
Glad you figured it out on your own!!

Two comments:
First, your user might appreciate knowing which file(s) don't exist by specifically naming it/them in your message. In the case of three inputs that's not too big of a task for the user to track down, but for a real-world programme, with hundreds or thousands of files to check, I would think it would be advantageous to have a list of specifically what doesn't exist.

Secondly, you might also want to look into using the not (!) operator with your logic:
Code:
if [ -e "$i" ]
 then
 echo > /dev/null
 else
 echo ONE OR MORE FILES DO NOT EXSIST!
 exit
 fi

as it would help you to eliminate the echo > /dev/null. Psuedo code:
Code:
   if ! exists $f
   then
      echo "file $f does not exist"
   end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping

Hi evryone i need a help . i have a file xcv.the content is : accelerate i want a script which will run 1000 times in loop and changing the value to accelerate to acceler in 1st loop and in 2nd loop it will be again accelerate and so on . (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

2. Shell Programming and Scripting

Looping

Hi, Now I have written a script which sorts the records in the file and splits them according to some condition. Now, I need to modify the script so that it reads all the files one after the other and does the sorting & splitting. Pls help me in reading all the files in a directory and... (8 Replies)
Discussion started by: Sunitha_edi82
8 Replies

3. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

4. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

5. Shell Programming and Scripting

looping search question

This is my requirement-- I have a list file that contains a list of 50 words. eg word1 word2 word3 I want to search/grep for each of those words in a directory/and subdirectories and list the file in which that word exists. Ne ideas for script/command? I know grep -r <pattern>... (3 Replies)
Discussion started by: alfredo123
3 Replies

6. Shell Programming and Scripting

Looping question

Hi, I have series of data stored in a variable xyz: (between 0 and 100) example: 20 45 98 21..... I need to find if there is/are any occurance of data > 95 Not sure what kind of looping is required to check. Please help. thanks (2 Replies)
Discussion started by: hemangjani
2 Replies

7. Shell Programming and Scripting

Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following: my @LAYOUT = ( {SQL_1 => "select count (*) FROM prospect WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy') and tiff_filename is not null ... (2 Replies)
Discussion started by: kregh99
2 Replies

8. Shell Programming and Scripting

nested looping question

Hi, I'm having some trouble with the syntax in constructing a simple nested 'for' loop. My code is as follows: #!/bin/bash dir1="fred flume haystack" for dir2 in ${dir1} do fred="1 2 3" flume="a b c" ... (7 Replies)
Discussion started by: Sn33R
7 Replies

9. UNIX for Dummies Questions & Answers

looping question

I am writing a simple script and want to keep the user in a fuction until they are ready to get out. For some (probably stupid) reason, it doesn't seem to be working. You guys see anything that I'm overlooking? crsd() {until do /home/wcs3611.crsdtmp.sh echo 'run another? \c' ... (1 Reply)
Discussion started by: hedrict
1 Replies

10. UNIX for Dummies Questions & Answers

Help with looping

Hi, Actually I have a file which consists data . for eg names. Then I want my sql query to read this file and produce the output. Currently I am using this FOR EG : FILENAME is NAMES for i in `cat NAMES` { sqlplus -s $CONNECTID << EOF spool rooh set heading off select... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question