-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection8.sh
executable file
·48 lines (43 loc) · 1.16 KB
/
section8.sh
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
#!/bin/sh
EXERCISE_NUMBER=0
TOTAL_ALLOWED_EXERCISE=2
# Exercise 1:
# Write a shell script that displays one random number to the screen and also generates a syslog
# message with that random number. Use the "user" facility and the "info" facility for your
# messages.
# Hint: Use $RANDOM
function e1() {
local number=$RANDOM
local message="Writing $number to the log"
echo $message
logger -p user.notice $message
}
# Exercise 2:
# Modify the previous script so that it uses a logging function. Additionally tag each syslog
# message with "randomly" and include the process ID. Generate 3 random numbers.
function e2() {
local number=$RANDOM
local message="Writing $number to the log"
echo $message
logger -p user.notice -t "randomly" -i $message
}
function run() {
if [ $# -ne 1 ]
then
echo "No exercise selected!"
read -p "Please pass in the exercise number[1-${TOTAL_ALLOWED_EXERCISE}]: " EXERCISE_NUMBER
else
EXERCISE_NUMBER=$1
fi
run_exercise $EXERCISE_NUMBER
}
function run_exercise() {
if [ "$1" -le "$TOTAL_ALLOWED_EXERCISE" ]
then
FUNCTION_NAME="e$1"
$FUNCTION_NAME
else
echo "Wrong exercise number passed: "$1
fi
}
run $@