Lab Assignment--Shell Scripts


Lab Code

To get the sample and exercise code, please use the following commands in your cs330 directory:
   curl -O -s https://www.labs.cs.uregina.ca/330/Shell/Lab11.zip
   unzip Lab11.zip

You will be creating a shell script that can explore a directory structure and remove compiled C/C++ programs. Your code will be broken down into tasks:

1. Input Validation

The script begins by verifying the command-line argument (in one if statement). It ensures that:

If either condition fails, the script stops immediately using exit.

2. Iterating Through Directory Contents

A loop is used to examine every item inside the given directory. Each entry is checked to determine its type:

3. Recursive Behavior

Instead of using a separate function, the script invokes itself when it encounters a subdirectory. This is done by calling the script using its own filename and passing the new directory as an argument.

4. Identifying Executable Files

It uses the "file" command that inspects file content rather than relying on file extensions or executable permissions.

The Unix file command is a simple but very useful tool that tells you what kind of data is inside a file. It does not base its output on file extensions (like .jpg or .txt), but rather on the contents of the file.

A couple of examples of using file:

ntemp05@os2:~/cs330/Lab5$ ls
Samples  demo  functions.c  functions.h  functions.o  main.c  main.o  makefile
ntemp05@os2:~/cs330/Lab5$ file main.c
main.c: C source, ASCII text
ntemp05@os2:~/cs330/Lab5$ file demo
demo: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter
/lib64/ld-linux-x86-64.so.2, BuildID[sha1]=ae9d2c96d72bd5888dc2fdfd2b133d8b0dd627cb, for
GNU/Linux 3.2.0, with debug_info, not stripped
ntemp05@os2:~/cs330/Lab5$ file main.o
main.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), with debug_info, not stripped
ntemp05@os2:~/cs330/Lab5$ file makefile
makefile: ASCII text

If the file description contains indicators of a binary executable format (such as ELF on Linux systems), the file is considered a compiled C/C++ program. Hint: use "grep".

5. File Removal

Once a file is confirmed to be a compiled executable, it is deleted. You might want to use a flag with the rm to confirm that you want to remove the file. Prior to testing the rm, you might want to use echo to confirm what files will be removed.

Summary

In short, the script:

  1. Validates input
  2. Loops through directory contents
  3. Recursively processes subdirectories
  4. Detects compiled executables
  5. Deletes those executables

Sample Run:

You might want to copy you script into your cs330 directory.
temp05@os1:~/cs330$ ./cleanup.sh
Error: Argument is not provided or  is not a directory
ntemp05@os1:~/cs330$ ./cleanup.sh cleanup.sh
Error: Argument is not provided or cleanup.sh is not a directory
temp05@os1:~/cs330$ ./cleanup.sh Lab6
rm: remove regular file 'Lab6/bad'? y
rm: remove regular file 'Lab6/good'? y
temp05@os1:~/cs330$ ./cleanup.sh Lab5
Recursively entering Lab5/Samples
rm: remove regular file 'Lab5/Samples/p11'? n
rm: remove regular file 'Lab5/Samples/p11b'? n
rm: remove regular file 'Lab5/Samples/p11c'? n
rm: remove regular file 'Lab5/demo'? n
rm: remove regular file 'Lab5/functions.o'? n
rm: remove regular file 'Lab5/main.o'? n
temp05@os1:~/cs330$    

Assessment

You will be asked to explain at least three lines of code. You will be under more scrunity if you have done something out of the lab presentation.

Deliverables:

Submit 2 files to URCourses:

  1. code of bash shell script
  2. script of the run showing run like sample above