BASH 101: Notes 
#local variables
[aesteban@localhost ~]$ Name=Angel
[aesteban@localhost ~]$ echo $Name
Angel
[aesteban@localhost ~]$ Name="Angel Cool"
[aesteban@localhost ~]$ echo $Name
Angel Cool
[aesteban@localhost ~]$ #readonly
[aesteban@localhost ~]$ readonly Name
[aesteban@localhost ~]$ Name="Angel Esteban"
bash: Name: readonly variable
[aesteban@localhost ~]$

#index arrays
[aesteban@localhost ~]$ Fruits[0]=apple
[aesteban@localhost ~]$ Fruits[1]=orange
[aesteban@localhost ~]$ Fruits[2]=banana
[aesteban@localhost ~]$ Fruits[3]=grapes
[aesteban@localhost ~]$
[aesteban@localhost ~]$ States=("California" "Texas" "Florida")
[aesteban@localhost ~]$ echo ${States[1]}
Texas

[aesteban@localhost ~]$ echo ${Fruits[2]}
banana
[aesteban@localhost ~]$ echo ${Fruits[*]}
apple orange banana grapes
[aesteban@localhost ~]$ echo ${#Fruits[*]}
4
[aesteban@localhost ~]$ unset Fruits
[aesteban@localhost ~]$ echo ${!States[*]}
0 1 2
[aesteban@localhost ~]$

#associative arrays
[aesteban@localhost ~]$ declare -A INFO
[aesteban@localhost ~]$
[aesteban@localhost ~]$ INFO["name"]="Angel Esteban Cool"
[aesteban@localhost ~]$ echo ${INFO["name"]}
Angel Esteban Cool
[aesteban@localhost ~]$
[aesteban@localhost ~]$ INFO=([name]="Angel Cool" [website]="angelcool.net")
[aesteban@localhost ~]$
[aesteban@localhost ~]$ echo ${INFO["website"]}
angelcool.net
[aesteban@localhost ~]$


Loops
[aesteban@localhost ~]$ for i in $(seq 1 5); do echo This line corresponds to line $i.;done;
This line corresponds to line 1.
This line corresponds to line 2.
This line corresponds to line 3.
This line corresponds to line 4.
This line corresponds to line 5.
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$ for i in {1..7}; do echo This is number $i.;done;
This is number 1.
This is number 2.
This is number 3.
This is number 4.
This is number 5.
This is number 6.
This is number 7.
[aesteban@localhost ~]$
[aesteban@localhost var]$ for i in `ls /var`;do echo $i;done;
...
[aesteban@localhost ~]$# looping through a list of servers
[aesteban@localhost ~]$for i in {1..5}; do ssh www-app0${i} "cd /var/www/html/barney.com && git pull origin live" ;done;


Comments
Comments are not available for this entry.
2024 By Angel Cool