Shell Scripting (Bash)

Bash

Bash (Bourne Again Shell) is a powerful command-line interpreter and scripting language. Bash scripting can significantly enhance productivity by automating repetitive tasks.

Structure of bash

Example of a bash script script.sh:

1
2
3
4
5
6
7
#!/bin/bash  

# This is a comment
echo "Hello, World!" # This is an inline comment
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

To execute the above script, run ./script.sh arg1 arg2.

Variables

1
2
name="Alice"
echo "Hello, $name"

Note that there should be no spaces around the =.

Command Substitution

Whenever you place $( CMD ) it will execute CMD, get the output of the command and substitute it in place. For example:

1
2
3
4
% echo "We are in $(pwd)"
We are in /Users/zhaosonglin/Desktop/programming/python/numpy
% echo "Today is $(date)"
Today is Tue Jun 25 15:48:09 CST 2024

Exit codes

An exit code is a number returned by a command or script to the calling environment. By convention:

  • An exit code of 0 indicates that the command or script executed successfully.
  • A non-zero exit code indicates that an error occurred.

Special Variables

  • $0 - Name of the script
  • $1 to $9 - Arguments to the script. $1 is the first argument and so on.
  • $@ - All the arguments
  • $# - Number of arguments
  • $? - Return code of the previous command
  • $$ - Process identification number (PID) for the current script
  • !! - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doing sudo !!
  • $_ - Last argument from the last command.

Special Functions

? and * can be used to match one or any amount of characters respectively.

1
2
3
4
5
6
7
8
% ls
01 hstack.py 04 mask.py basic.ipynb
02 transpose.py 04 slicing.ipynb
03 least-square.py 05 histogram.py
% ls *.ipynb
04 slicing.ipynb basic.ipynb
% ls b?sic.ipynb
basic.ipynb

{} can be used when a set of strings have common substrings.

1
2
% mkdir a b
% touch {a,b}/{x,y,z}

The above command will create three files inside a and b folder (a/x, a/y, a/z, b/x, b/y, b/z) respectively.

1
2
mv *{.py,.ipynb} dest
# Will move all *.py and *.sh files to folder dest

Functions

1
2
3
4
5
6
7
8
#!/bin/bash

greet() {
echo "Hello, $1"
}

greet "Alice"
greet "Bob"

If Statement

1
2
3
4
5
6
7
8
9
#!/bin/bash

number=10

if [ $number -gt 5 ]; then
echo "The number is greater than 5."
else
echo "The number is 5 or less."
fi

Loops

1
2
3
4
5
#!/bin/bash

for item in item1 item2 item3; do
echo $item
done
1
2
3
4
5
6
7
8
#!/bin/bash

counter=1

while [ $counter -le 5 ]; do
echo "Counter: $counter"
counter=$((counter + 1))
done

Finding files

1
find . -name src -type d
  • find .: Start searching in the current directory.
  • -name src: Match directories named src.
  • -type d: Ensure that the match is a directory.
1
find . -path '*/test/*.py' -type f
  • find .: Start searching in the current directory.
  • -path ‘/test/.py’: Match files where test appears as a directory in their path and the filename ends with .py (* can substitute any string).
  • -type f: Ensure that the match is a regular file (not a directory).
1
find . -name '*.tmp' -exec rm {} \;
  • find .: Start searching in the current directory.
  • -name ‘*.tmp’: Match files ends with .tmp.
  • -exec rm {} ;: For each file found, execute the rm command to delete it. {} is replaced by the current file’s path.

Shell Scripting (Bash)
https://thiefcat.github.io/2024/06/25/MIT-Missing/Shell-Scripting/
Author
小贼猫
Posted on
June 25, 2024
Licensed under