Warning

## Semestral projects A1--A5

WARNING: This information might be out of date. Before using the information here, ask your supervisor first. Valid for summer term 2015/2016

### Script implementation in BASH scripting language

• The tasks are to be solved individually. I'll be very angry if I find out that you do not understand the commands/processes you have solved in these tasks.
• The solutions should be uploaded using the Upload system that automatically evaulates the script.
• In case the solution is rejected by the system, read thoroughly the evaluation report.
• Remember to use the .sh suffix for your script to be automatically evaluated.
• The script must contain a (comment) header with the purpose of the script, author name, contact e-mail and current term.

Create a script that:

1. in the /tmp directory (folder) creates a subdirectory named osdtask,
2. in the /tmp/osdtask directory creates a file named rootdir.txt containing list of all files and directories of the root directory (not including hidden files),
3. sets the access rights for the file created: the owner and group have read and write permissions, others have no permissions,
4. in the directory /tmp/osdtask creates a symbolic link pointing to a file rootdir.txt and name it odkaz.txt so that the link is relative (i.e. the link is valid even in case the link and the referenced file are relocated into different directory),
5. in the /tmp directory deletes a subdirectory smazat (the directory might contain files and directories)
6. outputs a string “DONE” onto the standard error output.

Except for the last point the script should not produce any output.

Hint: see the following commands man, echo, cd, mkdir, ls, ln and rm.

WARNING: If you debug your script in a public computer (especially the file-server), remember to clean any files and directories you have created in the /tmp folder.

Create a script that:

1. prints its own name (without path) onto the standard output,
2. prints path to the current working directory,
3. outputs an empty line,
4. prints the first argument to the standard output,
5. outputs to the error output a list of all files and subdirectories (not recursively) that are present in a directory that is specified as the second argument of the script AND that contain the string specified as the first argument in their name,
6. executes the /bin/uname -a command and reads the output into a variable; then executes the program /tmp/osdwrite and passes it the value of the variable as an argument.

For testing purposes you can create an empty script /tmp/osdwrite on your local computer. Each program output goes to a separate line. The script should produce only the specified output.

You can expect that both arguments are always provided.

Example: Script output: (standard output only)

   doe_A2.sh
/home/student/john_doe/work

bin
<possible output of the /tmp/osdwrite program>

Error output of your script executed with the bin and / parameters:

  bin
sbin

Hint: see commands basename, grep.

Create a script that:

1. Tests if at least 1 command line argument is provided. If it is not provided, outputs a message CHYBA1 onto the standard error output and exits with a return value of 1.
2. Checks if the first argument is an existing ordinary file and that you have write permissions to it. If not, outputs message CHYBA2 onto the standard error output and exits with a return code of 2.
3. Outputs number of lines contained in the file provided as the first argument.
4. In case the second command line argument is provided, find out whether a command is running (has been executed) with the same name as the argument. If such process is running, print out it's PID and send it a SIGTERM (15) signal. If there is no such process, output an CHYBA3 message onto the stderr.

Example: Script output

7
1587

Hint: see commands wc, ps, cut, kill.

You might want to try different parameters of the ps command.

Create a script that reads standard input line by line. For each line read call a function called typsouboru (the only argument of the function is the line read) that performs the following operations:

1. If the argument is a valid directory (relative or absolute path), outputs a string DIR <directory_path> onto the stdout. Use the path in the same form it has been provided (as an argument).
2. If the argument is an existing file (of any type including symlinks), outputs a string FILE <file_path> onto the standard output.
3. In all other cases outputs a string INVALID <line_read> onto the standard error output.
4. The return value of the function is 0 if it is a valid file or directory, 1 otherwise.

If at least one INVALID line has been output, the script outputs onto standard error output a line # ERRORS where # stands for the number of invalid lines encountered till the end of the script run . If there were none such line (everything OK) do not output any line.

Example: Script output (stderr in red)

DIR /bin
FILE /bin/bash
INVALID /bin/bin
DIR /tmp
INVALID /tmp/xfile
FILE /etc/passwd
2 ERRORS

Hint: See the read command

Note: Do not use global variables in the function as the function is also tested outside the script. Use return value.

Create a script that parses an HTML page and finds references to PDF files. Do not download the HTML page, read it from stdin. Your script:

1. reads an HTML page from the stdin ,
2. crawls (parses) the HTML page and finds all references to PDF files (loook for PDF/pdf suffix), and
3. prints onto stdout a full URL path of the PDF file
Moreover, the script receives the URL address of the page as a commandline parameter (the URL contains also the page filename, i.e. ''index.html''). This information is usefull when the PDF link is relative.

The script should not provide any other output than specified. An incorrect script usage should be reported to stderr. Stderr can also be used for debuggin purposes.

- You can expect that the HTML page has maximally one PDF reference per line and that the ''<a ...>'' tag is not divided into multiple lines.
- URLs of the PDF files are specified absolutely (including ''http://...'') only.

Example output: (red lines are the standard error output)

http://muj.server.cz/prednaska01.pdf
http://muj.server.cz/cviceni/cviceni.pdf
Hint: see the commands sed, grep.