Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-14-2009
Registered User
 

Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Input from a file and converting the answer

Hello all,
I am new to unix and i am having probelms. If anyone could help. I am trying to input numbers from a file called data.file The problem i am having is I can input the file but when I hit enter to get the answer onto my output file i get a error message (standard_in) 1: parse error. My script looks like so. It in for converting Centigrade temperature into Fahrenheit or vise versa.

I can just enter on number like 32 and then go to ans.file and it shows the answer of this. Which is how it is supposed to look but i cannot enter more than one number or a file.


Code:
 Fahrenheit Temperature        Centigrade Temperature
_ _ _ _ _ _ _ _ _ _ _ _           _ _ _ _ _ _ _ _ _ _ _ _
   
            32                                      0
_ _ _ _ _ _ _ _ _ _ _ _           _ _ _ _ _ _ _ _ _ _ _ _
 
#!/bin/sh
print_dash_line()
{
#sub function to print dashes
echo "_ _ _ _ _ _ _ _ _ _ _ _        _ _ _ _ _ _ _ _ _ _ _ _ "
}
#***** Start of Main Program *****
#***** Assign Variables
input_file=data.file
output_file=ans.file
# formula ft=(9/5)*ct+32
# formula ct=(5/9)*(ft-32)
echo "1. Convert Centigrade temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperature into Centigrade"
echo -n "Select your choice (1-2) : "
read choice
if [ $choice -eq 1 ]
then
   echo -n "Enter Centigrade Temperature : "
   set `cat $input_file`
   echo $1 $2 $3 $4
   read ct
   ft=$(echo "scale=2;((9/5) * $ct) + 32" |bc)
   echo " Centigrade Temperature        Fahrenheit Temperature " > $output_file
   print_dash_line  >> $output_file
   echo >> $output_file
   echo "          $ct                           $ft " >> $output_file
   print_dash_line >> $output_file
elif [ $choice -eq 2 ]
then
   echo -n "Enter Fahrenheit Temperature : "
   set `cat $input_file`
   echo $1 $2 $3 $4
   read ft
   ct=$(echo "scale=2; (5/9)*($ft-32)" |bc)
   echo " Fahrenheit Temperature        Centigrade Temperature " > $output_file
   print_dash_line  >> $output_file
   echo >> $output_file
   echo "          $ft                          $ct " >> $output_file
   print_dash_line >> $output_file
else
    echo "Please select 1 or 2"
    exit l
fi

Thanks You all for any help you can give me.

Last edited by scottn; 12-14-2009 at 06:53 PM.. Reason: Please use code-tags
Sponsored Links
  #2  
Old 12-14-2009
TonyFullerMalv's Avatar
Registered User
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 1,021
Thanks: 0
Thanked 1 Time in 1 Post
To read data from a file a line at a time and process them you need something like:

Code:
while read DATA; do
  echo DATA = ${DATA}
done < ${input_file}

  #3  
Old 12-14-2009
Registered User
 

Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
I tried that one

Tony,

I tried this and it gives me the same problem. And also will not put it in the output file.
  #4  
Old 12-16-2009
Registered User
 

Join Date: Mar 2009
Posts: 170
Thanks: 0
Thanked 1 Time in 1 Post

Code:
input_file=data.file
output_file=ans.file

echo "1. Convert Centigrade temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperature into Centigrade"
echo -n "Select your choice (1-2) : "
read choice;
if [ $choice -eq 1 ]
then
   echo -n "Enter Centigrade Temperature : "
   echo " Centigrade Temperature        Fahrenheit Temperature " > ${output_file}
   print_dash_line  >> ${output_file}
   while read ct
   do
        ft=100;# change it to the formula
        echo "          $ct                           $ft " >> $output_file
        print_dash_line >> $output_file
   done < ${input_file}
....


HTH,
PL
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need script to take input from file, match on it in file 2 and input data darkoth Shell Programming and Scripting 1 11-03-2009 10:53 AM
converting fixed length file to delimited file Nishithinfy Shell Programming and Scripting 2 07-02-2009 07:50 AM
Problem in converting password protected excel file to csv file in unix Devivish UNIX for Advanced & Expert Users 2 03-30-2009 07:56 PM
Reading specific contents from 1 input files and appending it to another input file sksahu Shell Programming and Scripting 5 01-14-2009 05:09 AM
how to create auto-answer file solaix14 Shell Programming and Scripting 3 10-23-2008 06:40 PM



All times are GMT -4. The time now is 02:37 AM.