Chapter 1 : Shell script FAQs
Expert Level 1 : print commands
Explain all examples and options of “printf” command
Explain all examples and options of “echo” command
Explain how to do colorized and formatted output
Expert Level 2 : File & directory handling commands
Explain all examples and options of “cat” command
Explain all examples and options of “ls” command
Explain all examples and options of “cp” command
Explain all examples and options of “mv” command
Explain all examples and options of “mkdir” command
Explain all examples and options of “rm” command
Explain all examples and options of “chmod” command
Explain all examples and options of “umask” command
Explain all examples and options of “pwd” command
Explain all examples and options of “cd” command
Explain all examples and options of “wc” command
Explain all examples and options of “cmp” command
Explain all examples and options of “comm” command
Explain all examples and options of “diff” command
Explain all examples and options of “dos2unix” command
Explain all examples and options of “unix2dos” command
Explain all examples and options of “gzip” command
Explain all examples and options of “gunzip” command
Explain all examples and options of “tar” command
Explain all examples and options of “zip” command
Explain all examples and options of “unzip” command
Expert Level 3 : Process handling commands
Explain all examples and options of “ps” command
Explain all examples and options of “nice” command
Explain all examples and options of “kill” command
Explain all examples and options of “killall” command
Explain all examples of executing jobs later : “at” and “batch” commands
Explain all examples of executing jobs periodically: “crontab” command
Explain all examples and options of “time” command
Explain all examples of job control commands - fg, bg and jobs
Explain options of catching signals using traps
Expert Level 4 : Filter commands
Explain all examples and options of “pr” command
Explain all examples and options of “head” command
Explain all examples and options of “tail” command
Explain all examples and options of “cut” command
Explain all examples and options of “paste” command
Explain all examples and options of “sort” command
Explain all examples and options of “uniq” command
Explain all examples and options of “tr” command
Explain all examples and options of “grep” command
Explain all examples and options of “egrep” command
Explain basic search and replace examples and options of “sed” command
Expert Level 5 : Basic Networking commands
Explain all examples and options of “ifconfig” command
Explain all examples and options of “ping” command
Explain all examples and options of “telnet” command
Explain all examples and options of “ftp” command
Explain all examples and options of “sftp” command
Explain all examples and options of “ssh” command
Expert Level 6 : Basic Wireless commands
Explain all examples and options of “iwconfig” command
Explain all examples and options of “iwpriv” command
Explain all examples and options of “iwlist” command
Explain all examples and options of “iw” command
Explain all examples and options of “wpa_cli” command
Explain all examples and options of “hostapd_cli” command
Expert Level 7 : Basic environment variables
Explain use of $HOME
Explain use of $PWD
Explain use of $PATH
Expert Level 8 : Basic shell meta-characters
Explain all examples of “<” symbol
Explain all examples of “<<” symbol
Explain all examples of “>” symbol
Explain all examples of “>>” symbol
Explain all examples of “|” symbol
Explain all examples of “&” symbol
Explain with examples uses of “* “
Explain with examples uses of “.”
Explain with examples uses of “[ ]”
Explain with examples uses of “?”
Explain with examples uses of “$”
Explain with examples uses of “"
Explain with examples uses of “()”
Explain with examples uses of “$(cmd)”
Explain with examples uses of cmd
Explain with examples uses of ||
Explain with examples uses of &&
Explain with examples uses of $?
Explain with examples uses of $*
Explain with examples uses of $1
Explain with examples uses of #
Expert Level 9 : Basic shell syntax
Using Shell Variables: Variable Names, Defining Variables, Accessing Values, Unsetting Variables
Explain syntax & rules of “if, then, endif”
Explain syntax & rules of “if, then, elif, endif”
Explain syntax & rules of “for”
Explain syntax & rules of “while”
Explain syntax & rules of “until”
Explain syntax & rules of “case, esac”
Explain syntax & rules of defining functions
Explain syntax & use of “set” and “shift” : To manipulate positional parameters
Shell Functions: Pass Parameters, Returning Values, Nested Functions
Explain syntax & rules of using “break, continue” with “for” or “while”
Expert Level 10 : Basic shell arrays
Shell Arrays: assigning and printing array elements one by one, access all the items using ${a[@]}
Expert Level 11 : Basic shell operators
Explain with examples syntax & uses of Arithmetic Operators
Explain with examples syntax & uses of Relational Operators
Explain with examples syntax & uses of Boolean Operators
Explain with examples syntax & uses of String Operators
Explain with examples syntax & uses of File test Operators
Expert Level 12 : Regular expressions
Explain list of Regular expression metacharacters
Explain Character ranges
Explaim Character classes
Expert Level 13 : File operations programs
Write a shell script to check for the existance of a file
Write a shell script which continously appends data into a file.
Write a shell script to check the size of a file
Write a shell script to take a back up of a file when the file size exceeds 5kb
Write a shell script to delete a file in a specific path
Write a shell script to delete all files which have a specific pattern in the file name periodically
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
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
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
Write a shell script to search and print how many instances of stringx pattern are present per file
Write a shell script which reads a file one line at a time and prints each line on the console
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
- 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
Write a shell script which prints the first word of each line
Write a shell script which prints the 5th word of each line
- 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 | |--------------------------------------------------|"
Write a shell script which checks if a file is of type “regular”
Write a shell script which checks if a file is of type “directory”
Write a shell script which checks if a file is of type “symbolic link”
Write a shell script which checks if a file is of type “FIFO special”
Write a shell script which checks if a file is of type “block special”
Write a shell script which checks if a file is of type “character special”
Write a shell script which checks if a file is of type “socket”
Write a shell script which checks whether a file has read, write and/or execute permissions
Write a shell script which creates symbolic links in folderX for all files in folderY
- 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
- 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
Shell script to read a string from user input and print the string
Shell script to read a string from user input and print the length of the string
Shell script to read a string from user input and print the string in reverse
Shell script to compare 2 strings and print if they match or do not match
Shell script to compare 2 strings and check if string1 is a substring of string2
Shell script to take 3 strings as user input, concatenate the 2 strings and print the concatenated string as output
Shell script to define a multiline string and print the multiline string in the same multiline format
- 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
Shell script to convert all strings to upper case
Shell script to convert all strings to lower case
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.
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
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
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
- 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
- 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
Shell script to check if a string is a proper IPv4 address or not
Shell script to check if a string is a proper MAC address or not
Shell script to rearrange a string in alphabetic order
Shell script to trim all leading spaces in a string or array of strings
Shell script to trim all trailing spaces in a string or array of strings
Shell script to remove leading spaces, trailing spaces and white spaces in a string or array of strings
Shell script to create and print an array of strings
Shell script to search a word in an array of strings and print the number of occurances
Shell script to search letter ‘a’ and replace it with letter ‘k’ in a string
Shell script to search letter ‘a’ and replace it with letter ‘k’ in an array of strings
Shell script to reverse a string using for or while loop
Shell script to reverse all strings in an array of strings
Shell script to convert all characters to capital letters in a string
Shell script to convert all characters to capital letters in all strings in an array of strings
Shell script to convert all characters to small letters in a string
Shell script to convert all characters to small letters in all strings in an array of strings
Shell script to replace occurance of word “pencil” with “pen” in a string
Shell script to replace occurance of word “pencil” with “pen” in all strings in an array of strings
Shell script to search letter ‘a’ and delete it in a string
Shell script to search letter ‘a’ and delete it in an array of strings
Shell script to delete occurance of the word “paper” in a string
Shell script to delete occurance of the word “paper” in all strings in an array of strings
Shell script to print A-Z and a-z
Shell script to count number of new lines in a string or array of strings
Shell script to check if a string is a palindrome
Shell script to check if two strings are anagram of each other
Shell script to count number of words in a string where each word is separated by space
Shell script to sort the strings in an array of strings similar to a dictionary
Shell script to sort all characters in all strings in an array of strings
Shell script to extract information from an excel sheet and store it in a proper data structure in RAM
Shell script to find out smallest word in an english statement
Shell script to find out longest word in an english statement
Expert Level 15 : Integer operation programs
Shell script to find if a number is odd or even
Shell script to find if a number is prime or not
Shell script to find the Nth fibonacci number
Shell script to print the fibonacci series
Shell script to find the factorial of a number
Shell script to reverse an integer. Input : 12345, Output : 54321
Shell script to find the power of a number; Input : Base, power Output : (Base)^Power
Shell script to print all prime numbers till 1000
Shell script to print all perfect square numbers till 1000. 1, 4, 9, 16, 25 etc.,
Shell script to check if a number is perfect cube or not
Shell script to swap two numbers stored in two different variables
Shell script to find sum of all numbers in an array of integers
Shell script to find product of all numbers in an array of integers
Shell script to remove duplicate integers in an array of integers
Shell script to add only even numbers in an array of integers
Shell script to add only odd numbers in an array of integers
Shell script to print all negative numbers in an array of integers
Shell script to count number of integers which are palindrome numbers in an array of integers
Shell script to count number of perfect cubes in an array of integers
Shell script to find the square root of a number
Shell script to find all the divisors of a given number
Shell script to print the multiplicatio tables of a given number from 1 to 10
Shell script to perform matrix addition
Shell script to perform matrix multiplication
Shell script to reverse the contents of an integer array
Shell script to find the smallest element and it’s position in an array
Shell script to find the larget element and it’s position in an array
Shell script to convert to “centimeters” to “inches”
Shell script to convert seconds to microseconds
Shell script to swap two numbers without using temporary variables
Shell script to find the sum of all negative numbers in an array
Shell script to find LCM and HCF
Shell script to add all digits in a given integer
Expert Level 16 : Colorized and formatted output
Explain how to do colorized and formatted output
echo command to print the text “PASS” in green
echo command to highlight the text “PASS” in green
echo command to print the text “PASS” in green and in bold
echo command to print the text “PASS” in green and in italics
echo command to print the text “PASS” in green and underlined
echo command to print the text “PASS” in green and with the text highlighted in white
echo command to print the text “FAIL” in red
echo command to highlight the text “FAIL” in red
Expert Level 17 : Functions
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.
Write a shell script which passes 2 integer arguments to a function and prints the arguments inside the function
Write a shell script which passes 1 integer argument and 1 string argument to a function and prints the arguments inside the function
Write a shell script which passes a variable number of arguments to a function and prints the arguments inside the function
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).
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.
Write a shell script with a function which returns a string value
Write a shell script with a function which returns an integer value
Write a shell script with a function which returns an array
Expert Level 18 : awk
- 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
awk command to simply print the contents of file.txt: awk {print} file.txt
awk command to print only the first column of the contents of file.txt: awk -F’ ‘ ‘{print $1}’ file.txt
awk command to print only the third column of the contents of file.txt: awk -F’ ‘ ‘{print $2}’ file.txt
awk command to print the list of unique occupations in file.txt
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)
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)
awk command to print lines which match a given pattern: awk ‘/Actor/ {print}’ file.txt
awk command to print the number of lines/records in file.txt: awk ‘{print NR}’ file.txt
awk command to print the number of columns/fields in a file.txt: awk ‘{print NF}’ file.txt
awk command to display the contents of the file with line numbers for each record/line: awk ‘{print NR, $0}’ file.txt
awk command to display the contents of the file with each field seperated with a “|” symbol: awk ‘{print $1 ” | ” $2 ” | ” $3}’ file.txt
awk command to display only non-empty lines: awk ‘NF > 0’ file.txt
awk command to display longest line
awk command to display longest word
awk command to search for a specific string only in the occupation column
awk command to print the contents of the file where ID value should be modified to ID*100 and displayed
Expert Level 19 : sed
- 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"
sed command to occurances of the string “Actor” with “Performer”
sed command to delete all lines which match the string “Teacher”
sed command to replace the 3rd line with a new record “300 Terry Author”
sed command to add a new record before the 3rd line “201 Niel Author”
sed command to add a new record after the 3rd line “301 Jordan Author”
sed command to replace a line which has 2 patterns “Stephen” and “Actor” with “701 Stepanie Actress”
sed command to print all empty lines
sed command to add a blank line after every record: sed G file.txt
- 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
- 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
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
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
find command to list all files whose names start with “actor”; give an exmple of pattern matching for both case sensitive and insensitive
find command to find all files whose names have the string “salary” present and display only the salary column from all the files
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
find command to find a specific entry in intern_* files and move that entry to doctor_* files respectively using a single command
find command to find files of type socket in a specific path
find command to find files of type directory in a specific path
find command to find all shell scripts(.sh) in a specific path and execute them one by one using a single command
find command to find and remove any files with duplicate names in a single command
find command to remove all files which are empty using a single command
find command to display files which changed in the last hour
find command to list files in groups according to size (> 2GB, 1GB to 2GB, 50MB to 1GB and <50MB)
Expert Level 21 - Misc programs
Give a working example of a while loop that can be executed on the terminal(ie without using a shell script)
Shell script to convert output of date command to 3 different time zones