Unix FOR LOOPS


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix FOR LOOPS
# 1  
Old 05-21-2008
Unix FOR LOOPS

#!/bin/sh
for x in A B C
do
echo "This is ${x}"
done

This is A
This is B
This is C

Ok, can someone help me out with this? I wrote this simple for loop. What I am trying to accomplish is the following:

For each of the letters in the for loop ( A B C ) I want to echo This is (letter). Simple enough. Next, when I am passing a parameter to this script, if the parameter that I am passing it equals any of the letters in my for loop, I want the loop to start from the parameter given and continue through the end of the loop. I was playing around with continue and break, but not sure if they will or will not accomplish what I want. Any ideas would be GREATLY appreciated Smilie.

Wanted Results:

IN COMMAND LINE: myscript.sh B
Script will get executed and the results would be:
This is C

IN COMMAND LINE: myscript.sh A
This is B
This is C

You get the picture, (hopefully). As always, Thanks!
# 2  
Old 05-21-2008
What should happen if the letter is not one of those? Nothing?

Just keep a state variable.

Code:
seen=false
for x in A B C; do
  case $1 in $x) seen=true; continue;; esac
  $seen || continue
  echo This is $x
done

The construct m || n reads out "m or n" and basically means if m is false, execute n; in other words, it's a convenient shorthand for "if ! m; then n; fi"

false and true are shell commands which simply set their exit code to a false (non-zero) and true (zero) value, respectively. (The convention in the shell is that a zero exit code signifies success, and anything else, failure.)

Last edited by era; 05-21-2008 at 05:44 PM.. Reason: Explain true and false
# 3  
Old 05-21-2008
Smilie

era, you have gotten me out of some tight spots, my friend. Thank you so much for your quick and helpful responses.

P.S. If the letter given as a parameter is not present in the for loop, nothing shall happen. At least I don't think it does. Hmm, let me do more analysis. Either way, thanks a bunch! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. Homework & Coursework Questions

If and Loops

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: In this script you will take a directory as input from the user and change the end of line sequence from a Unix... (1 Reply)
Discussion started by: Pcarson
1 Replies

3. Shell Programming and Scripting

Too many for loops

My apologies for the long post.... I have a structure problem with my ksh script on linux. right now it does what i want (which i know is the point) but its really really ugly and i know there must be a simpler way to achieve my task. The data is a list of usernames. This list will help... (2 Replies)
Discussion started by: maverick72
2 Replies

4. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

5. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

6. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

7. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

8. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

9. UNIX for Advanced & Expert Users

Loops

Can anybody help please. I am trying to right a script which will loop until a certain action has been performed. For example i current have two batch jobs i would like to put into a wait status. Batch Jobs A and B . The script i am trying to get to work is below. jobs="A B" COUNT=0 while... (2 Replies)
Discussion started by: mariner
2 Replies

10. Shell Programming and Scripting

no more loops

Hello Guys I need some help with my script I'm not very good at this, hope you can point me in the right direction. My idea, to write a file with some router commands and use it on a script for me to run on a demand basis, ( or added to the cron, if I get it to work). The below script works,... (5 Replies)
Discussion started by: tony3101
5 Replies
Login or Register to Ask a Question