Practice Questions

This section contains detailed practice questions for you to practice and get hands-on with the BASH Commands and BASH Scripting

This section contains some practice questions on SHELL with no answers. All questions are self-explanatory and you are free to assume the input and output strategy of these scripts.

  1. Practice variations of commands let, grep, awk using -help

  2. Write a Shell script to add two numbers that are provided as command-line arguments. Display error message if the correct number of arguments are not provided.

  3. Write a Shell script to print the Internet Protocol address of your machine

  4. Add an extension “.try” to all the files in the current directory.

  5. Print all odd Fibonacci numbers that are <=100.

  6. Sort a list of command-line arguments (could be any number of them) only using basic commands, loops, and arrays. Do not use inbuilt sort.

  7. Write a Shell script to verify if the input (numeric/alphabet/alphanumeric) is a Palindrome

  8. Write a Shell script to verify if the input is Numeric or alphabetic or alphanumeric

  9. Write a shell script to print today’s date by using the calendar of the month or cal command. (You are not allowed to use date, awk, or sed command)

  10. List the commands you have used on your terminal (from history) in descending order of usage (frequency).

  11. List the commands you have used on your terminal (from history) in ascending order of usage (frequency).

  12. Consider the following input in the form of a CSV file. The Output contains every set of 3 consecutive rows merged into one line, separated by a colon.

    Input:

    • India, New Delhi

    • Canada, Ottawa

    • Oman, Muscat

    • Egypt, Cairo

    • United Kingdom, London

    • Belgium, Brussels

    • Bahrain, Manama

    • Bangladesh, Dhaka

    • China, Beijing

    • Italy, Rome

    • Japan, Tokyo

    Output:

    India, New Delhi:Canada, Ottawa:Oman, Muscat Egypt, Cairo:United Kingdom, London:Belgium, Brussels Bahrain, Manama:Bangladesh, Dhaka:China, Beijing Italy, Rome:Japan, Tokyo

  13. Answer the below question by addressing the AIM:

    • AIM: To create a document and supporting commands to store songs/music that you listen to, so that you can look back on them when you're bored and trying to find out songs that you've liked in the past. Kind of like a song diary.

    • REQUIREMENTS:

      • Create a storage document, which holds all the details that you add. The simplest form would be some kind of delimited document, but feel free to use multiple documents or even a database to store the information.

      • Create commands(name them whatever you want):

        Add entry:

        • Takes n arguments (your wish as to what data you want to keep).

        • At least 1 (or more) mandatory argument

        • At least 1 (or more) non-essential arguments

        • Suggestion: Song name, artist name, genre, youtube link ...etc

        • Should check to ensure no duplicate

        • Suggestion: Keep an id parameter, would be helpful in identification for editing

        • Use redirection (<, >) to add to the file.

        Edit entry

        • Allows updating the fields of a certain song entry. (ex. fixing spelling mistakes, adding in

          new youtube link).

        • Suggestion: Could be implemented as a Delete + Add

        Delete entry

        • Remove a song entry from your document.

        • Deletion should be achieved in a single command. (See: sed)

        View all entries

        • Put a "nice view" to all the songs you have. (Create a table-like view?)

        View select entries

        • View all the songs satisfying a certain requirement. (ex. all songs of 1 artist, all songs of the same genre)

    • BONUS: Allow for the user to add in custom fields

    • SUBMISSION TYPE:

      • Script to setup the whole system on someone's system. Assume no sudo permissions (For your own use, you may want to move some of your scripts to /usr/local/bin so that they are accessible throughout your system, or add them to your path)

      • Scripts for each of the commands(add, edit, view all, view select, and any additional commands you've included)

  14. Answer the below problem

    A popular time management technique uses a timer to break down work into intervals (usually 25 mins) followed by a small break. The task is to create a BASH script to aid this process. Take a command-line argument for the number of iterations (work + break). Print notifications about the breaks (5 mins) or time to work (25 mins) in the terminal. Every 4 iterations, include a long break (15 mins). Print notification when all the cycles are complete. You are free to change the content of the output to make it more meaningful as you wish but the iteration number needs to be present and the general structure must be the same. Bonus: Instead of printing to the terminal, send notifications. You are free to use your package manager to install additional non-default commands for this task (Only for the bonus and not the rest of the question).

    • Example usage:

      • bash timer.sh 2 Output structure:

        • 1 work

        • 1 break time

        • 2 work

        • 2 break time

        • Finished

  15. Write a bash script that will do the following:

    • Print recursively the last modified date and time and the file name of all the files in the current working directory in a given format. (Use piping)

      Example Output: If your current directory contains two files A and B. Then the output should be as follows:

      Last_Modified_Date_of_A Last_Modified_Time_of_A Relative_path_of_A

      Last_Modified_Date_of_B Last_Modified_Time_of_B Relative_path_of_B

    • Find all the commands that start the word “lo” and store the commands and their small descriptions in a file named your_roll_no.txt(For eg: 201801159.txt). Each line in the file should contain the command along with its small description.

    • Display the number of lines and the length of the longest line in the above-created File.

    • Replace all the occurrences of the word “function” with “method” in the above file. In addition, create a backup of your original file “your_roll_no.txt”.

    • Submission: script1.sh, your_roll_no.txt, backup file

  16. Write a bash script that will do the following:

    Adult income census was conducted and the output of the census contains 2 CSV files named file1.csv and file2.csv. Write a script named script2.sh which will perform the following operations:

    • Concatenate these 2 files into a single file named target_file.csv

    • Create a header file named header.csv which will contain: "Age,workclass,fnlwgt,education,education-num,marital-status,occupation,relationship,race,sex, capital-gain,capital-loss,hours-per-week,native-country,class" - Add this header file at the beginning of the target_file.csv

    • The target_file.csv would contain some missing values denoted by ‘?’, replace these missing values with your roll number.

    • Submission: script2.sh, target_file.csv

  17. Write a script to print all odd Fibonacci numbers that are <= 10946.

  18. Write a script to validate password strength of an inputted string. A strong password follows all the following criteria:

    • Length: Should have a minimum of 8 characters.

    • Contains both alphabet and number.

    • Includes both the small and capital case letters.

    If the password doesn’t comply with any of the above conditions, then the script should report it with the output “Weak Password -> Reason_for_weak_password”, where Reason_for_weak_password =

    • “Should include a lower case letter”

    • “Should include a capital case letter”

    • “Should use numbers in your password”

    • “Password length should have at least 8 characters”

    else it should output “Strong Password”.

    Examples:

    ./q5.sh i_am_a_password

    Output:

    Should use numbers in your password

    Should include a capital case letter

    1. ./q5.sh Qwerty1234

    Output:

    Strong Password

Last updated