Number loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number loop
# 1  
Old 02-28-2015
Number loop

Hello,

I've been trying to create a list of numbers in bash that loops every 10s as follow. So the list will contain 1-5 on start, then 2-6 after 10s and so on. However when it reaches 30 it needs to start back at 1.

Code:
1       2       3       27      28
2       3       4       28      29
3       4       5       29      30
4       5       6       30      1
5       6       7       1       2

This is what i've attempted so far but it of course fails when num is back to 0. Your help is much appreciated. Thanks.

Code:
#!/bin/sh

for (( ; ; ))
do
        for num in {0..26}
        do
                echo $num
                expr $num + 1
                expr $num + 2
                expr $num + 3
                expr $num + 4

        sleep 10
        done
done

# 2  
Old 02-28-2015
Simply maintain 5 numbers (my suggestion is to use an array) and reset them to 1 every time they hit 30:

Code:
typeset -i aiNum[1]=0
typeset -i aiNum[2]=1
typeset -i aiNum[3]=2
typeset -i aiNum[4]=26
typeset -i aiNum[5]=27
typeset -i iArrCnt=1

while <some-termination-condition> ; do
     (( iArrCnt = 1 ))
     while [ $iArrCnt -le 5 ] ; do
          if [ ${aiNum[$iArrCnt]} -lt 30 ] ; then
               (( ${aiNum[$iArrCnt]}++ ))
          else
               (( ${aiNum[$iArrCnt]} = 1 ))
          fi
          echo -n "${aiNum[$iArrCnt]}   "
     done
     echo " "
done

I hope this helps.

bakunin
# 3  
Old 02-28-2015
You say you're using bash but your script's shebang is /bin/sh?
IF it's bash, try
Code:
num=-1
while (:)
        do ((num++))
           echo -e $((num%30+1)) "\n" $(((num+1)%30+1)) "\n" $(((num+2)%30+1)) "\n" $(((num+3)%30+1)) "\n" $(((num+4)%30+1))
           sleep 1
        done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add line number to for loop?

> cat test.sh for t in `cat out.txt` do echo create directory data as "'"$t"';" done > output of out.txt is as below /oracle/SID/data1/file2_1/ /oracle/SID/data1/file1_1/ /oracle/SID/data1/file5_5/ /oracle/SID/data1/file_9/ /oracle/SID/data1/file_2/ when i run my test.sh... (6 Replies)
Discussion started by: crazy_max
6 Replies

2. Shell Programming and Scripting

For loop for number of files in a folder

Hi All, Need a for loop which should run for number of files in a folder and should pass the file name as parameter to another shell script for each loop. Please help me. Thanks. (2 Replies)
Discussion started by: chillblue
2 Replies

3. Shell Programming and Scripting

loop to display whatever number use types

sorry couldnt think of a proper title lol ok i have the following script it asks the user how many html tags they want. Im not sure how to display 2 tags if the user eneters the want only 2 tags tags as in <p></p> or <h1></h1> read -p "How many tags" tags1 if then echo "<$tags1>... (3 Replies)
Discussion started by: gangsta
3 Replies

4. Shell Programming and Scripting

How do I number my for loop outputs with this method?

Currently I am outputting users and I want to number them starting with 1... grep name list.txt | awk -F"=" '{ print $2 }' | while read user; do echo -e "1\t|$user" Currently I have: 1 | john 1 | amy 1 | max I want it to look like 1 | john 2 | amy 3 | max (2 Replies)
Discussion started by: etranman1
2 Replies

5. Shell Programming and Scripting

Compiling n number of SQL files in loop

Trying to compile all SQL files using a shell script. But the below code is not working. Below Code works fine when for loop is not there(commenting line no: 1,2 and 9). 1. sq_lfile=`ls *.sql` 2. for current_sql_file in $sql_file 3. do 4. sqlplus uname/pass@Service>>SQLLOG << -ENDOFSQL... (3 Replies)
Discussion started by: Dip
3 Replies

6. Shell Programming and Scripting

print number pyramid with for loop in unix

How can I print number pyramid with for loop(not while only for) in unix like: 1 22 333 4444 55555 ---------- Post updated at 09:09 AM ---------- Previous update was at 09:07 AM ---------- I forgot it is in ksh...I wrote a script in bash but it is nt wrkng in ksh... bash script... (12 Replies)
Discussion started by: joshilalit2004
12 Replies

7. Shell Programming and Scripting

how to add Loop number to variable name?

Hi there, I need to add a sequence of numbers to existing variables within the script to be able to call the variable automatically. Somehow it's just printing the loop number. Here's what I've got so far. :eek: #!/bin/sh FOLDER1=foo FOLDER2=bar FOLDER3=beer for ((a=1; a <= 3 ; a++))... (2 Replies)
Discussion started by: siul0_0
2 Replies

8. Shell Programming and Scripting

loop number of records.

Initially i store some files into anothe file Y. Now i want read the contents of file Y one by one do some check on each file. i,e Open file Y (contains multiple files) First read a file , do some check on that individual file.If that file satisfies teh condition put it in another file. Now... (1 Reply)
Discussion started by: vasuarjula
1 Replies

9. Shell Programming and Scripting

changing filenames in a directory to a number within a loop

hey guys. i'm new to shell scripting but not new to programming. i want to write a script that will take all the files in the current directory that end with a particular filetype and change all their names to a number in order. so, it would take all the jpg files and sort them in alphabetical... (30 Replies)
Discussion started by: visitorQ
30 Replies

10. Programming

forking n number of processes in a loop and not 2^n

Hi, Am working on linux. while forking in a loop how to avoid the child process from forking..for example int n = 5; pid_t pid; pid_t ch_pid; /*exactly wanted to create n processes and not 2^n processes*/ for(i = 0; i < n;i++) { if(pid = fork()) { /* parent... (4 Replies)
Discussion started by: rvan
4 Replies
Login or Register to Ask a Question