Saturday, January 22, 2022

Unix For VLSI Industry - Part 3a - Assignment 1 - SHELL Scripting


SHELL Scripting Debugging Assignment - 1

Understanding the Scripting syntax and using them in automation - these are 2 different things. A lot of times people use google to check the syntax and they have some logic to capture them but when they compile - they face a lot of error. And that's the point they stuck because debugging itself is a Skill and which always come by experience or say practice. In next couple of assignments, you will get few practice questions where you have to debug and find out the error and fix the script.

  1. Find the errors in the below shell script for calculating the factorial of a given number:

factorial() {

product=$1

if((product <= 2)); then

echo $product

else

f=$((product -1))

f = $(factorial $f)

f = $((f*product))

echo $f

fi

}

echo "Enter the number:"   

read num

if(num == 0); then   

echo 1

else

factorial $num

fi


  1. Correct the below shell script for a counter using a while loop.

#!/bin/bash

valid=true

count=1

while [ $valid ]

do

echo $count

if [ $count -eq 5 ];

then

break

fi

(count++)

done


  1. Find out the errors in the below shell script for a down counter.

#!/bin/bash

for (( counter=10; counter>0; counter-- ))

do

echo -n "$counter "

printf "\n"


  1. Correct the errors in the below shell script.

#!/bin/bash

echo "Enter your lucky number"

read n

case $n in

1)

echo "Gold Winner" ;

2)

echo "Silver Winner" ;

3)

echo "Bronze Winner" ;

*)

echo "Better Luck Next Time" ;

esac


  1. Correct the below shell script to return the string for the function.

#!/bin/bash

function func_name() {

str="VSLI Expert, $input"

echo $str

}

echo "Enter your name"

read input

val=$((func_name))

echo "Return value of the function is $val"


  1. Change the below script to print Fibonacci series in vertical order.

read N

x=0

y=1

echo "The Fibonacci series is : "

for (( i=0; i<N; i++ ))

do

    echo -n "$x "

    sum=$((x + y))

    x=$y

    y=$sum

done


  1. Correct the below script to find out the prime number.

read num

i=2  

f=0

while test $i -le ((expr $num / 2)) 

do

if test "expr $num % $i" -eq 0 

then

f=1

fi

i=`expr $i + 1`

done

if test $f -eq 1 

then

echo "Given number is not Prime number"

else

echo "Given number is Prime number"

fi


  1. Correct the below shell script for sorting the list.

arr=(100 40 220 1100 102)

echo ${arr[*]}

for ((i = 0; i<5; i++))

do

    for((j = 0; j<5-i-1; j++))

    do

        if [ ${arr[$j]} -gt ${arr[$((j+1))]} ]

        then

            temp=${arr[j]}

            arr[j]=${arr[$((j+1))]}  

            arr[$((j+1))]=temp

        fi

    done

done

echo ${arr[*]}


  1. Correct the below shell script to traverse a given list of numbers.

arr=(10 12 63 34 58)

for i in "${arr[$?]}"

do

    echo $i

done


  1. Correct the below script to generate the password with the given length by user.

read  PASS_LENGTH

for p in $(seq 1 5);                                   

do 

    openssl rand -base64 48 ; cut -c1-$PASS_LENGTH 

done


 This assignment is for your practice - you can practice on these scripting questions and later it will help you a lot in the Industry.
   
  Stay Tune and Happy Learning. We discuss more in next Assignment.:)

No comments:

Post a Comment