Input from a file and converting the answer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input from a file and converting the answer
# 1  
Old 12-14-2009
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 Scott; 12-14-2009 at 07:53 PM.. Reason: Please use code-tags
# 2  
Old 12-14-2009
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
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
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

3. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

KSH - Text from input file truncated while converting it to excel

Dear Members, I am using the attached script to convert a input file delimited by '|' to excel. However, while processing the attribute change_reason, the whole content of the text under change_reason is not displayed completely in the cell in excel. It is truncated after only first few words.... (1 Reply)
Discussion started by: Yoodit
1 Replies

6. Shell Programming and Scripting

Converting an scim .bin.user file to a stardict tab file possible with awk?

Hi all, Here is a scim sample.bin.user file a string1 0 a string2 0 a string3 63 b string4 126 c string5 315 d string6 0 e string7 63 e string8 126 f string9 0 I like to convert this into a dict.tab file to be compiled by the ... (4 Replies)
Discussion started by: hk008
4 Replies

7. Shell Programming and Scripting

how to input answer (enter) if output contains a string?

how to wrote a script that reads an input from the reader (dir name) and then answer yes to all questions in the script unless the answer to any of the questions contains a certain string? example: $] script.sh dir_name $] question_1: (answer should be y right after the question is echoed,... (3 Replies)
Discussion started by: faizlo
3 Replies

8. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

9. UNIX for Advanced & Expert Users

Problem in converting password protected excel file to csv file in unix

I need to convert a password protected excel file which will be in UNIX server to a comma separated file. For this I need to open the excel file in UNIX box but the UNIX box doesn't prompt for password instead it is opened in an encrypted manner. I could manually ftp the excel file to local... (2 Replies)
Discussion started by: Devivish
2 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question