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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to set the File Paths for Inputs and Outputs Amruta Pitkar UNIX for Dummies Questions & Answers 1 10-31-2006 05:19 PM
Checking the format of inputs in a file sendhilmani123 Shell Programming and Scripting 13 05-19-2006 07:12 PM
Validating inputs from a file sendhilmani123 Shell Programming and Scripting 1 05-10-2006 06:49 AM
redirecting serial inputs to a file? guilartec Shell Programming and Scripting 4 02-27-2006 11:30 PM
Reading in two inputs from a file MadHatter Shell Programming and Scripting 1 06-29-2005 11:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-01-2006
sendhil sendhil is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 13
Inputs from a file

Hi,
I have a shell script that has to taken inputs from a file say "Inputs". Now I take 2 inputs at a time. Suppose the Inputs file contains numbers like
2
3
4
5
Now I have a written a script for adding 2 numbers. When I run the script for first time 2 and 3 must be the inputs. When i run the script for the second time the inputs should be 4 and 5. Is there is any way to do this. I know that the inputs can be taken using 'exec'. But , how can i do for this case.

Thanx in advance
  #2 (permalink)  
Old 02-01-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
cat filename | paste - - | while read a b
do
vl=$(( a + b ))
echo $vl
done

if you are very particular to include your script to add the numbers, remove the line vl=$(( a + b )) and add your script name with variable a and b.
  #3 (permalink)  
Old 02-01-2006
sendhil sendhil is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 13
Thanks a lot
  #4 (permalink)  
Old 02-01-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
this is a round-abt way, better dont use it i thought of giving it a try thats it

Code:
# !/usr/bin/ksh
#1

#The above line indicates the run

num=`sed 2q $0 | tail -1 | sed 's/#//'`
cnt=1
counter=0
sum=0

while read line
do
if [ $cnt -eq $num ]
then
  if [ $counter -le 1 ]
  then
     sum=$(($sum + $line))
     counter=$(($counter + 1))
  else
     break
  fi
else
  cnt=$(($cnt + 1))
fi
done < data

echo "sum is $sum"
cnt=$(($cnt + 2))

sed 1q $0 > tmp
echo "#$cnt" >> tmp
awk 'NR>2' $0 >> tmp

mv tmp $0
chmod 744 $0
exit 0
Code:
>cat data
2
3
4
5
6
7
8
9
Code:
>sum.ksh
sum is 5

>sum.ksh 
sum is 9

>sum.ksh 
sum is 13
the above three are consecutive runs
  #5 (permalink)  
Old 02-01-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Doing away with the cat and the paste, this will work

Code:
#! /bin/ksh

echo "2
 3
 4
 5" | while read line
do
        NUM1=$line
        read line
        NUM2=$line
        SUM=$((NUM1+NUM2))
        echo $SUM
done
If reading inputs from a file, then this would do

Code:
#! /bin/ksh

while read line
do
        NUM1=$line
        read line
        NUM2=$line
        SUM=$((NUM1+NUM2))
        echo $SUM
done < input.file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0