Search This Blog

Sunday, October 1, 2023

Shell script 13

Question:

Write a script to replace a pattern with other pattern without opening the file.

Answer:

#!/usr/bin/bash

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

if [ -e $inputfile ]

then

        echo "File is exists in the following path: $inputfile"

        #pattern to replaced

        pattern_to_replace="state_name"

        #replacement pattern

        replacement_text="STATE_NAME"

        sed -i "s/$pattern_to_replace/$replacement_text/g" "$input file"

        echo "pattern '$pattern_to_replace' replaced with '$replacement_text' in '$inputfile'"

else

        echo "file not found: $inputfile"

        echo "Please provide realpath of the file and re-run the script\n Thank you for using this script"

fi

output:

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





Shell script 12

Question:

Open a file and read the content and print the lines one by one, also check the file is exist or not if it is absent say file is absent  

Answer:

#!/usr/bin/bash

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

if [ -e $path ]

then

        echo "File is exists in the following path: $path"

        for line in $(cat "$path")

        do

                echo "line: $line"

        done

else

        echo "file is absent"

        echo "Please provide realpath of the file and re-run the script"

        echo "Thank you for using this script"

        exit 1

fi



Basic Linux commands

How to create a file

    touch filename

    Explanation: touch used to create a file 

How to create multiple files at a time

    touch Math's Physics Chemistry Social

    touch file {1..1000}

    Explanation : We can create multiple files at a time 

    It will create 1000 files at a time in the current directory

Command to display the contents of the file

    cat filename

    cat -n filename

    cat -b filename

    Explanation: It will display from beginning of the line to end of the line forward direction 

    -n flag is will give you the line number 

    -b flag will give you line numbers only for non-blank lines

Command to display form end of the file to beginning of the file 

    tac filename 

    Explanation : It will display from end of the file to beginning of the file 

Redirecting the output of the command to a file

    cat file > output.text

    Explanation: file content is redirected to the output.text

Command to appending the content 

    cat filename >> output.text 

    Explanation: It will to the end of the line

Shell script 11

Question:

How to check the file is exist or not and if it exists then the size is zero or not

file="/home/kalam/if_condition.sh"

if [ -e $file ]

then

echo “File exists”

else

echo “File does not exist”

fi

## Checking for file size is Zero or not

 if [ -s $file ]

then

echo “File size is zero”

else

echo “File size is not zero”

fi



Shell script 10

Question:

How to check whether the file has read, write and execute permissions 

Answer:

 #!/usr/bin/bash

file=“/home/kalam/file.text”

## checking for write permissions 

if [ -r $file ]

then

echo “File has read access”

else

echo “File does not have read access”

fi

## checking for write permissions 

if [ -w $file ]

then

echo “File has write permission”

else

echo “File does not have write permission”

fi

## checking for execute permission 

if [ -x $file ]

then

echo “File has execute permissions”

else

echo “File does not have execute permission”

fi



Shell script 9

Question:

String length is empty or not

Answer:

#!/usr/bin/bash

mystring="I love my country"

if [ -z "${mystring}" ]

then

        echo "mystring length is not zero"

else

        echo "$mystring"

fi

Output:

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

I love my country

Shell script 8

Question:

How to convert from one case to other case

#!/usr/bin/bash

## this script is used for converting from lower case letters to upper case letter

user_name=Raju

user_fname=LAWRENCE

echo "$user_name" | tr '[:lower:]' '[:upper:]'

echo "$user_fname" | tr '[:upper:]' '[:lower:]'


Output:

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

RAJU

lawrence                        

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