Bash Shell Scripting
Bash Shell Scripting
Bash Shell Scripting
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.
2|Page P J Nandagopal
Shell Script
echo “$entry”
done
abcd=1
func_1;
if [ “$1” ]
then
if [ “$1 = “Yes” ]
func_1;
3|Page P J Nandagopal
Shell Script
sh myscript.sh
No argument passed
sh myscript.sh No
Function not trigerred
sh myscript.sh who
Prints nothing
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.
5|Page P J Nandagopal
Shell Script
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”
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