Chapter 1 : Shell script FAQs

Expert Level 1 : print commands

  1. Explain all examples and options of “printf” command

  2. Explain all examples and options of “echo” command

  3. Explain how to do colorized and formatted output

Expert Level 2 : File & directory handling commands

  1. Explain all examples and options of “cat” command

  2. Explain all examples and options of “ls” command

  3. Explain all examples and options of “cp” command

  4. Explain all examples and options of “mv” command

  5. Explain all examples and options of “mkdir” command

  6. Explain all examples and options of “rm” command

  7. Explain all examples and options of “chmod” command

  8. Explain all examples and options of “umask” command

  9. Explain all examples and options of “pwd” command

  10. Explain all examples and options of “cd” command

  11. Explain all examples and options of “wc” command

  12. Explain all examples and options of “cmp” command

  13. Explain all examples and options of “comm” command

  14. Explain all examples and options of “diff” command

  15. Explain all examples and options of “dos2unix” command

  16. Explain all examples and options of “unix2dos” command

  17. Explain all examples and options of “gzip” command

  18. Explain all examples and options of “gunzip” command

  19. Explain all examples and options of “tar” command

  20. Explain all examples and options of “zip” command

  21. Explain all examples and options of “unzip” command

Expert Level 3 : Process handling commands

  1. Explain all examples and options of “ps” command

  2. Explain all examples and options of “nice” command

  3. Explain all examples and options of “kill” command

  4. Explain all examples and options of “killall” command

  5. Explain all examples of executing jobs later : “at” and “batch” commands

  6. Explain all examples of executing jobs periodically: “crontab” command

  7. Explain all examples and options of “time” command

  8. Explain all examples of job control commands - fg, bg and jobs

  9. Explain options of catching signals using traps

Expert Level 4 : Filter commands

  1. Explain all examples and options of “pr” command

  2. Explain all examples and options of “head” command

  3. Explain all examples and options of “tail” command

  4. Explain all examples and options of “cut” command

  5. Explain all examples and options of “paste” command

  6. Explain all examples and options of “sort” command

  7. Explain all examples and options of “uniq” command

  8. Explain all examples and options of “tr” command

  9. Explain all examples and options of “grep” command

  10. Explain all examples and options of “egrep” command

  11. Explain basic search and replace examples and options of “sed” command

Expert Level 5 : Basic Networking commands

  1. Explain all examples and options of “ifconfig” command

  2. Explain all examples and options of “ping” command

  3. Explain all examples and options of “telnet” command

  4. Explain all examples and options of “ftp” command

  5. Explain all examples and options of “sftp” command

  6. Explain all examples and options of “ssh” command

Expert Level 6 : Basic Wireless commands

  1. Explain all examples and options of “iwconfig” command

  2. Explain all examples and options of “iwpriv” command

  3. Explain all examples and options of “iwlist” command

  4. Explain all examples and options of “iw” command

  5. Explain all examples and options of “wpa_cli” command

  6. Explain all examples and options of “hostapd_cli” command

Expert Level 7 : Basic environment variables

  1. Explain use of $HOME

  2. Explain use of $PWD

  3. Explain use of $PATH

Expert Level 8 : Basic shell meta-characters

  1. Explain all examples of “<” symbol

  2. Explain all examples of “<<” symbol

  3. Explain all examples of “>” symbol

  4. Explain all examples of “>>” symbol

  5. Explain all examples of “|” symbol

  6. Explain all examples of “&” symbol

  7. Explain with examples uses of “* “

  8. Explain with examples uses of “.”

  9. Explain with examples uses of “[ ]”

  10. Explain with examples uses of “?”

  11. Explain with examples uses of “$”

  12. Explain with examples uses of “"

  13. Explain with examples uses of “()”

  14. Explain with examples uses of “$(cmd)”

  15. Explain with examples uses of cmd

  16. Explain with examples uses of ||

  17. Explain with examples uses of &&

  18. Explain with examples uses of $?

  19. Explain with examples uses of $*

  20. Explain with examples uses of $1

  21. Explain with examples uses of #

