while loop inc a var help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop inc a var help please
# 1  
Old 03-23-2009
while loop inc a var help please

OK guys I think this is an easy one but i am having a bit of trouble getting a while loop I have to do what I want. I need it to increment a number 1 - 99 but I need it to use 2 digits. So not 1,2,3,4,5...99 but 01,02,03,04,05,06,07...10,11,12,...99. Always using 2 digits.

I tried this:

NUM="1"

while true; do
echo "$NUM"
NUM=$[$NUM + 1 ]

if [ "$NUM" == "99" ]; then
break
fi
done


I tried setting NUM to 01 initially but that just gives.

01
2
3
4
5


Any thoughts?
# 2  
Old 03-23-2009
OK I tried to cheat but I get this:
Code:
NUM="1"

while true; do

if [ $NUM -lt 10 ]
then
NUM="0$NUM"
fi

echo "$NUM"

NUM=$[$NUM + 1]

if [ "$NUM" == "15" ]; then
    break
fi

done

01
02
03
04
05
06
07
08
./1: line 13: 08: value too great for base (error token is "08")


?????
# 3  
Old 03-23-2009
Code:
zsh -c 'print -l {01..99}'

Code:
bash -c 'i=1;while ((i < 100)); do printf "%.2d\n" $i;((i+=1));done'

A slow version for pre-POSIX shells:

Code:
sh -c 'i=1;while [ $i -lt 100 ]; do [ $i -lt 10 ] && echo 0$i || echo $i; i=`expr $i + 1`;done'


Last edited by radoulov; 03-23-2009 at 12:12 PM..
# 4  
Old 03-23-2009
OK cool thanks.

I ended up changing the way I inced it to the:

i=`expr $i + 1`

format and I was good to go!

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog

I have been searching and reading about syslog. I would like to know how to Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog. tail -f /var/log/messages dblogger: msg_to_dbrow: no logtype using missing dblogger: msg_to_dbrow_str: val ==... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

3. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

4. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

5. Solaris

/var/adm & /var/sadm

what is the difference between tha /var/adm and /var/sadm files in solaris 10 Os please can any one respond quickly thanking you (2 Replies)
Discussion started by: wkbn86
2 Replies

6. UNIX for Dummies Questions & Answers

Difference between $var and ${var}

Hi I want to know what is the difference between $var and ${var}. :D I saw in few of the scripts examples.Somewhere people use $var and somewhere ${var}. Is there any difference between two ? :confused: Thanks (4 Replies)
Discussion started by: dashing201
4 Replies

7. Solaris

Lost /var/sadm/install/contents file and /var/sadm/pkg

Hello, I recently found that my /var/sadm/install/contents, ~/admin/default, /var/spool/patch and /var/spool/pkg files were empty. This broke the pkginfo, pkgchk and other package related tools. The pkgmap no longer points to where the applications have been installed. I have replaced the... (0 Replies)
Discussion started by: ronin42
0 Replies

8. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

9. Shell Programming and Scripting

Use loop var i within Cut Command

Hi, In the following bash code rather than cutting at a predefined character I would like to cut at position i (i var from loop). Is this possible? I have tried eval, but either it's not possible or my syntax is wrong. thanks Nick for i in {1..9} do theChar=$(echo... (3 Replies)
Discussion started by: de_la_espada
3 Replies

10. Shell Programming and Scripting

Storing space delimited line in var with loop?

I have a script that converts a file into an html table. This script works fine for a 1 column table. However, I'm trying to do this for a multi-column table. My input file will look something like this: a b c d e f g h i My script basically works by taking in each line and putting that... (2 Replies)
Discussion started by: eltinator
2 Replies
Login or Register to Ask a Question