Commit b18c7e5e authored by Alex Segers's avatar Alex Segers

destroyed bash-scripting.md

parent 7975b5c2
# DevOps Foundation - Bash Scripting
## Table of Contents
[1-Script Parent path](#parentpath)
[2-Quote Marks](#quotemarks)
[3-Logic and comparisons](#logicalcomparison)
[4-Conditional statements](#conditionalstmt)
[5-Ranges](#ranges)
[6-Iterations](#iterations)
[7-Builtin Variables](#builtinvariables)
[8-Math Operations](#mathoperations)
[9-Defining and Initializing Array](#array)
[10-Using awk](#awk)
[11-Using sed](#sed)
[12-Using tr](#tr)
[13-Parameter Expansion](#parameterexpansion)
<a name="parentpath"/>
## Script Parent path
---
* Good practice to define Script folder if you reference
* other scripts with respect to current script
```
PARENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
```
<a name="quotemarks"/>
## Quote Marks
---
```
echo 'fixed literal'
foo=4
echo "Interpolate variables such as $foo"
echo "This is how we escape \"quotes\" within quotes"
`excute another command` and assigns to a variable
bar=`ls -l`
bar=`date`
```
<a name="logicalcomparison"/>
## Logic and comparisons
---
*Numeric Comparisons*
| Comparison | Description |
| --- | --- |
| int1 -eq int2 | Returns True if int1 is equal to int2. |
| int1 -ge int2 | Returns True if int1 is greater than or equal to int2. |
| int1 -gt int2 | Returns True if int1 is greater than int2. |
| int1 -le int2 | Returns True if int1 is less than or equal to int2 |
| int1 -lt int2 | Returns True if int1 is less than int2 |
| int1 -ne int2 | Returns True if int1 is not equal to int2 |
*String Comparisons*
| Comparison | Description |
| --- | --- |
| str1 == str2 | Returns True if str1 is identical to str2. |
| str1 != str2 | Returns True if str1 is not identical to str2. |
| str | Returns True if str is not null. |
| -n str | Returns True if the length of str is greater than zero. |
| -z str | Returns True if the length of str is equal to zero. (zero is different than null) |
*File Comparisons*
| Comparison | Description |
| --- | --- |
| -d filename | Returns True if file, filename is a directory. |
| -f filename | Returns True if file, filename is an ordinary file. |
| -r filename | Returns True if file, filename can be read by the process. |
| -s filename | Returns True if file, filename has a nonzero length. |
| -w filename | Returns True if file, filename can be written by the process. |
| -x filename | Returns True if file, filename is executable. |
*Expression Comparisons*
!expression
Returns true if expression is not true
expr1 -a expr2
Returns True if expr1 and expr2 are true. ( && , and )
expr1 -o expr2
<a name="conditionalstmt"/>
## Conditional Statement
---
If...then
```
if [ expression ]
then
commands
fi
```
If..then...else
```
if [ expression ]
then
commands
else
commands
fi
```
If..then...else If...else
```
if [[ expression ]]
then
commands
elif [[ expression2 ]]
then
commands
else
commands
fi
```
<a name="ranges"/>
## Ranges
---
```
echo {1..10} # prints 1 to 10
echo {1..10..2} # prints 1 to 10 incrementing by 2
echo {a..z} # prints a to z
echo {A..Z} # prints A to Z
echo {A..Z}{1..2} # prints a combination A1 A2 B1 B2 C1 C2 ...
echo foo{A..Z}{1..2} # prints a combination fooA1 fooA2 fooB1 fooB2 ...
echo $(seq 1 10) # prints 1 to 10
echo $(seq 1 2 10) # prints 1 to 10 increment by 2
```
<a name="iterations"/>
## Iteration (Loops)
---
```
for var1 in list
do
commands
done
```
```
while [ expression ]
do
commands
done
```
<a name="builtinvariables"/>
## Built in variables
---
| Variable | Description |
| --- | --- |
| $1-$N | Stores the arguments (variables) that were passed to the shell program from the command line.|
| $? | Stores the exit value of the last command that was executed.|
| $0 | Stores the first word of the entered command (the name of the shell program). |
| $* | Stores all the arguments that were entered on the command line ($1 $2 ...). |
|"$@" | Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). |
<a name="mathoperations"/>
## MATH Operations
```
op1=4
op2=5
result=$(( op1 + op2 )) # result=9
result=$(( op2 - op1 )) # result=1
result=$(( 4 * 5 )) # result=20
result=$(( 4 % 2 )) # result=0
result=$(( 4 % 2 == 0)) # result=1
echo "$op1 * $op2" | bc # result=20
echo "$op1 * 1.2" | bc # result=4.8
```
<a name="array"/>
## Defining and Initializing Array
```
declare -a array # An empty array is defined
array[0]=valA # Setting value of a given array index
array[1]=valB
array[2]=valC
array=( valA valB valC ) # another way
${array[i]} # displays array's value for this index.
# If no index is supplied, array element 0 is assumed
${#array[i]} # to find out the length of any element in the array
${#array[@]} # to find out how many values there are in the array
```
<a name="awk"/>
## Using AWK
| Variable | Description |
| --- | --- |
| $0 | Complete line |
| $1 | First column |
| $2 | Second column |
| $X | so on |
| NF | Number of columns |
| NR | Total lines |
-F : Column Separator
awk -F, ' { print $0 }'
with search condition
```
awk -F, ' $1 == "foo" { print $0 }' myfile.csv # Filter lines with 1st column "foo" and print
awk -F, '{ print $3","$4" }' myfile.csv # Print column 3 and 4 of all lines
```
<a name="sed"/>
## Using sed
Syntax:
sed 's/pattern/replacewith/g' inputfile
where 'g' stands for Global. Multiple occurence of pattern will be replaced
For Regex:
^ Begin of Line
$ End of Line
Inplace substition of file
-i : To change in file
Example:
The following statement will change the value enabled from 0 to 1 and display on screen. If '-i' is added in the arguments, it will change in the file
```
sed 's/enabled=0/enabled=1/' /etc/yum.repos.d/CentOS-Vault.repo
```
<a name="tr"/>
## Using tr
Examples:
```
cat myfile | tr "is" "an" # will change occurrance "i" with "a" and "s" with "n"
cat myfile | tr ' ' ',' # change space to comma
cat myfile | tr ' ' '\n' # change space to newline
cat myfile | tr 'a-z' 'A-Z' # Change lower case to upper case
cat myfile | tr -d 'o' # will delete occurance of "o"
cat myfile | tr -s # will remove double spaces to single space
```
<a name="parameterexpansion"/>
## Parameter Expansion
```
${varname:-word} # if varname exists and isn't null, return its value;
# otherwise return word
${varname:=word} # if varname exists and isn't null, return its value;
# otherwise set it word and then return its value
FILE="/home/asad/test.tar.gz"
echo "${FILE#*.}" # print tar.gz
echo "${FILE##*.}" # print gz
ext="${FILE#*.}" # store output in a shell variable
echo "$FILE has $ext" # display it
```
---
## Questions:
[1. Generate SQL](./questions/1-generate-sql.md)
[2. Parse Columns](./questions/2-parse-columns.md)
[3. String Manipulation](./questions/3-string-manipulation.md)
[4. Iteration](./questions/4-iteration.md)
[5. Generate Report](./questions/5-generate-report.md)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment