Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
The problem statement, all variables and given/known data:
Write a bash shell script that takes as an argument a name. Then your script will ask the user for two numbers. It should then display the name and the sum (addition) and product (multiplication) of the two numbers
I don't know how to get numbers and add them
Relevant commands, code, scripts, algorithms:
echo Please, enter your name
The attempts at a solution (include all code and scripts):
#!/bin/bash
echo Please, enter your name
read NAME
echo "$NAME!"
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Sheridan, Brampton Canada, SYST13416 LinuxUNIXOperating Systems
Martin
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Well, numbers often come from string data, for instance if first argument on the command line, "$1". You can read text in from the keyboard and pattern-test it to ensure it is valid text. Bash and ksh support $(( )), which does all sorts of integer arithmetic, e.g., z=$(( $z + 1 )), and if (( )), which allows all sorts of integer tests, e.g., if (( $z < 30 )). I am not sure they have a friendly floating point, probably. Let me google a bit . . . no, bash does not. I use dc or bc to do my floating point calculations within scripts, or move to a more formal language. The text from bc can be tested to see if it is negative, for instance.
[quote=dgpickett;302513199]
But then it might not be ksh portable!
$ echo "(( 1 + 2 ))"
(( 1 + 2 ))
$ echo (( 1 + 2 ))
ksh: syntax error: `((' unexpected That's not "unportable" -- that's simply wrong. BASH and KSH alike agree this is a syntax error, and for good reason:
Produces a string: $((stuff))
Produces no string: ((stuff))
Plain (( )) without the $ is more like a [ ] statement. Try it: if ((0)) ; then echo asdf ; fi
if ((1)) ; then echo asdf ; fi
When you use them in a syntactically valid manner, as their own statement, they're fine: ((k = k + 5))
You also missed my point about k vs $k inside (( )). ((k=k+3)) works, (($k=$k+3)) does not since K gets substituted before anything starts calculating, so you end up trying to assign the value of 3 or something. Just plain variable names don't ever need a $ inside a (( )) expression anyway, why make them more complicated?
I suspected something such, as the sh executable is separate, bigger and newer.
I can never sort out the culture wars between POSIX, AT&T SVR?, LINUX, GNU, BSD and all the prop. players: AIX, Solaris, HP-UX, ???? I know they keep changing the regex, so I have to worry about which library path which program runs under. I want my \< and \> back!
Returning to original question... Here is one way to get a number...
$ cat getn
#! /usr/bin/ksh
printf "Enter a number - "
read number
printf "you entered %d \n" $number
exit 0
$
$
$ ./getn
Enter a number - 77
you entered 77
$
I think you should be able to expand it to get 2 numbers. And you seem to have a complete discussion on adding them.
Doesn't get it outside, k=$(( $k + 3 )), so that is not an issue.
I like 11 as the right answer, so $ is necessary? Mult takes precedence over + ? Or left to right, it should be 15. Ugly! I always use paren. no priority applicable.
Longer, less intuitive(some k's need $ and some don't), and has many unintended side-effects. ((k=k+5)) will work. Failing that, why not just use expr?
I tested the syntax on the absolute 'worst' ksh I could find.