Simple bash script "unexpected end of line error"
I'm kind of new to scripting so I have created a simple script that utilizes a sub function to read in a number and echos out the number and the square of that number. However, I get this error " line 18: syntax error: unexpected end of file"
Here is the code: Code:
#!/bin/bash I know it's a simple error but I've been looking for awhile and have no idea what's wrong. |
Quote:
|
Also how about
$NUM * $NUM = $FINAL ??? did you mean $FINAL = $NUM * $NUM |
I made both changes and I have a new error. Taking out the parentheses fixed the one but when I changed
Code:
$NUM * $NUM = $FINAL Code:
$FINAL = $NUM * $NUM |
Try this:
Code:
#!/bin/bash |
Excellent, it works. Thank you. Could you explain why it works though? I mean, what exactly the parentheses did and everything?
|
Quote:
|
Well, if you'll notice, matthewg42 and cfaj actually did two things to correct that line. The one that you noticed was the use of the $(( )) construct which just tells bash that you are doing some math. The second thing was to remove the spaces around the equal sign. In bash you can't have any spaces around that equal sign, or it will fail.
Code:
# The following line will work, no spaces |
I'd add that to assign a value to a variable, the $ sign must not precede the variable name, that is
Code:
FINAL=$(( $NUM * $NUM )) |
Ahhh, ok, got it. Thanks guys.
|
Minor tip, you can actually remove the '$' in front of the variables when its inside the $(( )) construct and just do:
Code:
FINAL=$((NUM * NUM)) |
Quote:
|
All times are GMT -5. The time now is 11:33 PM. |