0

I've the following bash script. It calculates ax^2 + bx + c. Asks for a,b and c as you can see and gets x as a command line argument.

echo "Enter a value for a: "
read a
echo "Enter a value for b: "
read b
echo "Enter a value for c: "
read c

echo Result is `expr $a \* $1 \* $1 + $b \* $1 + $c`.

exit

What I want it to do now is(without any modifications to the above code) that get(override) the values of a, b & c from a file(values listed one after another, all in a line) in command line and skip asking for them in the execution of the script.

I though getopts would be the function for this purpose but I couldn't figure out how to use it. Or is it something else?

Thanks.

Can
  • 4,516
  • 6
  • 28
  • 50
  • You seem to have conflicting requirements: 1) `without any modifications to the above code` and then 2) `skip asking for them in the execution of the script` – sampson-chen Nov 25 '12 at 18:57
  • Moreover, where "from command line" do you want to get `a, b & c` from if you don't want to ask the user for input? do you want to parse it from a file? – sampson-chen Nov 25 '12 at 18:58
  • yeah, I want parsing from a file. I was just adding that necessity as an edit. Also I don't want any changes to the code since this assignment of mine requires it like that. – Can Nov 25 '12 at 19:01
  • sure, can you show us what the exact format of your input file would look like? – sampson-chen Nov 25 '12 at 19:02
  • Do you mean something with redirection? `script < fileContainingABC` – mouviciel Nov 25 '12 at 19:02
  • As a note, rather than `echo "Enter a value for a: "; read a`, I would suggest `read -p "Enter a value for a: " a`. – cmh Nov 25 '12 at 19:04
  • My input file is just 2, 3, 4; all in a new line. Nothing else. – Can Nov 25 '12 at 19:09

1 Answers1

1

You can create the file you wanted:

1
2
3

Calling it params.txt, then you do:

$ ./myScript 2 < params 
Enter a value for a: 
Enter a value for b: 
Enter a value for c: 
Result is 11.

And just works.

mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
  • @CanSürmeli I really doubt I'd call this "variable overriding", but I couldn't get the way I'd call it either. – mgarciaisaia Nov 25 '12 at 19:36
  • You're right actually. What was wanted from me was basically just redirection but I thought hard on it as something else. – Can Nov 25 '12 at 19:57