Translate page

Tuesday, January 25, 2022

Unix For VLSI Industry - Part 3b - Assignment 2 - SHELL Scripting


SHELL Scripting Debugging Assignment - 2


In the previous assignment, I have captured few of the practice question related to debugging part. We know very well how much this is important from Industry point of view. Like I said in last article - Understanding the Scripting syntax and using them in automation - these are 2 different things. Debugging the existing code OR say enhancing that as per the requirement is completely different. For that first you need to understand the different error messages and then you should know how to fix that part.
In this part also - there are list of questions which can help you to do practice.

  1. Correct the below shell script to split the given string.

#! /bin/bash

string="abc,xyz,jkl,raw"

IFS=' '

read -r arr <<< "$string"

for val in "${arr[@]}";

do

  printf "name = $val\n"

done


  1. Correct the below shell script to match the two input strings.

read a

read b

if ["$a"=!"$b"] then

    echo "Matched"

elif

    echo "Not Matching"

fi


  1. Correct the below shell script to create a shell function library.

function square(){

    v1=$10

    $n = $((v1*v1))

    echo $n

}


  1. Correct the below shell script to create a shell function library to calculate the factorial of a given number.

function factorial(){

    v1=$2

    n=1

    while [[ $v1 -gt 0 ]]; 

do

v1=$(($v1 - 1))

done

    echo $n

}


  1. Correct the below shell script to calculate the number of users.

read userInput

case [$userInput] [in]

    {1} lslogins -o USER ;

    {2} who --count | grep users ;

    {3} who -q | grep -v users ;

    {4} groups ;

    {*} echo -e "Please Enter Correct Input \n" ;

esac


  1. Correct the below shell script to find out how many terminals a user has logged-in.

read input

if [[ $input ]] and [ $input eq $input 2>\dev\null ]

then

echo "Number of terminals are : "

cat /etc/passwd | grep $input -c 

else

cat /etc/passwd>userlist

echo "Number of terminals are : "

grep -c $input userlist

fi


  1. Correct the shell script to swap two numbers.

first = 5

second = 10

$temp = first

$first = second

$second = temp


  1. Correct the shell script to find out the largest number among the arguments in the script.

max=$1

for arg in "$#"

do

    if [ "arg" gt "max" ]

    then

        $max=arg

    fi

done

echo "Largest value is: $max"


  1. Correct the below script to calculate the sum of digits of the given number.

Num=123

g=$Num

s=0

while { $Num gt 0 }

do

    k = $Num % 10

    $Num = $Num / 10

    $s = $s + $k 

done

echo  "sum of digits of $g is : $s"


  1. Correct the below script to display all words of a given file in ascending order.

read filename

if [ ! -f $filename ]

then

echo "File does not exist"

else

for i in $(cat $filename)

do

echo $i > "TEMP"

done

echo "$(sort "$TEMP")"

fi

if [ -f "$TEMP" ]

then

rm "$TEMP"

fi


This assignment is for your practice - We have inserted Errors for you, so that you can understand the error messages and fix accordingly. 
I can understand that you may be in need to solution - And Solution is that it should run error free. :) 
Again - It's for your Practice, not just to copy paste the code and use it anywhere. 
   
  Stay Tune and Happy Learning. We discuss more in next Article.:)

No comments:

Post a Comment

Must Read Article

Related Posts Plugin for WordPress, Blogger...