You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
2.4 KiB

4 years ago
= heredoc, Positional Parameters !
4 years ago
Apostolos rootApostolos@swarmlab.io
// Metadata:
:description: Intro and Install
:keywords: Linux, apt
:data-uri:
:toc: right
:toc-title: Πίνακας περιεχομένων
:toclevels: 4
:source-highlighter: highlight
:icons: font
:sectnums:
{empty} +
.NOTE
[NOTE]
====
Assuming you're already logged in
====
== heredoc
=== Exercise
[source,bash]
----
#!/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 ""
4 years ago
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
)
4 years ago
----
4 years ago
4 years ago
=== expansion
[source,bash]
----
real_fc="barca" # set real_fc
win="real_fc" # $win is "real_fc"
echo "${!win}" # we print the variable linked by $win's content
----
4 years ago
== 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
[source,bash]
----
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
----
== extras
4 years ago
- differences $@ $*
[source,bash]
----
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
----