Differences
This shows you the differences between two versions of the page.
— | bash [2020/09/09 05:18] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== #!/bin/bash ====== | ||
+ | * [[csh]] | ||
+ | * [[ksh]] | ||
+ | * [[sh]] | ||
+ | * [[tcsh]] | ||
+ | * [[zsh]] | ||
+ | |||
+ | * [[bash Built-In Commands]] | ||
+ | * [[bash completion]] | ||
+ | * [[bsfl]] | ||
+ | * [[getopts]] | ||
+ | * [[iterm]] | ||
+ | |||
+ | * [[ftp:// | ||
+ | * [[ftp:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | |||
+ | == String Operators == | ||
+ | |||
+ | * **-a** and | ||
+ | * **-o** or | ||
+ | * **=** or **==** equal to | ||
+ | * **-n** string is not null | ||
+ | * **-z** string is null, that is, has zero length | ||
+ | |||
+ | == Numeric Operators == | ||
+ | |||
+ | * **-eq** number equals | ||
+ | * **-ne** number does not equal | ||
+ | * **-gt** greater than | ||
+ | * **-ge** greater than or equal to | ||
+ | * **-lt** less than | ||
+ | * **-le** less than or equal to | ||
+ | |||
+ | == File Operators == | ||
+ | |||
+ | * **-e** file exists (returns false in Bourne shell if file is a symlink) | ||
+ | * **-f** file is not a directory or device file | ||
+ | * **-d** file exists and is a directory | ||
+ | * **-h** file exists and is a symlink (also **-L**) | ||
+ | * **-s** file exists and has a size greater than zero | ||
+ | * **-w** file exists and is writeable | ||
+ | * **-x** file exists and is executable | ||
+ | * **-r** file exists and is readable | ||
+ | * **-b** file is a block device | ||
+ | * **-c** file is a character device | ||
+ | * **-p** file is a pipe | ||
+ | * **-S** file is a socket | ||
+ | * **-O** you are owner of file | ||
+ | * **-G** you are group owner of file | ||
+ | |||
+ | == Regular Expressions == | ||
+ | |||
+ | Use '' | ||
+ | |||
+ | Check if a variable is a positive integer: | ||
+ | |||
+ | < | ||
+ | [[ $1 =~ ^[0-9]+$ ]] | ||
+ | </ | ||
+ | |||
+ | == Functions == | ||
+ | |||
+ | Functions are created and called the same as any shell script: use command line arguments as parameters to pass, and echo any output. | ||
+ | |||
+ | <code bash> | ||
+ | function parse_input() { | ||
+ | | ||
+ | echo "You passed ${1} as an argument" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == Read each line == | ||
+ | |||
+ | <code bash> | ||
+ | cat filename | while read line; do | ||
+ | echo "Your line says ${line}" | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | == String subset == | ||
+ | |||
+ | Syntax: ${variable:< | ||
+ | |||
+ | <code bash> | ||
+ | string=" | ||
+ | echo ${string:2} # returns hai | ||
+ | </ | ||
+ | |||
+ | == Strings == | ||
+ | |||
+ | ** Get string length ** | ||
+ | |||
+ | <code bash> | ||
+ | ${#string} | ||
+ | expr length $string | ||
+ | </ | ||
+ | |||
+ | ** Find the index of the first occurrence of a letter in a string ** | ||
+ | |||
+ | Returns 0 on not found. | ||
+ | |||
+ | <code bash> | ||
+ | expr index abcdefg d | ||
+ | </ | ||
+ | |||
+ | ** Strings as commands ** | ||
+ | |||
+ | You need to use '' | ||
+ | |||
+ | See [[http:// | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | cmd=" | ||
+ | $cmd # Doesn' | ||
+ | eval $cmd # Works | ||
+ | </ | ||
+ | |||
+ | ** Simple sed replacement ** | ||
+ | |||
+ | <code bash> | ||
+ | echo testing testing2 | sed -e 's/ /\|/' # returns testing|testing2 | ||
+ | </ | ||
+ | |||
+ | ** Simple cut with a delimiter, grabbing a field ** | ||
+ | |||
+ | <code bash> | ||
+ | eix --only-names -Ie bash | cut -d "/" | ||
+ | </ | ||
+ | |||
+ | == Variables == | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | * **$#** - number of bash arguments | ||
+ | * **$*** - display all variables passed | ||
+ | * **$$** - pid of shell, or script being run | ||
+ | * **$0** - name of the script | ||
+ | * **$!** - pid of last job run in background | ||
+ | * **$_** - last command run, without the arguments | ||
+ | |||
+ | == Displaying variables == | ||
+ | |||
+ | When setting a variable using the backtick operator, use double quotes around the variable to retain newlines. | ||
+ | |||
+ | <code bash> | ||
+ | files=`ls` | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | == Console settings == | ||
+ | |||
+ | Get the size of the available cols, lines. | ||
+ | |||
+ | < | ||
+ | tput cols | ||
+ | tput lines | ||
+ | </ | ||
+ | |||
+ | == Cool bash stuff == | ||
+ | |||
+ | * **mktemp** (coreutils) make a temporary file in /tmp | ||
+ | |||
+ | == Get exit status from piped outputs == | ||
+ | |||
+ | " | ||
+ | |||
+ | <code bash> | ||
+ | $ true | false | true | ||
+ | $ echo ${PIPESTATUS[*]} | ||
+ | </ | ||
+ | |||
+ | == Special variables == | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | == Colors == | ||
+ | |||
+ | ^ Color ^ Normal ^ Bold ^ | ||
+ | | Gray | 00;38 | 01;30 | | ||
+ | | Black | 00;30 | | | ||
+ | | Red |00;31 | 01;31 | | ||
+ | | Green | 00;32 | 01;32 | | ||
+ | | Purple 1 | 00;33 | 01;33 | | ||
+ | | Blue | 00;34 | 01;34 | | ||
+ | | Purple 2 | 00;35 | 01;35 | | ||
+ | | Cyan | 00;36 | 01;36 | | ||
+ | | White | 00;37 | 01;38 | | ||
+ | | Bright Blue | | 01;37 | | ||
+ | | Yellow | 02;33 | | | ||
+ | | Dark Gray | | | | ||
+ | |||
+ | == Trapping signals == | ||
+ | |||
+ | You can use '' | ||
+ | |||
+ | This is useful in scenarios where a shell script calls other scripts, and cancelling them would only cancel that one executed script instead of the main one. | ||
+ | |||
+ | < | ||
+ | control_c() { | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | trap control_c INT | ||
+ | </ | ||
+ | |||
+ | == Command Line Control == | ||
+ | |||
+ | * Delete: Ctrl W | ||
+ | * Clear Line: Ctrl U | ||
+ | * Start of Line: Ctrl A | ||
+ | * End of Line: Ctrl E | ||
+ | |||
+ | == Check Input == | ||
+ | |||
+ | Check to see if input is an integer: | ||
+ | |||
+ | < | ||
+ | if [[ $1 == ? | ||
+ | </ |