LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help in display a sequence of number (https://www.linuxquestions.org/questions/programming-9/help-in-display-a-sequence-of-number-589189/)

silverhand 10-03-2007 03:06 PM

help in display a sequence of number
 
I am doing a exercise for using for and while loop to perform from tw0 any variable by using command seq; but it is used for increasable number such as: 3 to 15 or 1 to 10..i was wonder like how about to perform from 15 to 3 or from 10 to 1; such as: using first and last flags...Plz help me to tide off this complex...Thanks for reading

raskin 10-03-2007 03:35 PM

Code:

seq 15 -1 3

jozyba 10-03-2007 03:37 PM

In man seq you see:
Quote:

seq [OPTION]... FIRST INCREMENT LAST
So if 'first' is 15, 'increment' is -1 and 'last' is 3 you get:
Code:

for x in $(seq 15 -1 3); do echo $x; done
likewise 10 to 1:
Code:

for x in $(seq 10 -1 1); do echo $x; done
Or if you're using bash and you want to decrease one at a time, you can use extended brace expansion:
Code:

for x in {15..1}; do echo $x; done
The advantage of using 'seq' is that you can decrement by a value other than one. For instance here we go from 15 to 3 in decrements of 5:
Code:

for x in $(seq 15 -5 3); do echo $x; done

silverhand 10-03-2007 04:14 PM

thanks a lot guys...It got a very quick and nice lesson for my first step to bash script..thanks and cheers


All times are GMT -5. The time now is 01:21 PM.