Search This Blog

Saturday, September 30, 2023

Shell script 7

Question:

Read the csv file and print the respective filed column by using the input filed separator.

Answer:

#!/usr/bin/bash

path="/home/kalam/names_and_employ_id.csv"

while IFS="," read name id

do

        echo "Employee name is: $name"

        echo "Employee id is: $id"

done < $path

output:

kalam@DESKTOP-OSJTLNE:~$ sh field_reading.sh

Employee name is: Name

Employee id is: id

Employee name is: raju

Employee id is: 1010

Employee name is: Naga

Employee id is: 1011

Employee name is: Naresh

Employee id is: 101


gvim names_and_employ_id.csv

Name,id

raju,1010

Naga,1011

Naresh,101


Note:

IFS Input Filed Separator in the *.csv filenames and employee id's are separated with , 


Shell script 6

Question:

Grep the multiple patterns in one shot and print the lines which matches from the file. Include if the no of arguments are not matching to the total no of arguments

Answer: 

 #!/usr/bin/bash

#command line arguments if the no of arguments are not matching the script will exit

path=$1

pattern1=$2

pattern2=$3

echo "total number of arguments $#"

if [ ! "$#" = 3 ]

then

        echo "Please provide the required argunments, No of arguments are not matching"

        echo "Usage of this script is sh <<filename.sh>> arg1 arg2 arg3"

        echo "Rerun the script with the required arguments\nThank you for using this script"

        exit 1

else

        echo "Controller starts executing the script"

        required_pattern=$(grep -E "$pattern|$pattern2" $path)

        echo "$required_pattern"

fi


Output:

kalam@DESKTOP-OSJTLNE:~$ sh command_lin_arguments.sh filename.sh name Telugu

total number of arguments 3

Controller starts executing the script

echo "Please Provide the user name and state name"

read name state_name

echo "User provided name is :$name and His state name is : $state_name "

if [ "$state_name" =  "andra" ]; then

        echo "$name speaks Telugu"

else

        echo "$name does not speak Telugu"

fi

Note:

https://www.blogger.com/blog/post/edit/7948084828664881049/5347819150177177366?hl=en

consider this script name i used for above example 


Shell script 5

 #!/usr/bin/bash

#command line arguments if the no of arguments are not matching the script will exit

path=$1

pattern=$2

echo "total number of arguments $#"

if [ ! "$#" = 2 ]

then

        echo "Please provide the required arguments, No of arguments are not matching"

        echo "Usage of this script is sh <<filename.sh>> arg1 arg2"

        echo "Rerun the script with the required arguments\n Thank you for using this script"

        exit 1

else

        echo "Controller starts executing the script"

        required_pattern=$(grep $pattern $path)

        echo "$required_pattern"

fi


Output:

kalam@DESKTOP-OSJTLNE:~$ sh  command_lin_arguments.sh

total number of arguments 0

Please provide the required argunments, No of arguments are not matching

Usage of this script is sh <<filename>> arg1 arg2

Rerun the script with the required arguments

Thank you for using this script


! --> Is Used to for negation of condition

$# --> It returns the total number of arguments 

$1 -> Indicates 1st argument

$2 --> Indicates 2nd argument

Shell script 4

Question:
Write a script whether the provided path type is file or dir

 #!/usr/bin/bash

#this script is for command line arguments

path=$1

if [ -f $path ]

then

        echo "the provided path type is file, you can read the content in any editor"

else

        if [ -d $path]

        then

        echo "the provided path type is not a file ,it is a Dir"

        fi

fi


Output :

the provided path type is file, you can read the content in any editor


Note;

-f  checks for file type

-d checks for directory 

Shell script 3

Question2:
Write a script whether the file is readable or not, if it is readable print the file content on the terminal after 5 sec

Answer:

Path="/home/kalam/filename.sh"

echo "User provide path is : $Path"

sleep 5

if [ -e $Path ]

then

        echo "file is present in the $Path"

        if [ -r $Path ]

        then

                echo "the file $Path is readable"

                info=$(cat $Path)

                echo $info

        else

                echo "file is not in readable format"

        fi

else

        echo "File is absent in the given path: $Path"

        echo "Please correct the path and rerun the script"

        echo "thank you for using this script"

