-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection9.sh
executable file
·122 lines (113 loc) · 3.18 KB
/
section9.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
EXERCISE_NUMBER=0
TOTAL_ALLOWED_EXERCISE=3
# Exercise 1:
# Write a shell script that loops through the /etc/passwd file one line at a time. Prepend each line
# with a line number followed by a colon and then a space.
# Example output:
# 1: root:x:0:0:root:/root:/bin/bash
# 2: daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# 3: bin:x:2:2:bin:/bin:/usr/sbin/nologin
# 4: sys:x:3:3:sys:/dev:/usr/sbin/nologin
function e1() {
local line_number=0;
grep -v "^#" /etc/passwd | while read line
do
echo $line_number:" "$line
((line_number++))
done
}
# Exercise 2:
# Write a shell script that asks the user for the number of lines they would like to display from the
# /etc/passwd file and display those lines.
# Example output:
# How many lines of /etc/passwd would you like to see? 3
# root:x:0:0:root:/root:/bin/bash
# daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# bin:x:2:2:bin:/bin:/usr/sbin/nologin
function e2() {
read -p "How many lines of /etc/passwd would you like to see? " number_of_lines_user_see
local line_number=0;
grep -v "^#" /etc/passwd | while read line
do
if [ $number_of_lines_user_see -le $line_number ]
then
break
fi
echo $line_number:" "$line
((line_number++))
done
}
# Exercise 3:
# Write a shell script that that allows a user to select an action from the menu. The actions are to
# show the disk usage, show the uptime on the system, and show the users that are logged into
# the system. Tell the user to enter qto quit. Display "Goodbye!" just before the script exits.
# If the user enters anything other than 1, 2, 3, or q, tell them that it is an "Invalid option."
# You can show the disk usage by using the dfcommand. To show the uptime, use the uptime
# command. To show the users logged into the system, use the whocommand. Print a blank
# line after the output of each command.
# Example output:
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) 2
# 14:59:08 up 3 days, 7:36, 7 users, load average: 0.13, 0.22, 0.33
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) 4
# Invalid option.
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) q
# Goodbye!
function e3() {
while true
do
echo "1. Show disk usage."
echo "2. Show system uptime.="
echo "3. Show the users logged into the system."
read -p "What would you like to do? (Enter q to quit.) " action
case $action in
1)
df -h
echo
;;
2)
uptime
echo
;;
3)
whoami
echo
;;
"q")
echo "Goodbye!"
break
;;
*)
echo "Invalid option"
;;
esac
done
}
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 $@