📙
Workbook - Introduction to Software Systems
  • About - Workbook
  • Introduction
  • Software Engineering
    • Software Systems
    • Software Product Development
    • Networking
  • TERMINAL PROGRAMMING
    • LINUX - SHELL
    • Practice Questions
  • Web Technologies
    • HTML
    • CSS
    • JavaScript
  • Databases
    • Database Systems
    • SQL - CURD Operations
  • Object Oriented Programming
    • Python
  • Lab Activities
    • Understanding GIT - Lab 1
    • SHELL - Lab 2
      • Class Quiz (15/6/2021)
      • Class Test Script
      • SHELL Lab Activity Task
      • SHELL Lab Activity Solution
  • ASSIGNMENTS
    • Assignment-1 SHELL Solutions
Powered by GitBook
On this page

Was this helpful?

  1. ASSIGNMENTS

Assignment-1 SHELL Solutions

Question-1:

cat "$1" | sed  "s/./#/5g" > output.txt 

Question-2:

#!/bin/bash

file_path=$1 # taking the path of input file
value=`cat $1` # reading the file

IFS=' ' # setting delimiter as <space>
read -ra arr<<<$value #splitting the input text by the delimiter and storing the splits in array named arr
n=${#arr[@]} # n = total no. of elements in the array
dob=${arr[$n-1]} # date of birth is the last element of the array

IFS='/'
read -ra arr2<<<$dob
dob_day=${arr2[0]}				# Getting day,month,year for dob date
dob_month=${arr2[1]}      
dob_year=${arr2[2]}

cur_date=$(date '+%Y-%m-%d')
IFS='-'
read -ra arr3<<<$cur_date
cur_day=${arr3[2]}				# Getting day,month,year for current date
cur_month=${arr3[1]}
cur_year=${arr3[0]}


age=$(( cur_year - dob_year ))

if [[ ${cur_month#0} -lt ${dob_month#0} ]]; then	
	age=$(( $age - 1 ))
elif [[ ${cur_month#0} -eq ${dob_month#0} ]]; then				# Basic logic for age calculation
	if [[ ${cur_day#0} -lt ${dob_day#0} ]]; then
		age=$(( $age - 1 ))
	fi
fi

unset arr[-1]
arr+=($age)
function join { local IFS=" "; shift; echo "$*"; }				# Output Generation
result=$(join , ${arr[@]})

echo $result
echo $result > output.txt

Question-3:

#!/bin/bash 

echo "1. Words - start with 's' and is not follow by 'a'" 

cat "$1" | grep -o "\bs[^a]\w*" 

echo "2. Word starts with 'w' and is followed by 'h'" 

cat "$1" | grep -o "\bwh\w*" 

echo "3. Word starts with 't' and is followed by 'h'" 

cat "$1" | grep -o "\bth\w*" 

echo "4. Word starts with 'a' and is not followed by 'n'" 

cat "$1" | grep -o "\ba[^n]\w*" 

Question-4:

Question-5:

#!/bin/bash

echo "Directories:"
find $1 -mindepth 1 -type d -printf '%f, ' -execdir sh -c 'echo "$(find "{}" -mindepth 1 -type f | wc -l)" file\(s\)' \; | sort -nr -k 2

echo "Files:"
find $1 -mindepth 1 -type f -printf '%s %f\n' | sort -nr -k 1 | awk '{print $2}';

Question-6:

#!/bin/bash

#taking flag input 
# this way, we take input irrespective of the order of flags
while getopts ":C:k:f:l:n:o:v:adc:" fl;
do
    case "${fl}" in
        f) fname=${OPTARG};;
        l) lname=${OPTARG};;
        n) mobile=${OPTARG};;
        C) op=${OPTARG};;
        c) column=${OPTARG};;
        v) search=${OPTARG};;
        d) descending=1;;
        a) ascending=1;;
        o) office=${OPTARG};;
        k) ename=${OPTARG};;
    esac
done

# initiating the contacts.csv file
touch contacts.csv
lines=$(cat contacts.csv | wc -l)
if [ $lines == 0 ]
then
    echo "fname,lname,mobile,Office" > contacts.csv
fi

#executing the desired command

case "${op}" in
    insert)
        echo -n "" >> contacts.csv
        echo "${fname},${lname},${mobile},${office}" >> contacts.csv;;

    #executing search by iteratively reading the file and matching the value of the specified column 
    search)
        if [[ "$column" == "fname" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$a" == "$search" ]]
                then
                    echo $a,$b,$c,$d
                fi
            done < contacts.csv
        elif [[ "$column" == "lname" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$b" == "$search" ]]
                then
                    echo $a,$b,$c,$d
                fi
            done < contacts.csv
        elif [[ "$column" == "mobile" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$c" == "$search" ]]
                then
                    echo $a,$b,$c,$d
                fi
            done < contacts.csv
        else
            while IFS="," read -r a b c d
            do
                if [[ "$d" == "$search" ]]
                then
                    echo $a,$b,$c,$d
                fi
            done < contacts.csv
        fi
        ;;

    #executing delete by iteratively reading the file and matching the value of the specified column 
    #if the value matches, we skip it 
    delete)
        touch temp.csv
        if [[ "$column" == "fname" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$a" != "$search" ]]
                then
                    echo "${a},${b},${c},${d}" >> temp.csv
                fi
            done < contacts.csv
        elif [[ "$column" == "lname" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$b" != "$search" ]]
                then
                    echo "${a},${b},${c},${d}" >> temp.csv
                fi
            done < contacts.csv
        elif [[ "$column" == "mobile" ]]
        then
            while IFS="," read -r a b c d
            do
                if [[ "$c" != "$search" ]]
                then
                    echo "${a},${b},${c},${d}" >> temp.csv
                fi
            done < contacts.csv
        else
            while IFS="," read -r a b c d
            do
                if [[ "$d" != "$search" ]]
                then
                    echo "${a},${b},${c},${d}" >> temp.csv
                fi
            done < contacts.csv
        fi
        cat temp.csv > contacts.csv
        rm temp.csv
        ;;

    #executing display, sorting based on first name by using the sort command with k flag. 
    #t flag is used to define the separato as coma
    display)
        touch temp.csv
        cat contacts.csv > temp.csv
        if [[ "$ascending" == 1 ]]
        then
            tail -n+2 temp.csv | sort -k1 -t,
        elif [[ "$descending" == 1 ]]
        then
            tail -n+2 temp.csv | sort -k1 -r -t,
        else
            tail -n+2 temp.csv
        fi
        rm temp.csv
        ;;

    #executing edit by iterating over the rows
    # if the value matches the specified value, we replace the row with the input, else echo the row back
    edit)
        touch temp.csv
        while IFS="," read -r a b c d
        do
            if [[ "$a" == "$ename" ]]
            then
                echo "${fname},${lname},${mobile},${office}" >> temp.csv
            else
                echo "${a},${b},${c},${d}" >> temp.csv
            fi
        done < contacts.csv
        cat temp.csv > contacts.csv
        rm temp.csv
        ;;
esac 

PreviousSHELL Lab Activity Solution

Last updated 3 years ago

Was this helpful?