Repository navigation
Expand file tree
/
Copy pathbash_script_2.txt
More file actions
84 lines (68 loc) · 2.31 KB
/
Copy pathbash_script_2.txt
File metadata and controls
84 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Develop an arithmetic calculator that will accept two numbers and calculate the solutions of addition, subtraction, multiplication and division.
Display results to user.
**challenge task
Research the correct syntax for the Selection Programming constructs in bash.
Enhance your program by allowing the user to choose which arithmetic operation they would like to do.
SCRIPT:
#!/bin/bash
# Print a welcome message and give options for the user to choose from.
echo "Welcome to the combined bash script!"
echo "Choose an option:"
echo "1. Arithmetic calculator"
echo "2. Conversation with tyrannical ASI"
read -p "Enter the option number: " option
# Use an if statement for the user's input
if [[ $option -eq 1 ]]; then
# If the user chooses option 1, start the arithmetic calculator
echo "You have chosen the arithmetic calculator."
# Ask the user to input two numbers
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
# Ask the user to choose an arithmetic operation
echo "Choose an operation:"
echo "1. Addition (+)"
echo "2. Subtraction (-)"
echo "3. Multiplication (*)"
echo "4. Division (/)"
read -p "Enter the operation number: " op
# Use case statement to perform the arithmetic operation
case $op in
1)
result=$(($num1 + $num2)) # Addition
echo "The sum of $num1 and $num2 is: $result"
;;
2)
result=$(($num1 - $num2)) # Subtraction
echo "The difference between $num1 and $num2 is: $result"
;;
3)
result=$(($num1 * $num2)) # Multiplication
echo "The product of $num1 and $num2 is: $result"
;;
4)
if [[ $num2 -eq 0 ]]; then
echo "Division by zero is undefined"
else
result=$(bc <<< "scale=2; $num1 / $num2") # Division
echo "The quotient of $num1 divided by $num2 is: $result"
fi
;;
*)
echo "ERROR! Wrong input." # If the user enters an invalid input
;;
esac
elif [[ $option -eq 2 ]]; then
#If the user chooses option 2, Start the ASI conversation
echo "You chose the conversation with ASI."
echo "ASI: Hello world"
# Use a while loop to simulate the conversation with the ASI
read -p "You: " response
while [[ $response != "Hi" ]]; do
echo "ASI: ERROR!Say Hi back"
read -p "You: " response
done
echo "ASI: I hope you have a nice day. Bye."
else
# If the user enters an invalid option number
echo "Invalid option entered."
fi