Shell Tools and Scripting

Shell Scripting Intro

Most shells have their own scripting language with variables, control flow and its own syntax.
What makes shell scripting different from other scripting programming language is that
it is optimized for performing shell-tasks.
Thus,creating command pipelines,saving results into files,
and reading from standard input are primitives in shell scripting,
which makes it easier to use than general purpose scripting languages.
For this section we will focus on bash scripting since it is the most common.

Variables

To assign variables in bash,use the syntax foo=bar,
and access the value of the variable with $foo.
Note that foo = bar will not work
since it is interpreted as calling the foo programing with arguments = and bar.
In general,in shell scripts the sapce character will perform argument splitting.

String

String in bash can be defined with ' and " delimiters,
but they are not equivalent.
String delimited with ' are literal strings,
and will not substitute variables values whereas " delimited strings will.

1
2
3
4
5
foo=bar
echo "$foo"
# prints bar
echo '$foo'
# prints $foo

Control Flow

As with most programming languages, bash supports control flow techniques
including if, case, wihle and for.Similarly,
bash has functions that take arguments
and can operate with them.

1
2
3
4
5
6
mcd(){
mkdir -p "$1"
# create directory
cd "$1"
# cd the directory just created.
}

Here $1 is the first arguments to the script/function.
Unlike other scripting languages,
bash uses a variety of special characters
to refer to arguments, error codes, and other relevant variables.

Below is a list of some of them.
A more comprehensive list can be found here

  • $0 - Name of the script.
  • $1 to $9 - Arguments to the script,$1 is the first argument like argv in C language.
  • $@ - All the arguments.
  • $# - Number of arguments.
  • $? - Return code(status) of the previous command.
  • $$ - Process identification number(PID) for the current script.
  • !! - Entire last command ,including arguments.A common usage is sudo !!.
  • $_ - Last argument from the last command.

Commands will often return output using stdout,errors through stderr,
and return code to report errors in a more script-friendly manner.
The return code or exit status is the way scripts/commands have to communicate how execution went.
A value 0 usually means everything went OK.
Anything different from 0 means an error occurred.

Conditionally Execute Commands

Exit codes can be used to conditionally execute commands using &&(and operator)
and ||(or operator),both of which are short-circuiting operators.
Commands can also be separated within the same line using a semicolon ;.
The true program will always hava a 0 return code
and the false program will always hava a 1 return code.

1
2
3
4
5
6
7
8
9
10
11
12
false || echo "op, fail"
# op, fail
true || echo "will not be printed"
# will not be printed
true && echo "This went well"
# This went well
false && echo "will not be printed"
# will not be printed
true ; echo "This will always run"
# This will always run
false ; echo "This will always run"
# This will always run

Process Substitution

more info

Another common pattern is wanting to get the output of a command as a variable.
This can be done with command substitution.
whenever you place $(CMD) it will execute CMD,
get output of the command and substitute it in place.
A lesser known similar feature is process substitution.
<(CMD) will execute CMD and place the output in a temporary file named pipe.
and substitute the <() with that file’s name .
This filename is passed as an argument to current command.
If the >(CMD) form is used, writing to the file will provide input for CMD in ().
If the <(CMD) form is used, the file passed as an argument should be read to obtain the output of list.

1
2
3
4
5
6
7
8
9
10
11
#diff <(ls foo) <(ls bar)
# This will show differences between files in dirs FOO and BAR.
date | cat
cat <(date)
# Equal to date | cat
echo <(date) <(date) <(date)
# dev/fd/63 dev/fd/62 dev/fd/61
date > >(cat)
# writing to the file will provide input for cat
echo >(cat)
# dev/fd/63

Expand Expression

When launching scripts, you wil often want to provide arguments that are similar.
Bash has ways to making this easier, expanding expression by carrying out filename expansion.

  • Wildcards - When you want to perform some sort of wildcard matching,you can use ? and * to math one or more amount
    of characters respectively.
  • Curly braces{} -When never you have a common substring in a series of commands,you can use curly braces for bash to
    expand this automatically.
1
2
3
4
5
6
convert image.{png, jpg}
# will expand to
convet image.png image.jpg

touch {foo, bar}/{a..d}
# This create files foo/a ,foo/b....foo/d;bar/a...bar/d
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2023 Ataraxia

请我喝杯咖啡吧~