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

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