-
Notifications
You must be signed in to change notification settings - Fork 3
Scripting
`some command`
plugs the some command
output into some other context.
Currently backticks are deprecated in favour of $(some command)
form which:
- permits nesting
$(some command $(some nested))
- treats
\\
differently
<(command_list)
Feeds the output of processes into the stdin of another process (piping allows only one
command output to be redirected into stdin of another process).
The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion.
Nothing more than shell implementation
Controls how parameters/expressions are expanded
Introduced by $
e.g. $param_name
. Variable may be enclosed in braces in order to separate it from adjacent characters
Begins with unquoted ~
, includes all following characters up to unquoted slash. Characters are treated as login name. Evaluation of ~login
yields login
user home directory.
todo
Single quotes preserve everything. No expansion occurs
Positional parameters
-
$#
number of command line arguments -
$*
all of positional arguments (single word)
Special
-
$?
last command's exit code -
$!
most recently executed process ID
if
statement tests wheter exit status of command is equal to 0
if test command; then
#sth
fi
bash has alias for test
command and is called [
so the statement becomes:
if [ command ]; then
fi
With Bash 2.02 extended test statement was introduced:
if [[ command ]]; then
fi
Extended test statement is not compliant with POSIX. It features:
- No need to quote variables (in
[
not quoted variables like "something with spaces" will yield error: too many arguments) - Regular expression matching with
=~
e.g.[[ $variable =~ .*string ]]
- Wildcard matching e.g.
[[ $(lxc-ls) == *"desired_container"* ]]
- Chaining tests with and and or like:
[[ ... && ... ]]
(otherwise:[] && []
)
To negate test condition:
if ! [ ... ]
condition | description |
---|---|
-f file |
true if file exists and is regular file |
-z string |
true if length of string is zero |
-n string |
true if length of string is non-zero |
Bash scripts can exit with error codes
Some codes have special meaning
code | meaning |
---|---|
1 | general catch-all |
2 | misuse of built-ins (e.g. syntax errors like missing keywords) |
126 | cannot execute command (permission issue or not executable script) |
127 | command not found (typo or command missing on $PATH ) |
128 | wrong exit argument |
128+n
|
fatal signal n
|
130 | termination via signal (ctrl+c ) |
print the quote sign
awk 'BEGIN { print "Here is a single quote <'"'"'>" }' #this is concat of three strings '...' "'" '...'
awk 'BEGIN { print "Here is a single quote <'\''>" }'
simple examples:
#longest line in data file
awk '{ if (length($0) > max) max = length($0) }
END { print max }' data
# Print every line that has at least one field
awk 'NF > 0' data
you can have multiple rules in awk (first goes first rule, then second, if line contains both patterns then will be processed two times)
awk '/12/ { print $0 }
/21/ { print $0 }' data
awk env variables:
-
AWKPATH
- contains awk programs -
AWKLIBPATH
- contains extensions (can be written in C/C++)
- https://google.github.io/styleguide/shell.xml
- https://github.com/robbyrussell/oh-my-zsh/wiki/Coding-style-guide
- http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
- https://www.kernel.org/doc/Documentation/process/coding-style.rst
- https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
- http://www.tldp.org/LDP/abs/html/internalvariables.html
- http://mywiki.wooledge.org/BashFAQ
- http://mywiki.wooledge.org/BashPitfalls
- https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
- http://wiki.bash-hackers.org/syntax/pe (http://wiki.bash-hackers.org/start)
- http://tldp.org/LDP/abs/html/exitcodes.html
- https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
- General
- OS
- Networks
- Configuration
- Protocols
- Link layer
- Sockets
- Routing
- Tunneling
- Debugging
- LoRa
- Virtualization
- Infrastructure as a code
- Desktop environments
- Monitoring
- Benchmarking