Let's develop a script with some common administrative tasks using bash menus. 1
This is useful because once it's done, you simply select the desired option and the script, does the 2
entire job for you. 3
It can even be run by junior sysadmins that do not know so much about Linux command line and so on. 4
You can think of this script like a control panel where you can manage different tasks. 5
vim system_administration.sh 6
I’m changing the PS3 variable which is the prompt. "Your choice:" and the select statement. 7
select ITEM and I'll add four or five menu entries, but feel free to add as many as you need "Add 8
User" 9
the first entry "List All Processes" 10
These are all common administrative tasks. 11
"Kill Process" "Install Program" and "Quit" 12
Do and done The user will select an entry from the menu using its corresponding number which is held into 13
the reply variable. 14
So I'm going to test the variable to see what has the user chosen. 15
So if [[ $REPLY -eq 1 ]] if the user has selected the first option, then I'm prompting 16
for the username. 17
I want to add a new user so read -p "Enter the username:" and the name of the variable will be username 18
Before actually creating the user 19
I want to check if it doesn't already exist. I'll run the command that creates the user only if the 20
user does not exist. 21
And I'll do a command substitution. 22
I'm searching for the user in the password file using grep and the -w option which is searching 23
for whole words. 24
So output = and the command substitution. 25
So grep - w the string I'm searching for username and the file /etc/ passwd The output of the 26
command will be stored into the output variable; if the variable, which is a string, is not empty, 27
that means that the user already exists on the system. 28
And I'm printing a message saying that it already exists, so if [[ -n "$output" ]] I'm using the 29
minus -n test, which returns true, 30
if the string is not zero-length, then echo "The username already exists!" 31
else, so if the user does not exist, I'm going to create it sudo useradd and some options, -m 32
-s for the shell bin/ bash and the username. 33
So $username 34
This could be enough, but I'd like in this case to test if the last command was executed successfully. 35
Remember that the status of the last run command is saved into $? and the value zero 36
means no error; and the value which is different from zero means there was an error. 37
So I am testing the status of the last command. 38
This is optional, so if [[ $? -eq 0 ]] , then "The user was added successfully!" 39
And I'm also printing the last line of the password file to see the newly created user so tail -n1 40
/etc/passwd and else echo 41
"There was an error adding the user" 42
fi for this inner if and another fi I for the other if, for this one 43
That was for the first menu entry. I have indented the code for better readability. 44
I can see clearly where an if statement starts and ends. This is especially important if you have nested 45
structures like nested if statements here. Let's implement the second option. 46
So "Listing all processes" I'll add an elif statement for the outdter if, so elif [[ $REPLY -eq 47
2 ]] 48
So if the user has chosen 2; then echo "Listing all processes." 49
sleep 1, so the user has enough time to read the message and then ps-ef This is how I list all 50
processes. 51
And that's all. Let's implement the 3rd entry, so the code for killing a process. elif 52
[[ $REPLY -eq 3]] 53
then I'm prompting the user for the processed name and killing it read -p "Enter the program 54
to kill." 55
and the name of the variable that will be created process and pkill $process, I'm killing it. 56
OK, it's done! The next menu entry will install a program, so elif [[ $REPLY -eq 4 ]] 57
I'm prompting the user for the program to install and then install it using apt so read -p 58
"Enter the program to install." 59
and the name of the variable app; and the command sudo app update && sudo 60
app to install $app. 61
And finally, the implementation of the last menu option, which is quite so 62
elif [[ $REPLY -eq 5 ]] 63
then echo "Quiting." 64
sleep1 and exit And the last else statement for the case, when the user selects a nonexisting menu 65
entry, so else echo "Invalid menu selection" 66
and fi OK, that's all. Let's test the script! 67
shnod +x system_administration and I'm running it. 68
I'm selecting the first option. 69
adduser 70
First, I am entering a username that already exists, student, the username student already exists. 71
One more time, I'm entering a nonexisting user, for example, u100. 72
It's asking for the password. 73
And the user, u100, was added successfully and it has printed out the last line of the password 74
file! Great! Let's see 75
the second menu entry, it's "Listing all processes." 76
OK, now I'm going to kill a process. In another terminal 77
I'm starting a process like sleep100 and I'm choosing 3 and the name of the process is sleep. 78
And it was terminated. 79
Great! Let's install a program. 80
I want to install nmap, and it's installing it. 81
And finally "Quit", I'm quitting the script. 82
Great! This is a very useful system administration script. 83
Feel free to add as many entries as you need for the most common administrative tasks.