Simple LOOP script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple LOOP script help
# 1  
Old 07-29-2009
Simple LOOP script help

I am trying to create a simple ksh loop script. What I've pasted below is not working. Any help is appreciated.

typeset -i count
typeset -i tcount
count=0
tcount=0
while $tcount >= 11
do
print "\$count is $count"
pwd
ls -l
sleep 30
$(( $tcount = $count + 1 ))
$count=$tcount
done
# 2  
Old 07-29-2009
Loop

Well, you set the variable to 0 and then loop while it is >= 11.
It will, quite correctly, loop exactly 0 times.
Try chaning the >= to <= and see what happens.
# 3  
Old 07-29-2009
thanks.. didn't work

I tried the change but it didn't help.. here is the result. Any other idea's... Thanks for your help with this.

$ cat maldo_sleep
typeset -i count
typeset -i tcount
count=0
tcount=0
while $tcount <= 11
do
print "\$count is $count"
pwd
ls -l
sleep 30
$(($tcount=$count+1))
$count=$tcount
done
$ ./maldo_sleep
./maldo_sleep[7]: 0: not found
$
# 4  
Old 07-30-2009
Quote:
Originally Posted by musikman65
I tried the change but it didn't help.. here is the result. Any other idea's... Thanks for your help with this.

$ cat maldo_sleep
typeset -i count
typeset -i tcount
count=0
tcount=0
while $tcount <= 11
do
print "\$count is $count"
pwd
ls -l
sleep 30
$(($tcount=$count+1))
$count=$tcount
done
$ ./maldo_sleep
./maldo_sleep[7]: 0: not found
$
Hi!

There are actually two errors in your script:
  1. testing a variable doesn't work that way. You have to use test - external or built-in, I don't remember how this is in ksh - hence, I'll show the "old-fashioned way" :-) see your ksh manual page, look for keyword test.
  2. you are using < in your test. for shell, less-than means input redirection from a file
So, this should work:
Code:
    while test $tcount -le 11
    # ... your code here
    done

I also don't quite understand the way incrementation of tcount is done. Simple
Code:
tcount=$(($tcount+1)) would do.

Regards,

pen

Last edited by pen; 07-30-2009 at 02:00 AM.. Reason: syntax error
# 5  
Old 08-03-2009
Loop

Why not something simple, like
for foo in 0 1 2 3 4 5 6 7 8 9 10 ; do
echo "Count is ${foo} "
pwd
ls -l
sleep 30
done


if you are going to iterate over a known and manageable range like this, for MAY be more appropriate than while.
# 6  
Old 08-03-2009
A sample ksh loop :
Code:
$ cat loop.ksh
typeset -i count=1
typeset -i limit=10

while (( count <= limit ))
do
   echo "count=$count"
   (( count += 1 ))
done

$ loop.ksh
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
gssjgu:/g/g00k00/gssjgu/TMP>

With ksh93 you can also do :
Code:
$ cat for_loop.ksh 
typeset -i limit=10

for (( count=1; count<=limit; ++count ))
do
   echo "count=$count"
done

$ for_loop.ksh
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
$

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with simple bash script involving a loop

Dear unix wizards, I'd be very grateful for your help with the following. I have a hypothetical file (file.txt) with three columns: 111 4 0.01 112 3 0.02 113 2 0.03 114 1 0.04 115 1 0.06 116 2 0.02 117 3 0.01 118 4 0.05 Column 2 consists of pairs of integers from 1 to 4 (each... (5 Replies)
Discussion started by: aberg
5 Replies

2. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

3. Shell Programming and Scripting

simple while loop script to check a connection

Hi, I am completely new to shell scripting. Basically I am wanting to create a simple while loop script to check if a network connection is available from 'netstat -a' and if its present then output a character to the serial port. I think i am nearly there.. but need some assistance.. this is... (9 Replies)
Discussion started by: zippyzip
9 Replies

4. Shell Programming and Scripting

Simple loop using for

Dear experts, I am writing a bash script. At some point of the program I need to have 'for' loop. For simplicity I tried with some other simple code. The format of the loop is given below. k=51 m=55 for j in {$k..$m};do w=$(($j+2)) z=$(($j+9)) echo "$w, $z" done But my... (4 Replies)
Discussion started by: vjramana
4 Replies

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

6. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

7. Shell Programming and Scripting

A simple (?) loop

I have what I believe is a simple programming question. I have a text file that looks like: mol 1 G:\stereo01.hin block text ... ... ... endmol 1 However, I would like a file that repeats this entire block of text several times over. The lines of text in the middle remain the same for each... (2 Replies)
Discussion started by: red baron
2 Replies

8. Shell Programming and Scripting

simple while loop

i have a script called file2 #!/bin/ksh i=0 while do echo $i >> result.txt i=`expr $i + 1` done echo "***********************" >> result ------------------------------------------------------------------- (10 Replies)
Discussion started by: ali560045
10 Replies

9. Shell Programming and Scripting

simple for loop

i have the following process running in background: when i give "ps -lef" ------------------------------------------------------------------------ user2 user1 user1 user3 user1 user4 user5 user4 user3 user4 user2 user1 user1 user3 user1 user4 (3 Replies)
Discussion started by: ali560045
3 Replies

10. Shell Programming and Scripting

Simple script loop question

Hey all. Thanks in advance for any help you can give, hopefully this is an easy one. I want to create a loop to run a simple performance monitor like vmstat and record it to a file, but have very limited scripting skills (obviously). Starting with this... date >> /var/log/perfmon.log vmstat... (2 Replies)
Discussion started by: mattlock73
2 Replies
Login or Register to Ask a Question