Bash Shell Scripting

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Shell Script

Table of Contents
1) Introduction.............................................................................................................................................2
2) Shell Commands......................................................................................................................................2
3) Exercises..................................................................................................................................................4
a) Search & Count....................................................................................................................................4
b) Search and replace..............................................................................................................................4
c) Comparison..........................................................................................................................................4
d) Log file parsing.....................................................................................................................................4
e) Argument parsing................................................................................................................................6
f) File parsing...........................................................................................................................................6
4) Regression Automation...........................................................................................................................7
g) Setup....................................................................................................................................................7
h) Running...............................................................................................................................................7
i) Reporting..............................................................................................................................................7
j) Directory...............................................................................................................................................7
k) Test list.................................................................................................................................................7
l) Logs.......................................................................................................................................................7
m) Report.................................................................................................................................................8

1|Page P J Nandagopal
Shell Script

a) Introduction
This document talks about shell scripting using bash shell. Scripting is necessary for everyone:
(1) To automate hectic manual work
(2) Reduce the time taken to complete the task
(3) Avoid manual errors (oversee, miscalculation)
(4) Off office hours
(5) Avoid particular person presence

Bash shell is linux/unix shell which has set of commands for simplifying manual operation.

b) Bash shell commands


No Command Purpose Description
1 grep Find a pattern in a file grep “pattern” file;
cat file | grep “pattern”
cat README | grep “Section” | grep “How To
Run”
2 find Find a file find . –name ‘*.txt’
find /home/nanda/project “abc.env”
3 sed Replace pattern with sed “s/abcd/1234/g” file.txt > tmp.txt
new pattern mv tmp.txt file.txt
sed “s/abcd/1234/1” file.txt (single instance)
sed “s/^abc/123/g” file.txt (SOL)
sed “s/abc$/123/g” file.txt (EOL)
sed “s/ab[.]/123/g” file.txt
sed “s/ab[.*]/123/g” file.txt
sed “s/abc/123/gc” file.txt (user permission for
each replacement)
4 expr Arithmetic operation var=10
for count, add, var=`expr ‘$var + 1’` (Increment)
subtract, multiply var=`expr ‘$var \* 2` (Multiply by 2)
division var=`expr “$var \/ 2”`
var=`expr “$var % 2”`
5 echo Print variables & text name=”nanda”
echo “Hai how are you?”
echo “Hai $nameee how are you?”
6 awk Used for extracting File input.txt contains “A B C D”
fields cat input.txt |awk ‘{print $2 “ and ” $4}’
Output will be “B and D”
7 for Fixed condition loop

Input.txt contains names of verif engr


for entry in `cat input.txt`
do

2|Page P J Nandagopal
Shell Script

echo “$entry”
done

// Verilog For iii=0; iii<3; iii++


for iii in 0 1 2
do
echo “$iii”
done

// Raghu: Another way - to be tried


for iii in 1 to 10
echo “$iii”
done
8 while Dynamic condition count=1
loop // -lt, -le, -ge, -gt, -eq
while [ “$count” –le 10 ]
do
count=`expr $count + 1`
done

while [ “$tstname” != “perf” ]


9 >, >>, |& tee Write and append to echo “Nanda” > file.txt
file echo “Gopal” >> file.txt
\rm –f file.txt
echo “Nanda” |& tee file.txt
10 abcd=1 Variable declaration Type of variable is dynamically inferred
abcd=”hai” depending on value assigned.
11 function func_1 ()
{
echo “I am in func_1”
}

abcd=1
func_1;

// Raghu: Whether params can be passed?


12 $1, $2, … Command line
arguments parsing

if [ “$1” ]
then
if [ “$1 = “Yes” ]
func_1;

3|Page P J Nandagopal
Shell Script

elif [ “$1” = “No” ]


then
echo “Function not trigerred”
fi
else
echo “No argument passed”
fi

sh myscript.sh
No argument passed

sh myscript.sh No
Function not trigerred

sh myscript.sh who
Prints nothing

13 wc -c Word count ls | wc -c will print number of files in the


wc -l directory
14 pushd . Going inside and cd /home/nanda/ccore
popd coming out of rm –f $HOME/log.txt
directory for file in `cat list.dir`
do
echo “- $dir -“ >> $HOME/log.txt
pushd .
cd $dir
grep “L2CacheLoad” * >> $HOME/log.txt
popd
Done