Expert Level 9 : Basic shell syntax

  1. Using Shell Variables: Variable Names, Defining Variables, Accessing Values, Unsetting Variables

  2. Explain syntax & rules of “if, then, endif”

  3. Explain syntax & rules of “if, then, elif, endif”

  4. Explain syntax & rules of “for”

  5. Explain syntax & rules of “while”

  6. Explain syntax & rules of “until”

  7. Explain syntax & rules of “case, esac”

  8. Explain syntax & rules of defining functions

  9. Explain syntax & use of “set” and “shift” : To manipulate positional parameters

  10. Shell Functions: Pass Parameters, Returning Values, Nested Functions

  11. Explain syntax & rules of using “break, continue” with “for” or “while”

Expert Level 10 : Basic shell arrays

  1. Shell Arrays: assigning and printing array elements one by one, access all the items using ${a[@]}

Expert Level 11 : Basic shell operators

  1. Explain with examples syntax & uses of Arithmetic Operators

  2. Explain with examples syntax & uses of Relational Operators

  3. Explain with examples syntax & uses of Boolean Operators

  4. Explain with examples syntax & uses of String Operators

  5. Explain with examples syntax & uses of File test Operators

Expert Level 12 : Regular expressions

  1. Explain list of Regular expression metacharacters

  2. Explain Character ranges

  3. Explaim Character classes

Expert Level 13 : File operations programs

  1. Write a shell script to check for the existance of a file

  2. Write a shell script which continously appends data into a file.

  3. Write a shell script to check the size of a file

  4. Write a shell script to take a back up of a file when the file size exceeds 5kb

  5. Write a shell script to delete a file in a specific path

  6. Write a shell script to delete all files which have a specific pattern in the file name periodically

  7. Write a shell script to check when the disk space left is less than a specific threshold and delete all files whose names match a specific pattern

  8. Write a shell script which takes a string as an input argument; The script must search for the presence of the string in multiple files and print the list of files which have that string pattern

  9. Write a shell script which takes a string as an input argument; The script must search for the presence of the string in multiple files and print the list of files which DOES NOT MATCH that string pattern

  10. Write a shell script to search and print how many instances of stringx pattern are present per file

  11. Write a shell script which reads a file one line at a time and prints each line on the console

  12. Write a shell script which reads a file one line at a time and appends the line to another file if the line matches a specific string pattern

  13. Write a shell script which reads a file one line at a time and prints the number of words in each line; The script must also print the total word count by summing up the words in each line.
    Example output:
    Word count in line 01: 10
    Word count in line 02: 22
    ...
    Word count in line 20: 12
    Total word count: 10 + 22 +...+ 12 = 44
    
  14. Write a shell script which prints the first word of each line

  15. Write a shell script which prints the 5th word of each line

  16. Write a shell script which reads a file containing data separated by delimiters and displays the same data with formatting.
    Example:
    1. Input file contents:
    ID;Name;Age;Occupation
    100;Taylor;24;Singer
    101;Jack;20;Student
    102;Jenny;30;Doctor
    
    2. Output
    |--------------------------------------------------|
    | ID    |    Name      |  Age    |    Occupation   |
    | 100   |    Taylor    |  24     |    Singer       |
    | 101   |    Jack      |  20     |    Student      |
    | 102   |    Jenny     |  30     |    Doctor       |
    |--------------------------------------------------|"
    
  17. Write a shell script which checks if a file is of type “regular”

  18. Write a shell script which checks if a file is of type “directory”

  19. Write a shell script which checks if a file is of type “symbolic link”

  20. Write a shell script which checks if a file is of type “FIFO special”

  21. Write a shell script which checks if a file is of type “block special”

  22. Write a shell script which checks if a file is of type “character special”

  23. Write a shell script which checks if a file is of type “socket”

  24. Write a shell script which checks whether a file has read, write and/or execute permissions

  25. Write a shell script which creates symbolic links in folderX for all files in folderY

  26. Write a single shell script to:
    1. Take 2 file names as command line arguments
    2. Check for the existance of each file. If not present throw an error
    3. Search and replace all instances of string1 with string2 using ""sed"" command
    
  27. Write a shell script which:
    1. can take an unlimited number of file names as command line arguments and,
    2. removes duplicate lines in all the files
    

