User Tools

Site Tools


shell_or_bash_scripts

This is an old revision of the document!


Shell or Bash Scripts

ShebangHeader, which tells where the interpreter is located
#!/bin/sh
http://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang
Sourcing
declare Array

Small a!!!

	declare -a map

	map[1]="myvalue"
	map[1]="myvalue2"

	echo ${map[1]}
declare Map

Big A!!!

	declare -A map

	map[test]="myvalue"
	map[test2]="myvalue2"

	echo ${map[test2]}
iterate array keys

Access via ${!array[@]}

for i in "${!array[@]}"
do
  echo "key  : $i"
  echo "value: ${array[$i]}"
done
dereference an array / map value
${ARRAY[2]}
${map[key1]}
If Else
  • Empty spaces after the parenthesis are critical: [ $a==$b ]
# check if it is the root device
if [ $device_name==$root_device_name ]
then
	echo "$device_name is root"
else
	echo "NOT root"
fi
Command Substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:

$(command)

or like this using backticks:

`command`
shell_or_bash_scripts.1464157341.txt.gz · Last modified: (external edit)