Pushd .
Cd dir1; (cd tests/i2c)
Pushd .
Ls |wc -l
Cd dir2; (cd wr_rd_tsts)
Grep |awk |sed
Popd
Popd

4|Page P J Nandagopal
Shell Script

c) Exercises
a) Search & Count
Count how many oranges in the context.
Open an input file “input.txt” and type the following as shown below:
A man went to shop and bought 6 oranges. He went to another shop and bought 8 oranges. He went
to one more shop and bought 6 apples.

b) Search and replace


File “list.dir” Contents (ls-dR will give the list of all directory and sub directory)
/home/nanda/ccore/tb
/home/nanda/ccore/tb/vip/i2c
/home/nanda/ccore/tb/vip/i2c/sv_lib/tests

You are in home directory: /home/nanda


There are similar directories like i2c, i3c, spi, i2s etc are available

For all directories, run your script to replace


tests --> testcases
sv_lib --> uvm_tests
count & print --> no of tests
c) Comparison
Find out which column is bigger between Column A & C (B column should be skipped) and create
“output.txt file” with column A, C, CompareResult as shown below:
input.txt file entry:
10 9 20
20 25 15
125 256 1000
1019 900 956
121 121 121
Output file (script to generate output file)
A C Result
==================
10 20 C
20 15 A
125 1000 C
1019 956 A
121 121 Equal

d) Log file parsing


Count number of errors and warnings and give summary table
Segregate different kinds of errors
Log file
Simulation started

5|Page P J Nandagopal
Shell Script

Error: File not found


ERROR: Reset not set
Error: clock path
warning: reset is active high
Error: clock path
Error: Reset not set
Error: clock path

6|Page P J Nandagopal
Shell Script

e) Argument parsing
Pass four arguments and add even arguments and subtract odd arguments and print the results
(i.e. arg2 + arg4 and arg1-arg3). Report error arguments are more or less than four.

f) File parsing
Read line by line and insert pattern “#start#” at start of each line and pattern “#end#” at end of line

7|Page P J Nandagopal
Shell Script

d) Regression Automation
Here let us see how to automate regression and generate reports.
g) Setup
Creating new directory & fresh checkout.
h) Running
Creating list of tests & combinations.
i) Reporting
Looking at all logs and generating summary.
Parsing logs and consolidating errors.
Comparing with previous regression report.

j) Directory
Base Directory ($base_dir): /home/nandag/project/kss/cpuss/verif
Run Directory ($sim_dir): $base_dir/sim
Tests Directory ($tests_dir): $base_dir/tests
Log Directory ($log_dir): $base_dir/sim/logs
Script Directory ($script_dir): $base_dir/scr

k) Test list
Make sim TEST=”test_basic_1”
Make sim TEST=”test_basic_2” WIDTH=32
Make sim TEST=”test_basic_2” WIDTH=64
Make sim TEST=”test_basic_3”

Make sim TEST=”test_feature_1” MODE=0 LAUNCH=POSITIVE


Make sim TEST=”test_feature_1” MODE=0 LAUNCH=NEGATIVE
Make sim TEST=”test_feature_1” MODE=1 LAUNCH=POSITIVE
Make sim TEST=”test_feature_1” MODE=1 LAUNCH=NEGATIVE
Make sim TEST=”test_feature_2” MODE=0
Make sim TEST=”test_feature_2” MODE=1
Make sim TEST=”test_feature_3” MODE=0 LAUNCH=POSITIVE
Make sim TEST=”test_feature_3” MODE=0 LAUNCH=NEGATIVE
Make sim TEST=”test_feature_3” MODE=1 LAUNCH=POSITIVE
Make sim TEST=”test_feature_3” MODE=1 LAUNCH=NEGATIVE

l) Logs
Inside $log_dir/testname_seed.log
End of test: “TEST PASSED” or “TEST FAILED” message will be there
Error: compile failed
Error: Elaboration failed
Error: data mismatch
Error: timing violation
Error: ID mismatch

8|Page P J Nandagopal
Shell Script

m) Report
Summary
Total Tests: 14 (Prev: 14)
Pass: 10 (Prev: 8)
Fail: 4 (Prev: 6)

Error wise
Error-1: 2 tests
Error-4: 2 tests

9|Page P J Nandagopal