Expert Level 14 : String operation programs

  1. Shell script to read a string from user input and print the string

  2. Shell script to read a string from user input and print the length of the string

  3. Shell script to read a string from user input and print the string in reverse

  4. Shell script to compare 2 strings and print if they match or do not match

  5. Shell script to compare 2 strings and check if string1 is a substring of string2

  6. Shell script to take 3 strings as user input, concatenate the 2 strings and print the concatenated string as output

  7. Shell script to define a multiline string and print the multiline string in the same multiline format

  8. Shell script to define the multiline string below and print the number of occurances of the word “”it”” and “”for”” in the string
    Fog everywhere.
    Fog up the river, where it flows among green aits and meadows;
    fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city.
    Fog on the Essex marshes, fog on the Kentish heights.
    Fog creeping into the cabooses of collier-brigs;
    fog lying out on the yards and hovering in the rigging of great ships;
    fog drooping on the gunwales of barges and small boats.
    
    Expected output: 
    "it" count: 2
    "Fog" count: 8
    
  9. Shell script to convert all strings to upper case

  10. Shell script to convert all strings to lower case

  11. Shell script to compare 2 strings both of which have upper and lower case characters. The script should check whether the strings match by ignoring the (upper/lower) case differences.

  12. Shell script to take a string(alphanumeric/numeric/alphabetic/with-or-without-special-characters) as input from the user and strip all numbers from the string

  13. Shell script to take a string(alphanumeric/numeric/alphabetic/with-or-without-special-characters) as input from the user and strip all alphabets from the string

  14. Shell script to take a string(alphanumeric/numeric/alphabetic/with-or-without-special-characters) as input from the user and strip all astrix(*) characters from the string if they are present

  15. Shell script to print the longest occurance of repeating characters in a string.
    Input: a b c c c d e f f g h i i c c c c d f f
    Output: 4 contiguous occurances of c
    
  16. Shell script to print the longest occurance of non-repeating(contiguous and unique) characters in a string.
    1. Input: a b c c c d e f f g h i j i c c c c d f f
        Output: Longest non-repeating string is ""f g h i j"" of length 5 at position 9
    
    2. Input: a b c c d e d f e g h i j k i c c c c d f f
        Output: Longest non-repeating string is ""e g h i j k"" of length 6 at position 9
    
  17. Shell script to check if a string is a proper IPv4 address or not

  18. Shell script to check if a string is a proper MAC address or not

  19. Shell script to rearrange a string in alphabetic order

  20. Shell script to trim all leading spaces in a string or array of strings

  21. Shell script to trim all trailing spaces in a string or array of strings

  22. Shell script to remove leading spaces, trailing spaces and white spaces in a string or array of strings

  23. Shell script to create and print an array of strings

  24. Shell script to search a word in an array of strings and print the number of occurances

  25. Shell script to search letter ‘a’ and replace it with letter ‘k’ in a string

  26. Shell script to search letter ‘a’ and replace it with letter ‘k’ in an array of strings

  27. Shell script to reverse a string using for or while loop

  28. Shell script to reverse all strings in an array of strings

  29. Shell script to convert all characters to capital letters in a string

  30. Shell script to convert all characters to capital letters in all strings in an array of strings

  31. Shell script to convert all characters to small letters in a string

  32. Shell script to convert all characters to small letters in all strings in an array of strings

  33. Shell script to replace occurance of word “pencil” with “pen” in a string

  34. Shell script to replace occurance of word “pencil” with “pen” in all strings in an array of strings

  35. Shell script to search letter ‘a’ and delete it in a string

  36. Shell script to search letter ‘a’ and delete it in an array of strings

  37. Shell script to delete occurance of the word “paper” in a string

  38. Shell script to delete occurance of the word “paper” in all strings in an array of strings

  39. Shell script to print A-Z and a-z

  40. Shell script to count number of new lines in a string or array of strings

  41. Shell script to check if a string is a palindrome

  42. Shell script to check if two strings are anagram of each other

  43. Shell script to count number of words in a string where each word is separated by space

  44. Shell script to sort the strings in an array of strings similar to a dictionary

  45. Shell script to sort all characters in all strings in an array of strings

  46. Shell script to extract information from an excel sheet and store it in a proper data structure in RAM

  47. Shell script to find out smallest word in an english statement

  48. Shell script to find out longest word in an english statement

