Why Bash Scripting? (Bash)
- Bash stands for ‘Bourne Again Shell’
- Developed in the 80’s but a very popular shell today. Default in many Unix systems, Macs
- AWS, Google, Microsoft all have CLI’s to their products
- Bash Scripting helps in ease of execution of shell commands (no need to copy-paste every time!)
- Powerful programming constructs
Shell Commmands Refresher
Some important shell commands:
(e)grep
filters input based on regex pattern matchingcat
concatenates file contents line-by-linetail
\head
give only the last-n
(a flag) lineswc
does a word or line count (with flags-w
-l
)sed
does pattern-matched stringn replacement
A reminder of REGEX
‘Regex’ or regular expressions are a vital skill for Bash scripting.
You will often need to filter files, data within files, match arguements and a variety of otheruses. It is worth revisting this.
To test your regex you can use helpful sites like
regex101.com
Some Shell Practice
Let’s revise some shell commands in an example.
Consider a text fiel
fruits.txt
with 3 lines of data:1 2 3
banana apple carrot
If we ran
grep `a` fruits.txt
we would return:1 2 3
banana apple carrot
But fi we ran
grep `p` fruits.txt
we would return:1
apple
Recall that square parentheses are a matching set such as
[eyfc]
. Using^
makes this an inverset set (not these letters/numbers)So we could run
grep '[pc]` fruits.txt
we would return:1 2
apple carrot
You have likely used ‘pipes’ before in terminal. If we had many many fruits in our life we could use
sort | uniq -c
- The first will sort alphabetically, the second will do a count
- If we wanted the top n fruits we could then pipe to
wc-l
and usehead
1
cat new_fruits.txt | sort | uniq -c | head -n 3
1 2 3
14 apple 13 banana 12 carrot
First Bash Script
Bash Script Anatomy
A Bash Script has a few key defining features:
- It usually begins with
#!/usr/bash
(on itsown line)- So your interpreter knows it is a Bash script and to use Bash located in
/usr/bash
- This could be a different path if you installed Bash somewhere else such as
/bin/bash/
(typewhich bash
to check)
- So your interpreter knows it is a Bash script and to use Bash located in
- Middle lines contain code
- This may be line-by-line commands or programming constructs
- It usually begins with
Bash script anatomry
To save and run:
- It has a file extension
.sh
- Technically not needed if first line has the she-bang and path to Bash (
#!/usr/bash
), but a convention
- Technically not needed if first line has the she-bang and path to Bash (
- Can be run in the termina using
bash script_name.sh
- Or if you have mentioned first line (
#!/usr/bash
) you can simply run using./script_name.sh
- Or if you have mentioned first line (
- It has a file extension
Bash Script Example
An example of a full script (called
eg.sh
) is:1 2 3
#!/usr/bash echo "Hello world" echo "Goodbye world"
Could be run with the command
./eg.sh
and woudl output:1 2
Hello world Goodbye world
Bash and Shell Commands
Each line of your Bash Script can be a shell command.
Therefore, you can also include pipes in your Bash scripts.
Consider a text file (
animals.txt
)1 2 3 4 5
magpie, bird emu, bird kangaroo, marsupial wallaby, marsupial shark, fish
We want to count animals in each group
In shell you could write a chained command in the terminal. Let’s instead put that into a script (
group.sh
)1 2
#!/usr/bash cat animals.txt | cut -d " " -f 2 | sort | uniq -c
Now (after saving the script) runnign
bash group.sh
causes:1 2 3
2 bird 1 fish 2 marsupial
Standard streams & arguments
STDIN-STDOUT-STDERR
In Bash scripting, there are three ‘streams’ for your program:
- STDIN (standard input). A stream of data into the program
- STDOUT (standard output). A stream of data out of the program
- STDERR (standard error). Errors in your program
By default, these streams will come from and write out to the terminal.
Though you may see
2> /dev/null
in script calls; redirecting STDERR to be deleted. (1> /dev/null
) would be STDOUTSTDIN example
Consider a text file (
sports.txt
) with 3 lines of data.1 2 3
football basketball swiming
The
cat sport.txt
> new_sports.txtcommand is an example of taking data from the file and writing STDOUT to anew file. See what happends if you
cat new_sports.txt`1 2 3
football basketball swimming
STDIN v ARGV
A key concept in Bash scripting is arguments
Bash scripts can take arguments to be used inside by adding aspace after the script execution call
- ARGV is the array of all the arguments given to the program
- Each argument can be accessed via the
$
notation. The first as$1
, the second as$2
etc. $@
and$*
give all the arguments in ARGV$#
gives the length (number) of arguments
ARGV example
Consider an example script (
args.sh
):1 2 3 4 5
#!/usr/bash echo $1 echo $2 echo $@ echo "There are " $# "arguments"
Running the ARGV example
Now running
bash args.sh one two three four five
1 2 3 4
one two one two three four five There are 5 arugments