simple while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple while loop
# 1  
Old 12-13-2007
simple while loop

i have a script called file2

#!/bin/ksh


i=0
while [ $i -lt 5 ]
do
echo $i >> result.txt
i=`expr $i + 1`
done
echo "***********************" >> result

-------------------------------------------------------------------

Last edited by ali560045; 12-13-2007 at 05:17 AM..
# 2  
Old 12-13-2007
hey in my file result.txt,the o/p is
coming

i want if again i execute this script .........In the result.txt it should overwrite the data from previous execution of the script file2

plz help me in this

Last edited by ali560045; 12-13-2007 at 05:17 AM..
# 3  
Old 12-13-2007
how about

Code:
if test -f results.txt
then
      rm results.txt
fi

at the start.
# 4  
Old 12-13-2007
1. If writing in ksh use the shell built-in "print" instead of "echo".

2. If you post code here pls. use the [ code ] and [/ code ]-BBcodes

3. instead of using "expr" with backticks use "(( i += 1 ))"

4. open a file descriptor via exec and use "print -u[N]" to create output. This way you can open the file once at the start of the script and close it at the end. This way you can easily switch from append- to overwrite-mode:

Code:
#! /bin/ksh

typeset -i iCnt=0

exec 3>> /path/to/file       # apend-mode
#exec 3> /path/to/file       # overwrite-mode

while [ $iCnt -lt 5 ] ; do
     print -u3 - "iCnt now at $iCnt"
     (( iCnt += 1 ))
done
print -u3 - "--------------"
exec 3>&-

exit 0

bakunin
# 5  
Old 12-13-2007
look when i 1st execute the script i get the o/p at result.txt.
when 2nd time i m exexcuting i doesnt want the previous o/p there but the new o/p.......but the file should exist there............

until and unless i dnt execute it 2nd time the o/p in file result.txt should exist........

i hope u getting me
# 6  
Old 12-13-2007
thanks both of u.porter ur answer is good. bakunnin i still didnt understood wat u saying..........i thinks u r saying something imp,can u explain me little bit
# 7  
Old 12-13-2007
Quote:
Originally Posted by ali560045
bakunnin i still didnt understood wat u saying
Which part of it? My posting was discussing 4 different points numbered 1-4. Could you at least be so kind to explain which part of it you didn't understand?

Furthermore i provided a script as example - which part of it is unclear?

Why don't you just *try*, for crying out loud, the script i provided and observe what it is doing? First let it run as it is, twice, and look at the output file generated. Then uncomment the now commented second "exec..." statement and comment out the first one instead. Now let the script run again, twice - what is the output file looking like now?

I can help you writing scripts, but i can't do the reading of man-pages for you and i can't do the understanding for you.

As a last and more personal point:

Could you please skip that leet-speak and try to use a plain and civilized English as does the rest of us? English is not my native language and I'm far from perfection (in fact farther than i would like to be), but even a bad english speaker like me is able to avoid grammatical cardinal sins like writing "u" for "you" or "r" for "are". Honestly, i find it quite hard to figure out what "the o/p is" means, i have enough problems to think about even without having to ponder what "imp" means in "u r saying something imp" and so on... If you deem our answers so low in value that, to solicit them, even the effort of writing some extra characters is to be saved, than what reason do we have to give the answers you are seeking in first place?

We are not a bunch of wannabe-gangers here, most of us are hard-working professionals. This is not only showing in the things we do, but also in the ways how we do them and the way we talk about what we do (in one word: our culture). Please either blend into this group - including the language you use here - or do not try to get us to do work for you.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

a simple loop

Does any body can help me with a loop in this example? if then if then runner=$(grep "$1" "$2") runne=$(grep "$1" "$3") run=$(grep "$1" "$4") fi fi # # Message on screen... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

3. UNIX for Dummies Questions & Answers

Simple loop

I need to chmod a bunch of files with a specific extension in one directory. If I understand correctly first I would run ls command like this ls -R | grep .mp3 > /tmp/list once I have the output file I should be able to run a loop to chmod all the files in the list created. This is where... (5 Replies)
Discussion started by: eugenes18t
5 Replies

4. Shell Programming and Scripting

Simple using For loop

Hi expert, I'm using csh Code: #!/bin/csh set x = 0 set number = `awk '{array=$0} END {print array;}'` i want to use for loop to store data to $number repeatly untill x = 23 How to use c shell for loop? (2 Replies)
Discussion started by: vincyoxy
2 Replies

5. 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

6. UNIX for Dummies Questions & Answers

a simple loop in csh

Hello, I have a file with over 48000 lines and I need to select certain parts of the file. I know which awk commands work for what I need, I just need some help putting together a loop that will repeat the command. These are the commands that work: awk 'NR < 6' plot.out > plot.test (I get... (1 Reply)
Discussion started by: dsstamps
1 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Simple loop query

Hi All Just started with shell scripts and am stumped by, what is to most of you no doubt, a simple issue. All I'm trying to do is prompt a user for input and writing to a log file. If the user types the word 'stop', then the program should halt. If the word typed is 'clear', then the log file... (2 Replies)
Discussion started by: kutz13
2 Replies

9. Shell Programming and Scripting

Simple while loop question

I have written a script that countsdown from 20 to 1 and has a sleep in between each count but I want to make it sleep for half a second but I get errors whenever I change the sleep from 1 second to half a second any ideas? I am using Sun OS 5.9 heres what I've got: X=0 while do echo... (3 Replies)
Discussion started by: Brokeback
3 Replies

10. Shell Programming and Scripting

a simple while loop

Hallo everyone I might just be being dumb, but I am using the BASH shell and cannot get the following script to work: x=0 while do echo $x x=´echo "$x + 1" | bc´ done Can anybody help me out. I am just get a repeating output saying: bc: command not found 0 + 1: command not... (5 Replies)
Discussion started by: syno
5 Replies
Login or Register to Ask a Question