Shell learning notes – Basics

Time:2021-2-22

Shell learning notes – Basics

Learning course

LearnShell

Learning content

Hello,World

Knowledge points

  • Beginning of shell file
  #!/bin/bash
  • See all bash programs running
  ps | grep $$
  • See the bash interpreter
   which bash
  • Output Hello, world
#!/bin/bash
echo "Hello,world!"

variable

  • Define variable: use equal sign = (can’t have blank)
PRICE_PER_APPLE=5
MyFirstLetters=ABC
greeting='Hello        world!'
  • Escape character: use backslash\
PRICE_PER_APPLE=5
echo "The price of an Apple today is: $HK $PRICE_PER_APPLE"
  • Avoid confusion. ${} reference variables
MyFirstLetters=ABC
echo "The first 10 letters in the alphabet are: ${MyFirstLetters}DEFGHIJ"
  • Multiple white space characters are referenced by
greeting='Hello        world!'
echo $greeting" now with spaces: $greeting"
  • The output of the command is referenced as the value of the variable
greeting='Hello        world!'
echo $greeting" now with spaces: $greeting"
  • Time variable
FileWithTimeStamp=/tmp/my-dir/file_$(/bin/date +%Y-%m-%d).txt
echo $FileWithTimeStamp
  • practice
BIRTHDATE="Jan 1, 2000"
Presents=10
BIRTHDAY=`date -d "$date1" +%A`

if [ "$BIRTHDATE" == "Jan 1, 2000" ] ; then
    echo "BIRTHDATE is correct, it is $BIRTHDATE"
else
    echo "BIRTHDATE is incorrect - please retry"
fi
if [ $Presents == 10 ] ; then
    echo "I have received $Presents presents"
else
    echo "Presents is incorrect - please retry"
fi
    echo "I was born on a $BIRTHDAY"

Bithday can’t get the value in MacOS, and it runs normally in CentOS

Passing variables to a script

  • Script execution starts with parameter $1
  • $0 represents the script itself
  • Number of $# parameters
#!/bin/bash
echo $3
BIG=$5
echo "A $BIG costs just $6"
echo $#
sh my_shopping.sh apple 5 banana 8 "Fruit Basket" 15

array

  • () contains
  • Space interval
  • You don’t need to initialize each one
  • Length of array ${#arrayname [@]}
  • Read an element ${arrayname [3]}
my_array=(apple banana "Fruit Basket" orange)
new_array[2]=apricot
echo  ${#my_array[@]}                   # 4
my_array[4]="carrot"                    # value assignment without a $ and curly brackets
${my_array[3]}    #read element of array

Mathematical operators

$((expression))

String operation

  • String length
STRING="this is a string"
echo ${#STRING}
  • index
STRING="this is a string"
SUBSTRING="hat"
expr index "$STRING" "$SUBSTRING"

>This command is not supported on MAC, CentOS can

  • Intercept substring
STRING="this is a string"
POS=1
LEN=3
Echo ${string: $pos: $len} if there is no $len, truncate to the end of the string

>This command is not supported on MAC, CentOS can

  • String replacement
AA = "to be or not to be" # [@] not required
Echo ${AA [@] / be / eat} matches the first
Echo ${AA [@] // to be /} match all
Echo ${AA [@] / #to be / eat now} #
Echo ${AA [@] /% be / eat} matches with be
Echo ${AA [@] /% be / be on $(date +% Y -% m -% d)} replace with function at the end

Judgment sentence

  • if [ expression ]; then

    • Empty string (including multiple spaces), undefined is false
    • ! negate
    • &&For and, || for or – conditional expressions, double brackets are required to refer to [[]]
NAME="CC"

if [ "$NAME" = "AA" ]; then
    echo "I'm AA"
elif [ "$NAME" = "CC" ]; then
    echo "I'm CC"
else
    echo "I'm BB"
fi
  • Case statement
case "$variable" in
    "$condition1" )
        command...
    ;;
    "$condition2" )
        command...
    ;;
esac

mycase=1
case $mycase in
    1) echo "You selected bash";;
    2) echo "You selected perl";;
    3) echo "You selected phyton";;
    4) echo "You selected c++";;
    5) exit
esac

Loop statement

  • For loop
#!/bin/bash

array=(1 4 9 16 25)

for arg in ${array[@]}
do
echo $arg
done
  • While loop
# basic construct
while [ condition ]
do
 command(s)...
done


COUNT=4
while [ $COUNT -gt 0 ]; do
  echo "Value of count is: $COUNT"
  COUNT=$(($COUNT - 1))
done
  • Until statement
# basic construct
until [ condition ]
do
 command(s)...
done

COUNT=1
until [ $COUNT -gt 5 ]; do
  echo "Value of count is: $COUNT"
  COUNT=$(($COUNT + 1))
done

function

function_name {
  command...
}
function function_B {
  echo "Function B."
}
function function_A {
  echo "$1"
}
function adder {
  echo "$(($1 + $2))"
}

# FUNCTION CALLS
# Pass parameter to function A
function_A "Function A."     # Function A.
function_B                   # Function B.
# Pass two parameters to function adder
adder 12 56