Skip to main content

Bash Args

Learning outcomes

  1. Practice the use of Git and GitHub.
  2. Practice writing Bash scripts that use switches and arguments.
  3. Practice the use of Bash to code solutions to technical problems.

Goals

  • Practice problem solving.
  • Practice Bash fundamentals.
  • Practice using the official documentation

Guidelines

  • This assignment utilized automated-tests to check for the correctness of your code. These tests can be a bit specific about what you name things
    • Please follow the instructions on this assignment for the best outcome.
  • Make a habit of committing and pushing your code frequently to GitHub.
  • Make sure to meaningful commit messages that describe what you did.
  • Make sure the automated tests and checks continue to pass after you make changes.
  • Don't forget to fill out the self assessment on the README file

Instructions

  1. Accept the Assignment on Canvas.
  • This will create a repository for you with a few starting files.
      .
    ├── .vscode
    │ └── settings.json
    ├── my_script.sh ⬅️
    ├── test_my_script.sh
    └── README.md 🔼: update this once done
  • You will only be working on the files marked with the arrows
  1. Clone down the repository to your linux VM.

  2. Run the project tests by running each test script with bash.

  3. Our task would be to implement the functions in the files marked with ⬅️ arrow above until all the tests pass.

  4. At the end of each test, commit your code commit logo git commit -m "finished test xyz".

  5. DON'T FORGET to Update the README.md file with your self assessment

    • Your grade
    • Self Reflection
    • How long it took you to complete the assignment
  6. Push your code.

Requirements

  1. The script should be named my_script.sh.
  2. The script must accept the following switches and corresponding arguments:
    • -h: Display help information about the script and its usage.
    • -f <filename>: Specify the input filename.
    • -o <output_dir>: Specify the output directory where the results will be stored.
    • -t <type>: Specify the type of action to perform. The supported types are "process" and "analyze".
  3. The script must validate user input for the switches and arguments:
    • The -h switch should display the script's usage information.
    • The -f switch should check if the specified filename exists and is a regular file.
    • The -o switch should check if the specified output directory exists and is a directory.
    • The -t switch should accept only "process" or "analyze" as valid arguments.
  4. The script should implement the following functionalities based on the selected type:
    • If the user selects the "process" type, the script should perform a specific processing task on the input file and store the output in the specified output directory.
    • If the user selects the "analyze" type, the script should perform an analysis on the input file and generate a report in the specified output directory.
    • Be creative here! You can implement any processing or analysis task you want. For example, you can implement a script that processes a text file and generates a report containing the number of words in the file, or a script that analyzes a CSV file and generates a report containing the number of rows and columns in the file.
  5. Proper usage and error messages should be displayed when incorrect or insufficient arguments are provided.
  6. The script should be well-documented, with comments explaining its functionality and usage.

Example Usages

  1. Display help information:
    ./my_script.sh -h
  2. Process a file and store the output in the specified directory:
    ./my_script.sh -f input.txt -o output -t process
  3. Analyze a file and generate a report in the specified directory:
    ./my_script.sh -f input.txt -o output -t analyze

Resources