-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced Bash Scripting
More file actions
72 lines (56 loc) · 1.66 KB
/
Advanced Bash Scripting
File metadata and controls
72 lines (56 loc) · 1.66 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
# 1.1. Create a new Bash script
echo '#!/bin/bash' > conditional_script.sh
chmod u+x conditional_script.sh
# 1.2. Query the user and store their response
!/bin/bash
echo 'Are you enjoying this course so far?'
echo -n "Enter \"y\" for yes, \"n\" for no."
read response
# 1.3. Use a conditional block to select a response for the user
#!/bin/bash
echo 'Are you enjoying this course so far?'
echo -n "Enter \"y\" for yes, \"n\" for no"
read response
if [ "$response" == "y" ]
then
echo "I'm pleased to hear you are enjoying the course!"
echo "Your feedback regarding what you have been enjoying would be most welcome!"
elif [ "$response" = "n" ]
then
echo "I'm sorry to hear you are not enjoying the course."
echo "Your feedback regarding what we can do to improve the learning experience"
echo "for this course would be greatly appreciated!"
else
echo "Your response must be either 'y' or 'n'."
echo "Please re-run the script to try again."
fi
# 2.1. Create a Bash script
#!/bin/bash
echo -n "Enter an integer: "
read n1
echo -n "Enter another integer: "
read n2
sum=$(($n1+$n2))
product=$(($n1*$n2))
echo "The sum of $n1 and $n2 is $sum"
echo "The product of $n1 and $n2 is $product."
# 2.2. Add logic to your script
#!/bin/bash
echo -n "Enter an integer: "
read n1
echo -n "Enter another integer: "
read n2
sum=$(($n1+$n2))
product=$(($n1*$n2))
echo "The sum of $n1 and $n2 is $sum"
echo "The product of $n1 and $n2 is $product."
if [ $sum -lt $product ]
then
echo "The sum is less than the product."
elif [[ $sum == $product ]]
then
echo "The sum is equal to the product."
elif [ $sum -gt $product ]
then
echo "The sum is greater than the product."
fi