fi


Output:

User provide path is : /home/kalam/filename.sh

file is present in the /home/kalam/filename.sh

the file /home/kalam/filename.sh is readable

echo "Please Provide the user name and state name" read name state_name echo "User provided name is :$name and His state name is : $state_name " if [ "$state_name" = "andra" ]; then echo "$name speaks Telugu" else echo "$name does not speak Telugu" fi


Note: 
-r flag is used check for readable the file or not if yes condition will execute

Shell script 2

 Shell scripting 2

Question:

Read the user inputs and check for that whether it is available or not ,If it is available print the user inputs are available else print not available , also your output should print after 10 Sec

Answer:

Path="/home/kalam/filename.sh"

echo "User provide path is : $Path"

sleep 10

if [ -e $Path ]

then

        echo "file is present in the $Path"

else

        echo "File is absent in the given path: $Path"

        echo "Please correct the path and rerun the script"

        echo "thank you for using this script"

fi

Output:

User provide path is : /home/kalam/filename.sh

file is present in the /home/kalam/filename.sh

Note:

-e is the flag which is used to check for the existence of the file or directory

Shell script 1

If condition

Example:

echo "Please provide the user name and state name"

read name state_name

echo "User provided name is :$name and His state name is : $state_name "

if [ "$state_name" =  "Andra" ]; then

        echo "$name speaks Telugu"

else

        echo "$name does not speak Telugu"

fi


Output:

Please Provide the user name and state name

Balakrishna andra

User provided name is :Balakrishna and His state name is : andra

Balakrishna speaks Telugu

Shell scripting

Read the user inputs and execute the script

Example1: 

echo  -n  "Please type your required info"

read info

echo "You're required information is: $info"

Example2:

read -p "Please type your required info" info

echo  "You're required information is: $info""


Example and Example2 both gives the same but, in the example1 there is no read -p

Example2 : read -p --> it will ask a query and assigned to var

Wednesday, September 13, 2023

Lists in TCL scripting

 LISTS keywords and It's Usage 

lindex

lindex is a keyword and it will print the index value of the list.
Example:
   set  NameList  "Naresh Prashanth Sanjeev Manoj  Rajendhar"
   puts [lindex $NameList 0] ; #it will print the 0th index of NameList i,e Naresh
   puts [lindex $NameList 1] 
   puts [lindex $NameList 2] 
         
Explanation:

List index will start from "0" 
Naresh    -----> index 0
Prashanth  ---> index 1
Sanjeev     ---> index 2
Manoj      ----> index 3
Rajendhar ---> index 4

*****************************************************************************

llength

llength is a keyword -it will give you the size of the list.

Example:           
  set  NameList  "Naresh Prashanth  Sanjeev Manoj Rajendhar"
  puts [lindex   $NameList 0] 
  puts [llength  $NameList ] ; #it will print 5 because 5 Names are there in the NameList  

****************************************************************************

lsort

lsort is a keyword -it will sort the list means ascending/Descending order of the List      

Example1:
 set  NumList  "0  9 8 1 3 4"
 puts [lsort $NumList  -ascending ] ; # it will print from small to big
 puts [lsort $NumList  -descending ] ; # it will print from big to small 

 Explanation 1:
 In this example I am taking numerical values     
 Ascending values --> small to big like 0 1 3 4 8 9
 Descending values --> big to small like 9 8 4 3 1 0

Example2:         
 set flist  "Banana Anar Avagado Watermelon Grapes Mango "
 puts [lsort $flist] ; # it will print in alphabetical order

*************************************************************************

lsearch

lsearch is a keyword to search for the element if it is matches it will return 0 otherwise it will return -1 

Example:
    set str "Nag Raju Chandu Mahi"
    lsearch $str Nag

************************************************************************


TCL Basics

 TCL  
Tool Command language

Keywords in TCL

  • puts
  • expr
  • $
  • incr
  • info exists
  • split
  • concat

    LIST keywords

    • lindex
    • llength
    • lsort
    • lreplace
    • linsert
    • lappend
    • lsearch
    • lappend

    What is the sanity checks you have done for STA?

     Sanity checks for STA: Linking Checks: We need to check., is there any missing modules or missing pins in a library. this is done by link c...