Oppe Practice
Oppe Practice
These questions are provided for practising OPPE level questions. They are taken from previous NPPEs and
OPPEs. These are not graded and are not to be turned in.
Problem Statement
Write a script script.sh to execute the bash script run.sh and print "SUCCESS" if it exits with status 0, otherwise
print "FAIL". Note that the file may not have execute permission.
Sample Example
1 #!/bin/bash
2
3 chmod u+x run.sh
4 if ./run.sh &>/dev/null ; then echo SUCCESS
5 else echo FAIL
6 fi
OPPE Practice Question 1.1.1.2
Problem Statement
Write a script script.sh to create a link named storage in the current directory to the directory whose path is
stored in the variable DIR_PATH
Solution
1 #!/bin/bash
2
3 ln -s $DIR_PATH storage
OPPE Practice Question 1.1.1.3
Problem Statement
Write a function parent-of-parent in the script script.sh to return the parent of parent directory of the file/di-
rectory that is passed as an argument.
Note: Parent of / is /.
Solution
1 parent-of-parent(){
2
3 dirname "$(dirname "$1")"
4
5 }
OPPE Practice Question 1.1.1.4
Problem Statement
The poem "Sail away" by Rabindranath Tagore is provided through the standard input (&0).
Write a script script.sh to print the number of non-empty lines that does not contain an article (a, an, the) in it.
The command should print a number that is the count of lines, and should not print the lines.
Solution
1 #!/bin/bash
2
3 grep -vwE 'a|an|the' | grep . -c
OPPE Practice Question 1.1.2.1
Problem Statement
Write a script to verify the exact word "System" or "Commands" (case-sensitive) exist in the standard input.
If any of the words are present in the file, print 0 else 1. Do not print anything else.
Note that the words may occur on separate lines as well.
Solution
1 #!/bin/bash
2
3 grep -we System -we Commands -q
4
5 echo $?
OPPE Practice Question 1.1.2.2
Problem Statement
Write a script script.sh to extract and print the link of all anchor tag present in the standard input.
Note: Assume the starting a tag and href attribute value will be on the same line.
Solution
1 #!/bin/bash
2
3 grep 'href="[^"]*"' -o | cut -d '"' -f2
OPPE Practice Question 1.2.1.1
Problem Statement
Error Handling requires one to understand exit status implementation.
Write a script script.sh which will echo "Success" if there are 3 arguments passed to it and print all the three ar-
guments.
If there are more or less arguments, the exit status should be 1.
Sample Example
1 #!/bin/bash
2
3 if [[ $# -eq 3 ]]; then
4 echo Success "$@"
5 exit 0
6 else
7 exit 1
8 fi
OPPE Practice Question 1.2.1.2
Problem Statement
Sanjay is a professional photographer who capture photos of items for various clients. The photos database has
thousands of photos collected each year. The photographs are stored in jpg format using YYYYMMDD_HHMMSS.jpg
naming convention. For his backup purpose he wants to organise his data by moving the photos to a new directo-
ry created using monthYear (Ex: Jan2021) format (thus 12 directory for each year).
Write a shell script script.sh which will create folders based on the image name and creates and moves it to the
respective month directory.
Hint:
Refer date --help or man date to get the format of month and year.
Sample Input
These files are present in your script's current working directory.
1 20110313_024459.jpg
2 20110207_010404.jpg
3 20110812_222616.jpg
4 20110514_113934.jpg
Sample Output
These folder structure are supposed to be created by your script. Your script should not output anything to the
screen.
1 ./Aug2011/20110812_222616.jpg
2 ./Feb2011/20110207_010404.jpg
3 ./Mar2011/20110313_024459.jpg
4 ./May2011/20110514_113934.jpg
Solution
1 #!/bin/bash
2
3 while read -r photo ; do
4 ymd=${photo%_*}
5 folder=$(date -d $ymd +%b%Y)
6 mkdir -p $folder
7 mv $photo $folder
8 done < <(ls *.jpg)
OPPE Practice Question 1.2.1.3
Problem Statement
Write a bash script script.sh which works on output of ls -li to produce an output which will have unique
list of files which has no softlinks and has only one file for multiple hardlinks present.
Note: The final output should be sorted by ascending order of inode number.
Hint: Output of the ls -li is given as standard input (&0). Do not run ls -li yourself.
Sample Input
Sample Output
1 #!/bin/bash
2
3 sort | grep -v 'lrwx' | uniq -w 10
OPPE Practice Question 1.2.1.4
Problem Statement
Write a script script.sh that will remove the outer rectangle and grow the inner rectangle to the exact size of
outer rectangle and the inner rectangle region should be replaced with '0' (zero) including the boundary line.
Note:
Sample Input
1 ********************************
2 * *
3 * xxxxxxxxxxxxxxxxxxxxxxxxxx *
4 * x x *
5 * x x *
6 * xxxxxxxxxxxxxxxxxxxxxxxxxx *
7 * *
8 ********************************
Sample Output
1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2 x x
3 x 00000000000000000000000000 x
4 x 00000000000000000000000000 x
5 x 00000000000000000000000000 x
6 x 00000000000000000000000000 x
7 x x
8 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Solution
1 #!/usr/bin/env bash
2 while read -r line; do
3 line=${line//x/0}
4
5 # n = width of inner rectangle + 1
6 # +1 is due to the new line character
7
8 n=$(echo "$line" | grep -oE '0 +0' | wc -m)
9 for ((i = 0; i < (n-3); i++)); do
10 line=${line/0 /00}
11 done
12 line=${line//\*/x}
13 echo "$line"
14 done
OPPE Practice Question 1.2.2.1
Problem Statement
A text input usually contains puctuations, lowercase letters, and uppercase letters.
remove punctuations
Finally use concepts of sort and uniq commands to print the most frequent words ranked 5 to 10 along with its
count.
Note:
Sample Input
1 A B C D A E F G H H H A A B I J K L A X K Y Y Y Y Y Y Y G G G A Y A I I I
2 L L X X I G
Sample Output
1 l
2 h
3 k
4 b
5 j
Hint
Refer to tr, sort, uniq commands for help.
Solution
1 #!/bin/bash
2
3 tr -d '[:punct:]' | tr 'A-Z' 'a-z' | tr ' ' '\n' | sort | uniq -c | sort -nr | head
-10 | tail -5 | tr -s ' ' | cut -d ' ' -f3
OPPE Practice Question 1.2.2.2
Problem Statement
Write a script script.sh which will extract the values of color and name of color from the standard input.
A color is a string which is in the format of "name of color": [r, g, b, a] where r, g, b, a are integers.
Sample Input
Sample Output
1 "aliceblue":[240,248,255,1]
Solution
1 #!/bin/bash
2
3 #while read -r line; do
4 # echo $line | tr '<' '\n' | grep -v '^/' | cut -d'>' -f2 | grep . | tr -d '\n' |
grep -E '"[^"]*":\[[0-9]+,[0-9]+,[0-9]+,[0-9]+\]' -o
5 #done
6
7 #grep -o '"[a-z]*"\|:\|[\|[0-9]*\|,\|]' | tr -d '\n' | sed 's/],/]\n/g'
8 grep -oE '"[a-z]+"|:|\[|[0-9]+|,|\]' | tr -d '\n' | grep -oE '"[a-z]+":\[[0-9]+,[0-
9]+,[0-9]+,[0-9]+\]'
9