NOTE

Assuming you’re already logged in

1. heredoc

1.1. Exercise

#!/bin/bash

var=$(cat <<EOF
declare -A array=(
  [1,0]="first name"
  [1,1]="second name"
  [2,0]="third name"
  [3,0]="foo bar"
  [test 1]="test bar"
  )
EOF
)

echo "$var"

cat <<'EOF'
  ----------------------------------------------------------
        for with "${!array[@]}"
  ----------------------------------------------------------
for i in "${!array[@]}"; do
        echo "$i => ${array[$i]}";
done

    "result: "
EOF

for i in "${!array[@]}"; do
        echo "$i => ${array[$i]}";
done

echo ""
echo ""


cat << FOE > testfile.sh

declare -A array=(
  [1,0]="first name"
  [1,1]="second name"
  [2,0]="third name"
  [3,0]="foo bar"
  [test 1]="test bar"
  )
EOF
)

1.2. expansion

real_fc="barca"            # set real_fc
win="real_fc"              # $win is "real_fc"
echo "${!win}"             # we print the variable linked by $win's content

2. Bash Positional Parameters

  • vim testvar.sh

  • cut-paste

  • chmod +x ./testvar.sh

  • ./testvar.sh a1 a2 a3 a4 a5 a6 a7 a8 a9 10 11 12 13

echo  $#
echo  $@
echo  $?


cat <<'EOF'
$# = number of arguments.
$@ = what parameters were passed.
$* = what parameters were passed.
$? = was last command successful. Answer is 0 which means 'yes'
EOF



cat << FOE > testfile.sh
#!/bin/bash
RUSER=$1
ID=$2
SERVICE=$3
SERVICE_STACK=$4
VNCport=$5
RREPLICAS=$6
RREGION=$7
TTYport=$8
PASSWORDrw=$9
PASSWORDview=${10}
GIT_TUTOR=${11}
REBOOT=${12}
USERscope=${13}
FOE
chmod +x ./testfile.sh

#./testfile.sh
#
cat << 'FOE' > testfile1.sh
#!/bin/bash
RUSER=$1
ID=$2
SERVICE=$3
SERVICE_STACK=$4
VNCport=$5
RREPLICAS=$6
RREGION=$7
TTYport=$8
PASSWORDrw=$9
PASSWORDview=${10}
GIT_TUTOR=${11}
REBOOT=${12}
USERscope=${13}
FOE
chmod +x ./testfile1.sh

3. extras

  • differences $@ $*

set -- "arg  1" "arg  2" "arg  3"

for w in $*;
do
  echo "$w";
done

arg
1
arg
2
arg
3

for w in $@;
do
  echo "$w";
done

arg
1
arg
2
arg
3

# see "
for w in "$*";
do
  echo "$w";
done

arg  1 arg  2 arg  3

# see "
for w in "$@";
do
  echo "$w";
done

arg  1
arg  2
arg  3