![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
|||
|
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 |