0% found this document useful (0 votes)
6 views

Oppe Practice

The document provides a series of practice questions and solutions for OPPE level scripting tasks, focusing on various shell script functionalities. Each question includes a problem statement, sample inputs/outputs, and a corresponding solution. The topics range from file manipulation and error handling to text processing and directory management.

Uploaded by

Nilay Dorlikar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Oppe Practice

The document provides a series of practice questions and solutions for OPPE level scripting tasks, focusing on various shell script functionalities. Each question includes a problem statement, sample inputs/outputs, and a corresponding solution. The topics range from file manipulation and error handling to text processing and directory management.

Uploaded by

Nilay Dorlikar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

OPPE Practice Solutions

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.

OPPE Practice Solutions


OPPE Practice Question 1.1.1.1
Problem Statement
Sample Example
Solution
OPPE Practice Question 1.1.1.2
Problem Statement
Solution
OPPE Practice Question 1.1.1.3
Problem Statement
Solution
OPPE Practice Question 1.1.1.4
Problem Statement
Solution
OPPE Practice Question 1.1.2.1
Problem Statement
Solution
OPPE Practice Question 1.1.2.2
Problem Statement
Solution
OPPE Practice Question 1.2.1.1
Problem Statement
Sample Example
Solution
OPPE Practice Question 1.2.1.2
Problem Statement
Sample Input
Sample Output
Solution
OPPE Practice Question 1.2.1.3
Problem Statement
Sample Input
Sample Output
Solution
OPPE Practice Question 1.2.1.4
Problem Statement
Sample Input
Sample Output
Solution
OPPE Practice Question 1.2.2.1
Problem Statement
Sample Input
Sample Output
Hint
Solution
OPPE Practice Question 1.2.2.2
Problem Statement
Sample Input
Sample Output
Solution
OPPE Practice Question 1.1.1.1

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.

Note: run.sh may also be absent, handle it accordingly.

Sample Example

1 > echo "echo hello" > run.sh


2 > chmod a-x run.sh
3 > ./script.sh
4 SUCCESS
5 > echo "echa hello" > run.sh
6 > ./script.sh
7 FAIL
Solution

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).

1 Early in the day it was whispered that we should sail in a boat,


2 only thou and I, and never a soul in the world would know of this our
3 pilgrimage to no country and to no end.
4
5 In that shoreless ocean,
6 at thy silently listening smile my songs would swell in melodies,
7 free as waves, free from all bondage of words.
8
9 Is the time not come yet?
10 Are there works still to do?
11 Lo, the evening has come down upon the shore
12 and in the fading light the seabirds come flying to their nests.
13
14 Who knows when the chains will be off,
15 and the boat, like the last glimmer of sunset,
16 vanish into the night?

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.

Assume all a tags start and end in the same line.

Example: In the given text

1 <a href="https://onlinedegree.iitm.ac.in/">IITM BS Degree Programme</a>

https://onlinedegree.iitm.ac.in/ is the link to the site.

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 > ./script hello how are you?


2 > echo $?
3 1
4 > ./script comment ca va?
5 Success comment ca va?
6 > echo $?
7 0
Solution

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.

Do not create empty folders.

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

1 12 -rwxr-xr-x 1 root root 1113504 Apr 18 2022 bash


2 13 -rwxr-xr-x 1 root root 716464 Mar 13 2018 btrfs
3 14 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-debug-tree
4 15 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-find-root
5 16 -rwxr-xr-x 1 root root 396432 Mar 13 2018 btrfs-image
6 17 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-map-logical
7 18 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-select-super
8 19 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-zero-log
9 20 lrwxrwxrwx 1 root root 5 Mar 13 2018 btrfsck -> btrfs
10 21 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfstune
11 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bunzip2
12 23 -rwxr-xr-x 1 root root 2062296 Nov 25 2021 busybox
13 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bzcat
14 24 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzcmp -> bzdiff
15 25 -rwxr-xr-x 1 root root 2140 Jul 4 2019 bzdiff
16 26 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzegrep -> bzgrep
17 27 -rwxr-xr-x 1 root root 4877 Jul 4 2019 bzexe
18 28 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzfgrep -> bzgrep
19 29 -rwxr-xr-x 1 root root 3642 Jul 4 2019 bzgrep

Sample Output

1 12 -rwxr-xr-x 1 root root 1113504 Apr 18 2022 bash


2 13 -rwxr-xr-x 1 root root 716464 Mar 13 2018 btrfs
3 14 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-debug-tree
4 15 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-find-root
5 16 -rwxr-xr-x 1 root root 396432 Mar 13 2018 btrfs-image
6 17 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-map-logical
7 18 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-select-super
8 19 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-zero-log
9 21 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfstune
10 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bunzip2
11 23 -rwxr-xr-x 1 root root 2062296 Nov 25 2021 busybox
12 25 -rwxr-xr-x 1 root root 2140 Jul 4 2019 bzdiff
13 27 -rwxr-xr-x 1 root root 4877 Jul 4 2019 bzexe
14 29 -rwxr-xr-x 1 root root 3642 Jul 4 2019 bzgrep
Solution

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:

The outer rectangle is always made up of asterisks '*'

The inner rectangle is always made up of lowercase X 'x'

The input should be taken from the standard input

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.

Write a script script.sh which will

remove punctuations

convert uppercase letters to lowercase and

convert the space character to newline character in that order.

Finally use concepts of sort and uniq commands to print the most frequent words ranked 5 to 10 along with its
count.

Note:

The input should be obtained from the standard input (&0).

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

1 <span class="token string">"aliceblue"</span><span class="token operator">:</span>


<span class="token punctuation">[</span><span class="token number">240</span><span
class="token punctuation">,</span> <span class="token number">248</span><span
class="token punctuation">,</span> <span class="token number">255</span><span
class="token punctuation">,</span> <span class="token number">1</span><span
class="token punctuation">]</span><span class="token punctuation">,</span>

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

You might also like