BASH Talk

Intro to Bash and Batch Processing

Posted by Alexander Powers on 2019-03-30

This spring, I gave a talk on one of my favorite languages called BASH, the Bourne Again SHell.

What is BASH?


BASH is a unix shell and command language that reads from standard input or from a file. BASH allows you to interact with the filesystem and run/manage other programs through the command line. File system navigation and manipulation (coping, moving, deleting) isn’t neccesarily easier in BASH, but once you practice using it it is way faster then a graphical user interface. The talk that I gave focused mainly on the basic commands, and working with large quantities of files. The workshop slides and some batch scripting examples can be found on my github here.

Some Scripts


A script that makes script files with boiler plate and input validation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env bash
# Author: abpwrs
# Date: 20190325

if [[ $# -ne 1 ]]; then
echo "Usage: $0 <name-of-new-shell>"
exit
fi

cat > "$1" << EOF
#!/usr/bin/env bash
# Author: $USER
# Date: `date +%Y%m%d`

# args:
# 1 --
# 2 --

#if [[ \$# -ne 1 ]]; then
# echo "Usage: \$0 <param-1> <param-2> ..."
# exit
#fi

# script:
EOF

chmod 754 $1

A script that creates date formatted directories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash
# Author: abpwrs
# Date: 20190326

if [ $# -ne 1 ]; then
echo "... Incorrect Usage ..."
echo "Usage: ${0} ProjectNameNoSpaces"
exit -1
fi

DATE=`date +%Y%m%d`
PROJECT_NAME=${1}
DIR_NAME="${DATE}_${PROJECT_NAME}"
mkdir ${DIR_NAME}

An example of both ways to create functions in bash.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash
# Author: alexander
# Thu Feb 28 21:11:30 CST 2019

func_a (){
echo "inside of a"
}

function func_b (){
echo "inside of b"
}

func_c (){
echo "entering c"
func_a
func_b
echo "leaving c"
}

func_a
func_c
func_b

resulting in the following:

1
2
3
4
5
6
inside of a
entering c
inside of a
inside of b
leaving c
inside of b