Using "if" in "ksh" to change "for loop" values....

Using "if" in "ksh" to change "for loop" values....

Post by albertus:^ » Fri, 22 Oct 1999 04:00:00



Hello,

I have a script that contains this(shown below).  My problem is how do I
change
the values in the "for loop" statement? I tried doing another "if"
statement
but unluckilly I was unsuccessful. Needs some help.

Thanks in advance.

albertus:)

A part of my script.
####
if [[ $1 == "file1" && $2 == "name1" && $# == 2 ]]
then
  some commands.

  var=`commands`

  #If var=A, then my for loop will be "for i in 1 2 3"
  #If var=B, then my for loop will be "for i in 1 2 3 4 5"
  #If var=C, then my for loop will be "for i in 1 2 3 4 5 6 7 8 9 10
  #and so on...  I need to do this with "only one"
  #"for loop" statement.  Any ideas....

  for i in ???
  do
     some commands
  done

else
  some commands...
fi
###

 
 
 

Using "if" in "ksh" to change "for loop" values....

Post by Charles Dem » Fri, 22 Oct 1999 04:00:00




>Hello,

>I have a script that contains this(shown below).  My problem is how do I
>change
>the values in the "for loop" statement? I tried doing another "if"
>statement
>but unluckilly I was unsuccessful. Needs some help.

>Thanks in advance.

>albertus:)

>A part of my script.
>####
>if [[ $1 == "file1" && $2 == "name1" && $# == 2 ]]
>then
>  some commands.

>  var=`commands`

>  #If var=A, then my for loop will be "for i in 1 2 3"
>  #If var=B, then my for loop will be "for i in 1 2 3 4 5"
>  #If var=C, then my for loop will be "for i in 1 2 3 4 5 6 7 8 9 10
>  #and so on...  I need to do this with "only one"
>  #"for loop" statement.  Any ideas....

>  for i in ???
>  do
>     some commands
>  done

>else
>  some commands...
>fi
>###

How about something like this:

!#/bin/sh
var=$1
if [ $var = "A" ] ; then
    str="1 2 3"
fi
if [ $var = "B" ] ; then
    str="1 2 3 4"
fi
if [ $var = "C" ] ; then
    str="1 2 3 4 5"
fi
echo $str
for i in $str ; do
    echo $i
done

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.


 
 
 

Using "if" in "ksh" to change "for loop" values....

Post by Yns » Fri, 22 Oct 1999 04:00:00



> Hello,

> I have a script that contains this(shown below).  My problem is how do I
> change
> the values in the "for loop" statement? I tried doing another "if"
> statement
> but unluckilly I was unsuccessful. Needs some help.

> Thanks in advance.

> albertus:)

> A part of my script.
> ####
> if [[ $1 == "file1" && $2 == "name1" && $# == 2 ]]
> then
>   some commands.

>   var=`commands`

>   #If var=A, then my for loop will be "for i in 1 2 3"
>   #If var=B, then my for loop will be "for i in 1 2 3 4 5"
>   #If var=C, then my for loop will be "for i in 1 2 3 4 5 6 7 8 9 10
>   #and so on...  I need to do this with "only one"
>   #"for loop" statement.  Any ideas....

>   for i in ???
>   do
>      some commands
>   done

> else
>   some commands...
> fi
> ###

I would have thought you could do something like:

# set a var containing values
# e.g.
for_values='1 2 3'

# then call for loop
# e.g.
for i in `echo $for_values`
do
  <commands>
done

                --Yunus
-------------------------------------------------------------
"Don't be suckered in by the comments - debug only the code."
To reply by e-mail replace 'lovelyspam' with 'Yunus'.
-------------------------------------------------------------

 
 
 

Using "if" in "ksh" to change "for loop" values....

Post by Pierre-Michel Anse » Fri, 22 Oct 1999 04:00:00


The solutions given above should work fine.

You could also do a little bit shorter  (don't need extra var) :

case $var in
A) set 1 2 3;;
B) set 1 2 3 4 5;;
C) set 1 2 3 4 5 6 7 8 9 10;;
*) # default case ;;
esac

# then call for loop
for i
do
  <commands>
done

On the other side, if you need to work on the initial positionnal
parameters, that solution is not as good as the others as you
would have to save them before.

PM.

"albertus:^)" a crit :

Quote:> Hello,

> I have a script that contains this(shown below).  My problem is how do I
> change
> the values in the "for loop" statement? I tried doing another "if"
> statement
> but unluckilly I was unsuccessful. Needs some help.

> Thanks in advance.

> albertus:)

> A part of my script.
> ####
> if [[ $1 == "file1" && $2 == "name1" && $# == 2 ]]
> then
>   some commands.

>   var=`commands`

>   #If var=A, then my for loop will be "for i in 1 2 3"
>   #If var=B, then my for loop will be "for i in 1 2 3 4 5"
>   #If var=C, then my for loop will be "for i in 1 2 3 4 5 6 7 8 9 10
>   #and so on...  I need to do this with "only one"
>   #"for loop" statement.  Any ideas....

>   for i in ???
>   do
>      some commands
>   done

> else
>   some commands...
> fi
> ###

 
 
 

Using "if" in "ksh" to change "for loop" values....

Post by Ken Pizzi » Fri, 22 Oct 1999 04:00:00



Quote:>How about something like this:

>!#/bin/sh
>var=$1
>if [ $var = "A" ] ; then
>    str="1 2 3"
>fi
>if [ $var = "B" ] ; then
>    str="1 2 3 4"
>fi
>if [ $var = "C" ] ; then
>    str="1 2 3 4 5"
>fi
>echo $str
>for i in $str ; do
>    echo $i
>done

I had a similar thought, but would have used "case" instead:
  #! /bin/sh
  case $1 in
    A) str='1 2 3' ;;
    B) str='1 2 3 4' ;;
    C) str='1 2 3 4 5' ;;
    *) str='oops' ;;
  esac
  for i in $str ; do
    echo $i
  done

                --Ken Pizzini