Expert Level 15 : Integer operation programs

  1. Shell script to find if a number is odd or even

  2. Shell script to find if a number is prime or not

  3. Shell script to find the Nth fibonacci number

  4. Shell script to print the fibonacci series

  5. Shell script to find the factorial of a number

  6. Shell script to reverse an integer. Input : 12345, Output : 54321

  7. Shell script to find the power of a number; Input : Base, power Output : (Base)^Power

  8. Shell script to print all prime numbers till 1000

  9. Shell script to print all perfect square numbers till 1000. 1, 4, 9, 16, 25 etc.,

  10. Shell script to check if a number is perfect cube or not

  11. Shell script to swap two numbers stored in two different variables

  12. Shell script to find sum of all numbers in an array of integers

  13. Shell script to find product of all numbers in an array of integers

  14. Shell script to remove duplicate integers in an array of integers

  15. Shell script to add only even numbers in an array of integers

  16. Shell script to add only odd numbers in an array of integers

  17. Shell script to print all negative numbers in an array of integers

  18. Shell script to count number of integers which are palindrome numbers in an array of integers

  19. Shell script to count number of perfect cubes in an array of integers

  20. Shell script to find the square root of a number

  21. Shell script to find all the divisors of a given number

  22. Shell script to print the multiplicatio tables of a given number from 1 to 10

  23. Shell script to perform matrix addition

  24. Shell script to perform matrix multiplication

  25. Shell script to reverse the contents of an integer array

  26. Shell script to find the smallest element and it’s position in an array

  27. Shell script to find the larget element and it’s position in an array

  28. Shell script to convert to “centimeters” to “inches”

  29. Shell script to convert seconds to microseconds

  30. Shell script to swap two numbers without using temporary variables

  31. Shell script to find the sum of all negative numbers in an array

  32. Shell script to find LCM and HCF

  33. Shell script to add all digits in a given integer

Expert Level 16 : Colorized and formatted output

  1. Explain how to do colorized and formatted output

  2. echo command to print the text “PASS” in green

  3. echo command to highlight the text “PASS” in green

  4. echo command to print the text “PASS” in green and in bold

  5. echo command to print the text “PASS” in green and in italics

  6. echo command to print the text “PASS” in green and underlined

  7. echo command to print the text “PASS” in green and with the text highlighted in white

  8. echo command to print the text “FAIL” in red

  9. echo command to highlight the text “FAIL” in red

Expert Level 17 : Functions

  1. Write a shell script which uses a function to print a fixed string. The script should be able to call this function any number of times.

  2. Write a shell script which passes 2 integer arguments to a function and prints the arguments inside the function

  3. Write a shell script which passes 1 integer argument and 1 string argument to a function and prints the arguments inside the function

  4. Write a shell script which passes a variable number of arguments to a function and prints the arguments inside the function

  5. Write a shell script which passes multiple arguments to a function and prints all the arguments in one shot(find the special character used to print all the arguments in one go).

  6. Write a shell script to take 2 numbers as input from the user. Write separate functions to print their values after addition, multiplication, division and subtraction.

  7. Write a shell script with a function which returns a string value

  8. Write a shell script with a function which returns an integer value

  9. Write a shell script with a function which returns an array

