The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
a simple loop in csh dsstamps UNIX for Dummies Questions & Answers 1 05-22-2008 12:03 AM
simple for loop ali560045 Shell Programming and Scripting 3 12-16-2007 09:39 PM
Simple loop query kutz13 UNIX for Dummies Questions & Answers 2 07-17-2007 12:13 PM
Simple while loop question Brokeback Shell Programming and Scripting 3 07-21-2006 06:04 AM
a simple while loop syno Shell Programming and Scripting 5 04-08-2006 03:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-13-2007
ali560045's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 206
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 01:17 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-13-2007
ali560045's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 206
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 01:17 AM.
Reply With Quote
  #3 (permalink)  
Old 12-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
how about

Code:
if test -f results.txt
then
      rm results.txt
fi
at the start.
Reply With Quote
  #4 (permalink)  
Old 12-13-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,099
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
Reply With Quote
  #5 (permalink)  
Old 12-13-2007
ali560045's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 206
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
Reply With Quote
  #6 (permalink)  
Old 12-13-2007
ali560045's Avatar
Registered User
 

Join Date: Oct 2007
Posts: 206
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
Reply With Quote
  #7 (permalink)  
Old 12-13-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,099
Quote:
Originally Posted by ali560045 View Post
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0