Friday, January 28, 2022

Unix For VLSI Industry - Part 4a- TASK 1 - SHELL Scripting


SHELL Scripting Task - 1


In the previous articles or say Assignment 1 and Assignment 2, I have captured few of the practice question related to debugging of SHELL Scripting. We know very well how much this is important from Industry point of view. By now you are very much expert in the UNIX Command and specially writing any Kind of Script using the SHELL Scripting. I am now going to give you few small-small TASK which you can do yourself and these can be used directly in Industry with small modification as per the requirement.

This is the based on script or say automation, we normally use during regression-runs. If you don't know what's regressions - basically engineers run a set of code/tool on large set of small designs (known as Test cases) and analysis the result with respect to each test cases. Few test case are designed to be failed and few suppose to a pass (means should give expected result). If after running the code - result is different from the expected one - We consider regression testcase fail. :)
As you can see that it's looks like Interesting first time but it's huge in numbers and that's the reason there is a lot of automation happen around this using different type of scripting language. Today, we will discuss about the same but in SHELL scripting.

Task 1a -: There is a directory named “regression” inside which there are more than a thousand sub-directories. Each sub-directory has two more sub-directories which are “golden_dir” and “current_dir”. Inside these sub-directories, a file with different versions is present. 
  • Write a script to diff files present in “golden_dir” and “current_dir”. If diff is valid then update the “golden_dir” sub-directory with the new version of the file.

Task 1b -: 
  • Step 1: Write a script named “parent.sh” which run another script with “*.sh” extension only. 
  • If the user has a directory including 100 script files with different extensions in it. Write a script “extension_converter.sh” which can convert any extension of file to “*.sh” extension and call this script inside “parent.sh” script.

Task 1c :- 
  • There is a directory containing more than 100 script files written in python, perl, and TCL syntax. 
  • Each python script has first line i.e., /usr/bin/python
  • Each Perl script has first line i.e., /usr/bin/perl
  • Each tcl script has first line i.e., /usr/bin/tclsh
Write a script to dump python scripts to “python” directory, perl scripts to “perl” directory, and TCL scripts to “TCL” directory.


Task 1d :A directory contains thousands of files created in 2020 and 2021. 
  • Write a script to create a log file which contains the list of all files created in June 2020 along with the modified time.


Try these task and I am sure if you have prepared them today - Tomorrow you can use these in Industry with slight modification.
Stay Tune and Happy Learning.

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

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

Thursday, January 20, 2022

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


SHELL Scripting based Assignment

Shell Scripting is going to easy your daily work - specially repeated work. In the previous Article we have discussed the UNIX command and first assignment - Now it's time to use the scripting language to automate our work around the Unix command. There are other scripting languages also like TCL / PERL and Python but in this article we will talk about the Assignments or say Practice questions related to SHELL Scripting. You can use Google for now to learn the scripting part and then test your understanding using below questions.

Operation Over File or Directory

  1. Write a shell script to call another script in it which should call another script inside it.
  2. Write a shell script to call another script if it exists. If not, print “file not found”.
  3. Write a shell script to call another script if you have permission to call it.
  4. Write a shell script in bash to traverse a list in bash.
  5. Write a shell script to check if a given file is a regular file or a directory if it exists.
  6. Modify the script written in Q-5 to get file name as argument to the script instead of user typing it at run time.
  7. Modify the script written in Q-6 to get the unlimited number of arguments as file name.
  8. Modify the script written in Q-5, if it is a directory, exit with a 1-exit status. If it is some other type of file, exit with a 2-exit status.
  9. Write a shell script to check if a given file is a directory and return 0 exit status and return non-zero exit code if it is not a directory.
  10. Write a shell script to count the number of directories in the given path.
  11. Write a shell script to count the number of files in the given path.
  12. Write a shell script to draw a tree-like structure describing the hierarchy of directories and files for a given path.
  13. Write a shell script to print the name of files colon separated along with their type and modified dates.
  14. Consider the multiple files in your PWD. Write a shell script to rename all the files with the extension *.sh and ask the user to add a specific prefix in the name of the file , else add “vlsi_expert_”. Remember to have unique names for all files.
  15. Write a shell script to change the extension of all files from *.sh to *.txt. If no file with the extension *.sh found on a given path, then exit script with non-zero exit status.

Execute Unix Command using SHELL Scripting

  1. Write a shell script to execute the command "catabc". Report “command succeeded” if the command returns a 0 exit and exit your script with a 0 exit. Report “command failed” if the command returns a 1 exit and exit your script with a 1 exit.
  2. Write a shell script to count the number of lines of a text file.
  3. Write a shell script to collect all files containing “VLSI Expert” in it.
  4. Write a shell script to get all the lines of a file containing “pass” in it.
  5. Let us suppose, you have created an application or a software and for some specific reason you do not want to run that application. Then, create a script to give proper explanation for not executing the application.
  6. Write a shell script to see current date, time, username, and current directory.
  7. When the terminal is opened, by default the present working directory is your home directory. Make some changes so that the default pwd should be ~/work directory for all the terminals that are opened.
  8. Whenever you open a terminal, it should display a message like “Welcome "{your-name}" to the world of Linux Scripting!”.

Printing and getting data from user using Command line options

  1. Write a shell script to print even numbers between 1 and 100.
    • Modify script written in Q-21 to display the output in the following manner:
      • 3 9 12 15 18 … 10 numbers on one line and then next 10 numbers on the next line and so on.
    • Now, Modify script written to ask the user for the range instead of the range 1 to 100 using read command. Put a check that the min range is less than the max range.
    • Now, Further Modify script to allow the user to specify the range as arguments to your script file at run time. Your script should then print the even numbers falling in this range [min:max].
    • Modify script written in Q-24 to check that only two arguments are mandatory otherwise print an appropriate help message as printed by any other Linux command.
  2. Write a shell script to find out the biggest number from the given three numbers. The numbers are supplied as command line parameters. Print error if sufficient arguments are not supplied.

Print Patters on the terminal

  1. Write a shell script to print below patterns:

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