Expert Level 18 : awk

  1. Create sample input file to be used in the script exercises below
    $ cat file.txt
    ID  NAME        OCCUPATION
    1   Taylor      Singer
    2   Robert      Actor
    3   Nicole      Engineer
    4   Pennywise   Performer
    5   Hugh        Carpenter
    6   Jason       Actor
    7   Stephen     Actor
    8   Angelina    Doctor
    9   Kristin     Teacher
    10  Kristin     Teacher
    1   Taylor      Singer
    
    
  2. awk command to simply print the contents of file.txt: awk {print} file.txt

  3. awk command to print only the first column of the contents of file.txt: awk -F’ ‘ ‘{print $1}’ file.txt

  4. awk command to print only the third column of the contents of file.txt: awk -F’ ‘ ‘{print $2}’ file.txt

  5. awk command to print the list of unique occupations in file.txt

  6. awk command to ignore duplicate lines and print only unique lines from file.txt (ie script should identify that the last entry for Taylor is a duplicate and should not be printed)

  7. awk command to ignore duplicate {name+occupation} entries and print only unique {name+occupation} entries in the file.txt (i.e script should identify that both ID 9 and 10 have same {name+occupation} and only 1 should be printed)

  8. awk command to print lines which match a given pattern: awk ‘/Actor/ {print}’ file.txt

  9. awk command to print the number of lines/records in file.txt: awk ‘{print NR}’ file.txt

  10. awk command to print the number of columns/fields in a file.txt: awk ‘{print NF}’ file.txt

  11. awk command to display the contents of the file with line numbers for each record/line: awk ‘{print NR, $0}’ file.txt

  12. awk command to display the contents of the file with each field seperated with a “|” symbol: awk ‘{print $1 ” | ” $2 ” | ” $3}’ file.txt

  13. awk command to display only non-empty lines: awk ‘NF > 0’ file.txt

  14. awk command to display longest line

  15. awk command to display longest word

  16. awk command to search for a specific string only in the occupation column

  17. awk command to print the contents of the file where ID value should be modified to ID*100 and displayed

Expert Level 19 : sed

  1. Create sample input file to be used in the script exercises below
    $ cat file.txt
    ID    NAME        OCCUPATION
    100   Taylor      Singer
    200   Robert      Actor
    300   Nicole      Engineer
    400   Pennywise   Performer
    500   Hugh        Carpenter
    600   Jason       Actor
    700   Stephen     Actor
    800   Angelina    Doctor
    900   Kristin     Teacher
    102   Kristin     Teacher
    103   Taylor      Singer"
    
  2. sed command to occurances of the string “Actor” with “Performer”

  3. sed command to delete all lines which match the string “Teacher”

  4. sed command to replace the 3rd line with a new record “300 Terry Author”

  5. sed command to add a new record before the 3rd line “201 Niel Author”

  6. sed command to add a new record after the 3rd line “301 Jordan Author”

  7. sed command to replace a line which has 2 patterns “Stephen” and “Actor” with “701 Stepanie Actress”

  8. sed command to print all empty lines

  9. sed command to add a blank line after every record: sed G file.txt

  10. sed command to delete the 5th occurance of the word fog in a file which has the below content:
    Fog everywhere.
    Fog up the river, where it flows among green aits and meadows;
    fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city.
    Fog on the Essex marshes, fog on the Kentish heights.
    Fog creeping into the cabooses of collier-brigs;
    fog lying out on the yards and hovering in the rigging of great ships;
    fog drooping on the gunwales of barges and small boats.
    

Expert Level 20 : find

  1. Create the below list of files inside a folder “”book_keeping”
    $ ls book_keeping/
    actor_Record.txt
    actor_resume.txt
    actor_salary.txt
    doctor_record.txt
    doctor_resume.txt
    doctor_salary.txt
    intern_record.txt
    intern_resume.txt
    intern_Salary.txt
    
  2. find command to check whether “doctor_record.txt” file is present in the “book_keeping” folder; give an exmple of pattern matching for both case sensitive and insensitive

  3. find command to list all files whose names have the string “intern” present; give an exmple of pattern matching for both case sensitive and insensitive

  4. find command to list all files whose names start with “actor”; give an exmple of pattern matching for both case sensitive and insensitive

  5. find command to find all files whose names have the string “salary” present and display only the salary column from all the files

  6. find command to find all files whose names have the string “salary” present and to read the file and change all entries which match “$500” to $550 using a single command

  7. find command to find a specific entry in intern_* files and move that entry to doctor_* files respectively using a single command

  8. find command to find files of type socket in a specific path

  9. find command to find files of type directory in a specific path

  10. find command to find all shell scripts(.sh) in a specific path and execute them one by one using a single command

  11. find command to find and remove any files with duplicate names in a single command

  12. find command to remove all files which are empty using a single command

  13. find command to display files which changed in the last hour

  14. find command to list files in groups according to size (> 2GB, 1GB to 2GB, 50MB to 1GB and <50MB)

Expert Level 21 - Misc programs

  1. Give a working example of a while loop that can be executed on the terminal(ie without using a shell script)

  2. Shell script to convert output of date command to 3 different time zones