Congratulations on beginning your scripting journey.
First of all, please use [code][/code]
tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Second, clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).
is deprecated syntax. arithmetic expressions
should be done these days with $((..))
Other than that, there are no syntax errors in your code, and it runs for me as posted. You should however use #!/bin/bash
instead of #!/bin/sh
(assuming you are coding for bash, of course). Using "sh
" means that the script will be interpreted as a posix-compliant script, and many shell-specific features will be unavailable or have altered behavior.
Also, your next step should be to do some input checking on the variables, to make sure you're actually getting integers before you process them. This usually means running the input part in a loop, with an if
statement for testing the value.
while true; do
echo "enter x"
case $x in
*[^0-9]*) echo "Not an integer, try again."