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
How to set the File Paths for Inputs and Outputs Amruta Pitkar UNIX for Dummies Questions & Answers 1 10-31-2006 01:19 PM
Checking the format of inputs in a file sendhilmani123 Shell Programming and Scripting 13 05-19-2006 03:12 PM
Validating inputs from a file sendhilmani123 Shell Programming and Scripting 1 05-10-2006 02:49 AM
redirecting serial inputs to a file? guilartec Shell Programming and Scripting 4 02-27-2006 07:30 PM
Reading in two inputs from a file MadHatter Shell Programming and Scripting 1 06-29-2005 07:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 13
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-01-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 02-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 13
Stumble this Post!
Thanks a lot
Reply With Quote
  #4 (permalink)  
Old 02-01-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,547
Stumble this Post!
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
Reply With Quote
  #5 (permalink)  
Old 02-01-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,658
Stumble this Post!
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:50 AM.


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

Content Relevant URLs by vBSEO 3.2.0