|
############################################## Notations ############################################ |
|
Brackets [] |
|
Parentheses () |
|
Braces {} |
|
(Double) Quotation marks "" |
|
(Single) Quotation marks (apostrophes) '' |
|
Backticks `` (Same as the tilde ~ key) |
|
|
|
############################################## VARIABLES ############################################ |
|
|
|
# Declare the variables using declare |
|
declare -r read_only=1 # read only variable |
|
declare -i number # integer variable |
|
declare -a indices |
|
decalre -f function_name |
|
declare -x car="audi" # |
|
Another use of declare is |
|
declare | grep HOME # which returns HOME=/home/bozo |
|
|
|
# Variables are declared and assigned without $ and without {}. You have to use |
|
var=10 |
|
to assign. In order to read from the variable (in other words, 'expand' the variable), you must use $. |
|
|
|
$var # use the variable |
|
${var} # same as above |
|
${var}bar # expand var, and append "bar" too # output is 10bar |
|
$varbar # same as ${varbar}, i.e expand a variable called varbar, if it exists. |
|
|
|
You are also able to do some text manipulation inside the braces: |
|
STRING="This is a string" |
|
echo ${STRING// /_} |
|
|
|
$(command) - used to store the command result in |
|
STR2=$(date) |
|
Now STR2 stores the string "Thu May 7 09:32:06 PDT 2015". |
|
|
|
$VAR vs ${VAR} and to quote or not to quote: |
|
VAR=$VAR1 |
|
VAR=${VAR1} |
|
VAR="$VAR1" |
|
VAR="${VAR1}" |
|
all the above output will be same |
|
when to quote: let say if we have a directory with spaces like kafka server/, it is better to use quotes for VAR , so it |
|
thinks as a single output. if not treated as a multiple words and we cannot delete the directory using rm |
|
rm -rf $VAR #wont work |
|
rm -rf "$VAR" |
|
|
|
############################################## ARRAYS ############################################### |
|
|
|
# to declare and initiliaze an array |
|
array=( item1 item2 item3 ) |
|
distro=("redhat" "debian" "gentoo") |
|
|
|
# we can access the individual elements using |
|
${array[0]}" , "${array[1]}" , "${array[2]} |
|
|
|
# length of the array using |
|
${#array[@]} |
|
|
|
# to print all elements using for loop |
|
tLen=${#array[@]} |
|
for (( i=0; i<${tLen}; i++ )); |
|
do |
|
echo ${array[$i]} |
|
done |
|
|
|
# access the array indices using, this need to used in for loop |
|
for $index in ${!array[@]}: |
|
echo ${array[$index]} |
|
|
|
# to split the string based on specified delimiter |
|
IFS=':' read -r -a array <<< "TEST:0:1234" |
|
|
|
################################################# ASSOCIATIVE ARRAYS ############################################# |
|
|
|
# initialize the associative array |
|
declare -A services_mapping=( [AZURE]=azure_service_bus [BLOB]=blob) |
|
|
|
# another method to initialize the array |
|
declare -A services_mapping |
|
services_mapping[AZURE]=azure_services_bus |
|
services_mapping[BLOB]=blob |
|
|
|
# Display the content of the key: |
|
${services_mapping[AZURE]} |
|
|
|
# list all the keys |
|
${!services_mapping[*]} # will return an arrays. |
|
|
|
# Looping through keys and values in an associative array |
|
for key in "${!services_mapping[@]}"; do |
|
echo $key --- ${services_mapping[$key]}; |
|
done |
|
|
|
# Loop through all values in an associative array |
|
for value in "${services_mapping[@]}"; do |
|
echo $value; |
|
done |
|
|
|
# unsetting the associative array |
|
unset services_mapping |
|
|
|
# deleting the key |
|
unset services_mapping[BLOB] |
|
|
|
# deleting key which has space |
|
unset MYMAP["foo Z"] |
|
|
|
# length of the associatuve array |
|
${#services_mapping[@]} |
|
|
|
|
|
|
|
################################################ CONDITIOINAL OPERATORS ############################################ |
|
if operator: |
|
if [[ $container_name == "" ]] || [[ $image_name == "" ]] || [[ $json_file == "" ]]; then |
|
# do something |
|
fi |
|
|
|
# for strings comparison we use '==' |
|
# for arithmetic comparision we use '-eq' |
|
# to check whether the variable is empty or not, we use '-z' |
|
|
|
|
|
################################################## eval command ################################################### |
|
eval - construct command by concatenating arguments.The args are read and concatenated together into a single command. |
|
This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are |
|
no args, or only null arguments, eval returns 0. |
|
|
|
example: |
|
foo=10 x=foo |
|
y='$'$x |
|
echo $y # output to $foo |
|
eval y='$'$x |
|
echo $y # 10 |
|
In the first line you define $foo with the value '10' and $x with the value 'foo'. |
|
Now define $y, which consists of the string '$foo'. The dollar sign must be escaped with '$'. |
|
To check the result, echo $y. |
|
The result will be the string '$foo' |
|
Now we repeat the assignment with eval. It will first evaluate $x to the string 'foo'. Now we have the statement y=$foo which will get evaluated to y=10. |
|
The result of echo $y is now the value '10'. |
|
|
|
############################################ ARTITHMETIC EXPRESSIONS ############################ |
|
For integers: |
|
Use arithmetic expansion: $((EXPR)) |
|
num=$((num1 + num2)) |
|
num=$(($num1 + $num2)) # also works |
|
num=$((num1 + 2 + 3)) # ... |
|
num=$[num1+num2] # old, deprecated arithmetic expression syntax |
|
Using the external expr utility. Note that this is only needed for really old systems. |
|
num=`expr $num1 + $num2` # whitespace for expr is important |
|
|
|
For floating point: |
|
Bash doesn't directly support this, but there's a couple of external tools you can use: |
|
num=$(awk "BEGIN {print $num1+$num2; exit}") |
|
num=$(python -c "print $num1+$num2") |
|
num=$(perl -e "print $num1+$num2") |
|
num=$(echo $num1 + $num2 | bc) # whitespace for echo is important |
|
You can also use scientific notation (e.g.: 2.5e+2) |
|
|
|
|
|
################################################# GETOPTS ############################################## |
|
package="" # Default to empty package |
|
target="" # Default to empty target |
|
|
|
# Parse options to the `pip` command |
|
while getopts ":h" opt; do |
|
case ${opt} in |
|
h ) |
|
echo "Usage:" |
|
echo " pip -h Display this help message." |
|
echo " pip install <package> Install <package>." |
|
exit 0 |
|
;; |
|
\? ) |
|
echo "Invalid Option: -$OPTARG" 1>&2 |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
shift $((OPTIND -1)) |
|
|
|
subcommand=$1; shift # Remove 'pip' from the argument list |
|
case "$subcommand" in |
|
# Parse options to the install sub command |
|
install) |
|
package=$1; shift # Remove 'install' from the argument list |
|
|
|
# Process package options |
|
while getopts ":t:" opt; do |
|
case ${opt} in |
|
t ) |
|
target=$OPTARG |
|
;; |
|
\? ) |
|
echo "Invalid Option: -$OPTARG" 1>&2 |
|
exit 1 |
|
;; |
|
: ) |
|
echo "Invalid Option: -$OPTARG requires an argument" 1>&2 |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
shift $((OPTIND -1)) |
|
;; |
|
esac |