i am trying to write a script code in unix that will:
1. The problem statement, all variables and given/known data:
display following menu to user:
(A) Add
(B) Subtract
(C) Multiply
(D) Divide
(E) Modulus
(F) Exponentiation
(G) Exit
Then ask user for choice (A-F). After taking users choice ask user for two numbers and
perform chosen operation on those two numbers. The program should keep asking
for choice / numbers unless user chooses ‘G’ for exit.
2. Relevant commands, code, scripts, algorithms:
3. The attempts at a solution (include all code and scripts):
i tried everything to write this code i couldnt
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number
school name: Senaca college, Toronto, Canada, Name of Professor: peter wheeler, and Course Number: Tech 154
Last edited by vbe; 03-25-2014 at 06:22 AM..
Reason: added template...
OK, what exactly have you tried? Post your tries here and we can tell you why they don't work or what you do wrong.
bakunin
thats what ive tried
Code:
echo -n "Enter first number "
read a
echo -n "Enter second number "
read b
if [ $x == 'A' -o $x == 'a' ] ; then
c=$(( a + b ))
fi
if [ $x == 'B' -o $x == 'b' ] ; then
c=$(( a - b ))
fi
if [ $x == 'C' -o $x == 'c' ] ; then
c=$(( a * b ))
fi
echo "The answer is $c"
Last edited by Franklin52; 03-25-2014 at 04:15 AM..
Reason: Please use code tags
Ok i see you had did a read -p 'Please type ation:' x, that is not visible here .
You can either make sure the provided char inputs are non-caps, or use regex to catch both at once.
1) Make it small (is this posix?)
Code:
x=A
x=${x,,}
[ $x = a ] && echo "Add"
2) Or use Regex:
Code:
x=a
[ $x = [aA] ] && echo "Add"
Use either 1. or 2., this will save you from using if [ $x == 'B' -o $x == 'b' ] ; then for each of the A-F.
3) For menus or comparing 1 value with diffrent options, its quite easier to use a case statement
Code:
case $x in
a) echo "do add" ;;
b) echo "do sub" ;;
esac
4) Could use Regex here too (= instead)
Code:
case $x in
a|A) echo "do add" ;;
[bB]) echo "do sub" ;;
esac
edit: 5. Quotes:
I'd rather make the quotes around the variables, than around the letters.
If the variable is empty, or contains a special char (like arrow-up) it will produce and error message, rather than just not execute (as in not true ; not matching).
So yes, for the REGEX you need the double brackets.
BTW: bad behaviour example as i was running it as root
Edit: Yes, for anything more complex than a simple single yes/no requires a standard if-statement like:
Code:
if [ $x -ge 100 ] || [[ "$x" == [aA] ]]
then echo "Great"
else echo "Low"
fi
BTW: The only 'place' i actualy use == (as it wont work properly with a single) is with PHP.
All of both the shells (sh,bash) i came across worked well with a single =.
EDIT2:
Just read (once more) the 'man bash', and beeing in the homework section i must say:
-> Better get used to use the proper syntax right from the start!
Hi,
as I mentioned in this thread(https://www.unix.com/shell-programming-and-scripting/280737-awk-function-return-permutations-n-items-out-m.html), a helpful coding style may improve overall value and support for people who come here and want to learn things the participants from unix.com have... (2 Replies)
Good afternoon everyone,
I am very new to UNIX shell scripting and I am trying to understand the following code. I know what it does but I need to modify it so it will allow me to pass a file name as *FILENAME*
Thank for any guidance offered.
if ] ; then
match=`expr "$file" :... (2 Replies)
Unix script coding help?
i am trying to write a code that will display following menu to user:
(A) Add
(B) Subtract
(C) Multiply
(D) Divide
(E) Modulus
(F) Exponentiation
(G) Exit
Then ask user for choice (A-F). After taking users choice ask user for two numbers and
perform... (3 Replies)
I have a file f1.txt that contains string:
f1.txt
aaa
bbb
ccc
...
I want to write code to search that each string in file f2.txt(this file contains 1000+line codes).
file f2.txt
..
..
....aaa...xyz..
...
...
...ppp...
(dots . can be characters ot blank spaces)
If particular... (2 Replies)
Hi,
I have very little knowledge with unix and pmcmd. I need help with a issue.
I have to see whether a file has been dropped in a particular location/path. If the file dropped I have to check the last modified time, which should be greater than 8pmEST the prior day. If the file has been... (4 Replies)
I'm just starting a 'serious' coding in UNIX, so what I need is to run a C code on UNIX, What do I have to install (app) prior to coding/running the code and how do I compile that code?
can I write my c code in UNIX or I need to have a visual studio for this? (7 Replies)
Hi!
Suppose I am at a location xyz:/abc1/abc2/abc3
Is it possible to move to another location xyz:/mnl1/mnl2/mnl3
by some coding within a script? (5 Replies)
Given this one long stream of data (all one line):
<TransactionDetail><TransactionHeader><ErrorLogging>YES</ErrorLogging><HistoryLogging>YES</HistoryLogging><ErrorDetection>NO</ErrorD... (4 Replies)