M01: Introduction to Operating Systems
TU3: Installing specific purpose software
ASIX1
Pract. Exerc. 1: Introduction to shell scripts  2-05-17

GENERAL CONDITIONS
1- Deadline: 20-05-2017. No exercise sent to us beyond  this date will be accepted. On the 21st of May we will provide you with a proper answer of these exercises.
2- Create a tar.gz file 
including every script.
3- Send a tar.gz file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher.
     b) File Name:
          b.1) ASIX1 (Catalan): asix1_surname_name_m01tu03pr1.tar.gz
          b2.) DAW1 (English): daw1_surname_name_m01tu03pr1.targfz
    c) Subject:
        
c.1) ASIX1 (Catalan): asix1_surname_name_m01tu03pr1
         c.2)
DAW1 (English): daw1_surname_name_m01tu03pr1
 
4- Make your scripts individually.

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.- Local and global shell variables and the export command // The export command
    2.6.- Positional parameters (also called arguments)
    2.7.- Bash variables are untyped
    2.8.- Bash special variables. $? variable.
3.- Special characters
    3.1.- List of special characters
    3.2.- Comments
    3.3.- Quotes
    3.4.- Redirection
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.- test // 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
    7.4.- break and continue commands
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- Write a script to create a new group in your system. 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 1. Save your script with the following name: script01.sh. Run and check if your script works.

2- Write a script to create a new user in your system. 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 1. Save your script with the following name: script02.sh. Run and check if your script works.

3- Write a script called loginteller.sh that:
a) Firstly, it tries to download  loginteller.tar.gz  which is a file stored in a software repository  whose URL is http://www.collados.org/asix1/m01/tu3/loginteller.tar.gz. if your script fails to download loginteller.tar.gz, it will terminate returning an exit code equal to 1 and showing: "File not found".
b) Secondly,  the contents of loginteller.tar,gz will be extracted. After extracting its contents, two new files should have been created: loginteller.c and Makefile.
c) Thirdly, loginteller.c will be compiled  running the command make  loginteller and a new binary program called loginteller will be created on our system. If loginteller.c is not succesfully compiled, your scripts  will display "File not compiled" and it will be terminated returning an exit code equal to 2.
d)  Fourhly, loginteller will be installed on your system running make install. If loginteller is not succesfully installed, your script  will display "Progran was not installed" and it will be terminated returning an exit code equal to 3.
e) Finally, your script will run make clean to clean any temporary file created when loginteller.c was compilled. If  your script does not work properly, the following message wil be displayed: "Problems cleaning temporary files",  and it will be terminated returning an exit code equal to 4.
f) Your script must terminate returning an exit code equal to 0 if everything works fine.

4- Write 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 terminate returning an exit code equal to 1 and showing: "This script must be run as root".
b) Secondly, it tries to download  bomba.deb  which is a file stored in a software repository  whose URL is 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: "Debian package not found".
c) Thirdly,  bomba.deb will be installed on your system with the help of dpkg. If bomba.deb is not succesfully installed on your system, your script  will display "Debian package not installed on your system" and it will be terminated returning an exit code equal to 3.
d)
Finally, the newly installed program called bomba will be run. 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.
e) Your script must terminate returning an exit code equal to 0 if everything works fine.

5- Write a script called rmbomb.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 terminate returning an exit code equal to 1 and showing: "This script must be run as root".
c) Secondly, 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 not uninstalled and purged" and it will be terminated returning an exit code equal to 2.
c) Your script must terminate returning an exit code equal to 0 if everything works fine.

6- Write a script called mltpusr.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 terminate returning an exit code equal to 1 and showing: "This script must be run as root".
b) Secondly, it tries to download  users.ods  which is a file stored in a software repository  whose URL is http://www.collados.org/asix1/m01/tu3/users.ods. if your script fails to download users.ods, it will terminate returning an exit code equal to 2 and showing: "Users list not found".
c) Thirdly,  users.ods will be saved as new text .csv file called users.csv . If users.ods  can not be saved as users.csv, your script  will display "Conversion failure" and it will be terminated returning an exit code equal to 3.
d)
Finally, your script will create a new user on yur system for each username written in users.csv. For each new user, the default group will be users, the default shell will be /bin/bash and the default password will be fjeclot. Your script will ask for an initial UID that will be assigned to the first user. The UID value will be increased before creating a new user. If your script can not create a user, the following message will be displayed: "Problems creating a user", and it will be terminated returning an exit code equal to 4.
e) Your script must terminate returning an exit code equal to 0 if everything works fine.

7- Write a script called dmltpusr.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 terminate returning an exit code equal to 1 and showing: "This script must be run as root".
b) Secondly, it tries to download  users.ods  which is a file stored in a software repository  whose URL is http://www.collados.org/asix1/m01/tu3/users.ods. if your script fails to download users.ods, it will terminate returning an exit code equal to 2 and showing: "Users list not found".
c) Thirdly,  users.ods will be saved as new text .csv file called users.csv . If users.ods  can not be saved as users.csv, your script  will display "Conversion failure" and it will be terminated returning an exit code equal to 3.
d)
Finally, your script will remove any user (and its personal folder) that appears in users.csv.  If your script can not remove a user, the following message will be displayed: "Problems removing a user", and it will be terminated returning an exit code equal to 4.
e) Your script must terminate returning an exit code equal to 0 if everything works fine.

8- Write a script called menu.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) Run loginteller.sh
    2) Run bomb.sh
    3) Run unbomb.sh
If you select any other option, the following message will be displayed: "Wrong option. Please, select 1/2/3"   
c) Thirdly, the selected script 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.