EH1: Linux Lab
ASIX/DAW/DAM-1
Task A6: Introduction to shell scripts
6-5-24


INTRODUCTION TO SHELL SCRIPTS

DOCUMENTATION ABOUT SHELL SCRIPTING
1.- Introduction
    1.1.-
What is a Linux shell?
    1.2.- What is a shell program or script? // What is a shell program or script?
    1.3.- Interpreted vs Compiled programs
    1.4.- Why to write a shell script?  // Why to write a shell script?
    1.5.- What is a shebang (or sha-bang)?
    1.6.- Writing your first script and getting it to work
    1.7.- Different ways to invoke a script
    1.8.- Debugging bash scripts
2.-Variables
    2.1.- Variables: environment variables and user defined variables.
    2.2.- Displaying current values of your environment variables
    2.3.- User defined variables.
    2.4.- Accessing to a variable value.
    2.5.- Positional parameters (also called arguments)
    2.6.- Bash variables are untyped
    2.7.- Bash special variables
3.- Special characters
    3.1.- List of special characters
    3.2.- Comments
    3.3.- Quotes 
4.- Some important commands
    4.1.- exit
    4.2.- echo . Another web page with useful information about the echo command..
    4.3.- read
    4.4.- expr
    4.5.- File test operators // Other comparision operators
5.- if...then...else...fi structure
    5.1.- if..then..else..fi // if..then..else..fi
    5.2.- if..then..fi
    5.3.- brackets
    5.4.- Nested if/then Condition Tests
6.- case statement
    6.1.- case stament
7.- Loop staments
    7.1.- while statement
    7.2.- until statement
    7.3.- for statement
8
.-Useful links
    8.1.-
http://tldp.org/LDP/abs/html/
    8.2.-http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/index.html
    8.3.- http://www.freeos.com/guides/lsst/index.html
    8.4.- http://bash.cyberciti.biz/guide/Main_Page
    8.5.- http://www.dartmouth.edu/~rc/classes/ksh/welcome.html#top
9- Some small examples
    9.1.- Some small examples    
   
PRACTICAL EXERCISES


1- Develope a script called usrgrps.sh that:
a) Firstly, it
checks if the script is being run by the root user. If it is not  being run by the root user, it will be terminated returning an exit code equal to 1 and showing: "This script must be run as root".
b) Secondly, it shows a menu with the following options:
     1) Create a new group -> Your script will ask for: a group name and a GID number.
     2) Create a new user -> Your script will ask for: a username, a UID number and a home directory name. Default group: users.  Shell: /bin/bash. Skeleton /etc/skel. Additional group: vboxsf. Password: FjeClot23$
     3) Remove a group -> Your script will ask for: a group name.

 
    4)
Remove a user
-> Your script will ask for: a username
If you select any other option, the following message will be displayed: "Wrong option. Please, select 1/2/3/4/"
c)
Your script will terminate returning an exit code exit code equal to 0 if everything works fine but
    - if your script fails to create the new group, it will terminate returning an exit code equal to 2. and the following message will be displayed: "The new group can not be created"
    - if your script fails to create the new user, it will terminate returning an exit code equal to 3. "The new user can not be created"
    - if your script fails to remove the group, it will terminate returning an exit code equal to 4. "The new group can not be removed"
    -
if your script fails to remove the user, it will terminate returning an exit code equal to 5
.
"The new user can not be removed"

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2- Develope a script called backup.sh that:
a) Firstly, it
checks if the script is being run by the root user. If it is not  being run by the root user, it will be terminated returning an exit code equal to 1 and showing: "This script must be run as root".
b) Afterwards, check if a directory called /backups exists on your system. If the directory does not exist, it will be created. If the directory can not be created, your script
will be terminated returning an exit code equal to 2 and showing: "/backups can not be created".
c) Ask the user for a directory name inside /backups. If the directory does not exist, it will be created by your script. If the directory can not be created, your script will be terminated returning an exit code equal to 3 and showing: "Error creating directories in /backups"".

d) Ask the user an extension
e) Ask the user a directory name  where files with the extension given by the user could be found.
f) Each file in the second directory will be copied in the directory created in step c).
g) Finally, the script packs and compress 
the directory created in step c)  adding to file name the current  date and time.
Your script will terminate returning an exit code exit code equal to 0 if everything works

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3- Develope a script called bomb.sh that:
a)
Firstly, it checks if the script is being run by the root user. If it is not  being run by the root user, it will be terminated returning an exit code equal to 1 and showing: "This script must be run as root".
b) Secondly, it shows a menu with the following options:
     1) Download  bomba.deb  from http://www.collados.org/asix1/m01/tu3/bomba.deb. if your script fails to download bomba.deb, it will terminate returning an exit code equal to 2 and showing: "Package not found".
     2) Intall bomba.deb on your system with the help of dpkg. If bomba.deb is not succesfully installed on your system, your script  will display "Debian package can not be installed on your system" and it will be terminated returning an exit code equal to 3.
     3) Run bomba
. If  your script can not run bomba, the following message wil be displayed: "Problems running bomba",  and it will be terminated returning an exit code equal to 4.
     4) Uninstall and purge bomba.deb with the help of dpkg. If bomba.deb is not succesfully uninstalled and purged, your script  will display "Debian package can not beuninstalled and purged" and it will be terminated returning an exit code equal to 5.
If you select any other option, the following message will be displayed: "Wrong option. Please, select 1/2/3/4/".
c) Thirdly, your script asks the user if he/she wants to display again the menu in order to select a new option.  If the user writes "yes" the  menu is shown again. Otherwise, the script ends.
d)
Your script must terminate returning an exit code equal to 0 if everything works fine.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4- Write a script called menu_installer.sh that:
a) Firstly, it
checks if the script is being run by the root user. If it is not  being run by the root user, it will be terminated returning an exit code equal to 5 and showing: "This script must be run as root".
b) Secondly, it shows a menu with the following options:
     1) Run a command to update the list of software packages available
     2) Run a command to install a package called  ipcalc
     3) Run
a command to to install a package called  jed
 
    4)
Run a command to remove ipcalc
    5) Run a command to remove and purge jed
 
If you select any other option, the following message will be displayed: "Wrong option. Please, select 1/2/3/4/5"   
c) Thirdly, the selected option is run.
d) Finally, it shows a message asking the user if he/she wants to continue (option y) or not
(option n). If the user select y, the terminal will be cleared and the menu will be shown again.
e) Your script must terminate returning an exit code equal to 0 if everything works fine.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------