顯示包含「bash」標籤的文章。顯示所有文章
顯示包含「bash」標籤的文章。顯示所有文章

2017年3月10日星期五

BASH reference

#!/bin/bash    #tells *nix BASH should be used to run it

Special Parameters
$#    #Store the number of arguments passed from the command line
$1    #first parameter
$?    #Store the exit status of the last executed command
$_    #Print the last argument of the previous command
$$    #Return the process ID of the shell
$!     #Return the process ID of the last executed background process

${#var}    #Number of characters in $var
${#array} #The length of the first element in the array.

${para}  #same as $para. May be used for concatenating variables with strings.
${para-default},${para:-default}   #if para not set, use default
${para=default},${para:=default}    #if para not set, set it to default
${parameter+alt_value}, ${parameter:+alt_value}    #If parameter set, use alt_value, else use null string.
${parameter?err_msg}, ${parameter:?err_msg}If parameter set, use it, else print err_msg and abort the script with an exit status of 1.

The : makes a difference only when parameter has been declared and is null


$*     #* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@    #@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

For more details, please check:
http://tldp.org/LDP/abs/html/special-chars.html

[ is a synonym for test command. Even if it is built in to the shell it creates a new process.

[[ is a new improved version of it, which is a keyword, not a program.

Conditional Expression
[expr1 -ne expr2]    #Return true if expr1 is not equal to expr2
[expr1 -eq expr2]    #Return true if expr1 is equal to expr2
[expr1 -gt expr2]    #Return true if expr1 is greater than expr2
[expr1 -ge expr2]    #Return true if expr1 is greater than or equal to expr2
[expr1 -lt expr2]     #Return true if expr1 is less than expr 2
[expr1 -le expr2]    #Return true if expr1 is less than or equal to expr2
[-z  expr]    #Return true if the expression is null or empty
[expr =~ regular_expr]    #Return true if the regular expression is matched.
[expr1 -a expr2],[expr1]&&[expr2]    #Return true if both the expression is and
[expr1 -o expr2],[expr1]||[expr2]   #Return true if either of the expr1 or expr2  is true

[-a filepath],[-e filePath]    #Return true if file exists
[-f  filepath]   #Return true if it is file
[-d directory]    #Return true if it is directory
[-L filepath],[-h filePath]    #Return true if file is a symbolic link
[-S socket]    #Return true if file exists and socket file
[-b filepath]  #Return true if file is a block device
[-c filepath]  #Return true if file is a char device

[-r filepath]   #Return true if file is readable
[-w filepath]    #Return true if file is writable
[-x filepath]    #Return true if file is executable
[-u filepath]    #Return true if SUID is set
[-g filepath]    #Return true if SGID is set
[-k filepath]    #Return true if sticky bit is set
[-s filepath]    #Return true if file exists and has a size greater than 0

Number Notation
echo $((0xFFFF))          #Hex, Display 65535
echo $((032))                 #Octal, Output 26
echo $((2#11111111))  #Binary, Output 255

Frequency Used Command
source filepath    #executes the contents of a script in the current shell