Search This Blog

Friday, October 6, 2023

TCL Script 10

Question:
Write a proc to get the collaterals from  different dir

#!/usr/bin/tclsh

proc getPtpxInputs {hm_name run_tag} {

        set comman_path "/home/kalam/pnr_inputs/"

        set run_area    "$comman_path/$hm_name/$run_tag"

        set ptpx_inputs [list ${hm_name}.v ${hm_name}.map ${hm_name}.sdc]

        set upf_path    [list ${hm_name}.upf]

        foreach input $ptpx_inputs {

               set file_path [file join $run_area $input]

               set upf_path  [file join $comman_path/power_intent $upf_path]

               if {[file exists $file_path] && [file exists $upf_path]} {

                        puts "$file_path"

             }

       }

       puts "$upf_path"

   }

  set hm_name top

  set run_tag run1

getPtpxInputs $hm_name $run_tag

Usage of the script <proc_name> <hm_name> <run_tag>

output:

kalam@DESKTOP-OSJTLNE:~/pnr_inputs$ tclsh  getptpx_inputs.tcl

/home/kalam/pnr_inputs/top/run1/top.v

/home/kalam/pnr_inputs/top/run1/top.map

/home/kalam/pnr_inputs/top/run1/top.sdc

/home/kalam/pnr_inputs/power_intent/top.upf

Monday, October 2, 2023

Shell script 20

Question: Get the files from different directories by using case  statement

Answer:

#!/usr/bin/bash

if [ $# -eq 0 ]

then

        echo "Usage: $0 <case_number>"

        exit 1

fi

case_number="$1"

case "$case_number" in

        1)

                dir="/home/kalam/pnr_inputs/"

                files=("top.spef" "top.vi") ;;

        2)

                dir="/home/kalam/synth_inputs/"

                files="leaf_inst.rpt" ;;

        *)

                echo "Invalid case_number, Please choose 1 or 2"

                exit 1 ;;

esac

if [ ! -d "$dir" ]

then

        echo "Directory $dir does not exist"

        exit 1

else

        for file in "${files[@]}"

        do

        file_path="${dir}${file}"

        if [ -f "$file_path" ]

        then

                echo "$file_path"

        else

                        echo "File $file_path not found"

        fi

done

fi


Shell script 19

Question: write a script to print your lucky number

Answer:

#!/usr/bin/bash

read -p "What is your lucky number?" num

case  $num in

        1)

                echo "your lucky number is one";;

        2)

                echo "Your lucky number is two";;

        *)

                echo "None"

esac

Output:

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

What is your lucky number? 1

your lucky number is one

Shell script 18

Question: Usage of  $*,$1,$#,$0,$@,$?

 #!/usr/bin/bash

present_working_dir=`pwd`

echo "script name $present_working_dir/$0"

echo "1st argument $1"

echo "2nd argument $2"

echo "Previous command is success or not if yes it will give 0: $?"

echo "$*"

echo "total number of arguments $#"

echo "Arguments names $@"


Shell script 17

 CUT command usage

Question:

Usage of the cut command 

cut -d "," -f 1 cut_examples.sh

cut -d "," -f 2 cut_examples.sh

cut -d "," -f 3 cut_examples.sh

cut -c 1-4 cut_examples.sh

Sunday, October 1, 2023

Shell script 16

Question: Read the file content by read command

#!/usr/bin/bash 

while read msg

do

    echo "$msg"

done  <  "/home/kalam/filename.sh"

Explanation:

<  flag is used to open the file
read command is used to read the content

Shell script 15

Question: Variable assignment 

#!/usr/bin/bash

variable="Raju"

echo "$variable"

Question: Command substitution 

#!/usr/bin/bash

variable=`date`

echo $variable

FORMAT=$(date +%Y-%m-%d,%H-%M-%S)

echo "date: $FORMAT"




Shell script 14

Question: Backup the important files and directory's with the timestamp

Answer:

#!/usr/bin/bash

#source directory to back

source_dir="/home/kalam/raju"

#backup directory

backup_dir="/home/kalam/Professor"

#creating the timestamp for the backup

timestamp=$(date +"%Y%m%d%H%M%s")

backup_sub_dir="$backup_dir/backup_$timestamp"

mkdir -p "$backup_sub_dir"

rsync -av "$source_dir/" "$backup_sub_dir/"


Output:

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

sending incremental file list

./

sent 58 bytes  received 19 bytes  154.00 bytes/sec

total size is 0  speedup is 0.00

kalam@DESKTOP-OSJTLNE:~$ ls Professor/

backup_2023100120341696172680  backup_2023100120341696172691  backup_2023100120351696172717  backup_2023100120381696172901

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