Assignment 8

Step-1: Use vi or nano editor to write your script (Ex, nano lkopa.sh) for the following tasks.
Step-2: After saving the script, save and exit out of the editor and make the script executable by adding execute permission ( chmod +x lkopa.sh)
Step-3: Run your script using ./lkopa.sh

Task A (Correct script (25 points) + result/output after executing the script (25 points)- Conditional Statement Write a shell script using nano or vi editor (eg, nano lkopa.sh) like below, that performs the following task:

  1. Add the Shebang (#!/bin/bash) as the first line in your script.
  2. Read a number using read function
  3. Using if statement, check if the input number is less than 10, then print the message “Input number is greater than 10.
  4. If the number is not greater than 10, then print the message, “Input number is not greater than 10.

Task B (Correct script (25 points) + result/output after executing the script (25 points) – Shell Script to Create a new file Write a shell script using nano or vi editor (eg, nano lkopa.sh) like below, that performs the following task:

  1. Add the Shebang (#!/bin/bash) as the first line in your script.
  2. Reads the name of the file to check for a filename that exists.
  3. Check whether the given input is a directory or regular file.
  4. If the input is a directory and exists, then display the message “Directory exists”.
  5. If the input is a regular file, then display the message “It is a regular file, and the file exists” and display the contents of the file.
  6. If the given input name in step-1 doesn’t exist, then create the new file with the given name in step-1.
    (Extra credit: 10 points) Add your name to the file (using redirection operator ‘>’) and display the contents for the newly created file.
  7. Save and exit the editor and remember to make the script executable using the command chmod +x lkopa.sh)

Extra Credit(15 points)- Check Directory Write a script like below that

  1. Reads Two variables- your name and the name of the directory as input.
  2. Your script should check for the validity of the given directory name, if the entered filename is a directory, then display its contents
  3. If the directory doesn’t exist, then print an error message “Sorry, the entered directory name is not a valid directory name.”
  4. You need to execute your script and test the following directories to test with your script
  • /etc/systemd
  • /home
  • A directory that does not exist
    Display the contents for the directories you have entered

Leave a Reply

Your email address will not be published. Required fields are marked *