echo "Bash" > Scripting
What You will Learn
- Basic Scripting
- Variables
- Default/InBuild Variables
- Reading user input
- Read/Write Files
- Redirect Output
- For Loop
- If Statement
- Functions
- Curl
- Tool to Debug Your Script
My FirST SCRIPT
- Editors
- Use any text editor
- vi
- visual studio
- intellij etc
- File Format should be .sh
- File should have execution permission
- First line:
- #!/bin/bash
Variable declarations
#A variable is a temporary store for a piece of information
#Unlike many other programming languages, Bash does not segregate its variables by "type."
FIRST_VARIABLE = "My first variable"
echo $FIRST_VARIABLE
MY_MESSAGE="Hello World"
MY_SHORT_MESSAGE=hi
MY_NUMBER=1
MY_PI=3.142
MY_OTHER_PI="3.142"
MY_MIXED=123abc
Command Line Arguments
$* All arguments on command line (" $1 $2 ...").
$0 First word; that is, command name
$1 First command line argument
$2 2nd command line argument
.
.
.
$n nth command line argument
$# Number of command-line arguments.
$@ All arguments
$$ Process number of current process.
$? Exit code of prvious command
Reading user input
#!/bin/bash
# Ask the user for their name
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname
##########################################################
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
echo Thankyou $uservar we now have your login details
##########################################################
ExerCISE USER INPUT
- Show message to user:
- "What cars do you like?"
- "Provide me 3 options:"
- Read three input from user
- Output of script:
- Your first car was: {First Input}
- Your second car was: {Second Input}
- Your third car was: {Third Input}
for loops
# looping through range
#!/bin/bash
for i in {1..10}
do
echo "Looping ... number $i"
done
#########################################
# Exit when user say Bye
#!/bin/bash
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
##################################################
# Print all command line arguments
#!/bin/bash
for var in $@
do
echo "$var"
done
if-then statements
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [ $VAR -gt 10 ]
then
echo "The variable is greater than 10."
elif [ $VAR -eq 10 ]
then
echo "The variable is equal to 10."
else
echo "The variable is less than 10."
fi
###################################
man test
Functions
#!/bin/bash
hello_world () {
echo 'hello, world'
}
hello_world
##################################################
# Print max number
#!/bin/bash
function max ()
{
if [ $1 -gt $2 ]
then
echo $1
else
echo $2
fi
}
max 5 10
Practice: OUTput
#!/bin/bash
var1='A'
var2='B'
my_function () {
local var1='C'
var2=$1
echo "Inside function: var1: $var1, var2: $var2"
}
echo "Before executing function: var1: $var1, var2: $var2"
my_function D
echo "After executing function: var1: $var1, var2: $var2"
REDIRECT OUTPUT
#Redirect to file
cat hello.txt > redirect.txt
#Append to file
cat hello.txt >> redirect.txt
#Redirect one command output to another command input
cat hello.txt | grep text
#redirects stdout
echo test 1> afile.txt
#redirect stderr
test.sh 2> error.txt
READ File
#!/bin/bash
cat filename.txt | while read line
do
echo $line
done
######################################################
#!/bin/bash
while read line
do
echo $line
done < filename.txt
######################################################
#!/bin/bash
while IFS=' ' read -r line
do
echo $line
done < filename.txt
curl
#curl is a command line tool to transfer data to or from a server. It support many protocols.
#Examples:
# HTTP GET
curl -X GET https://wwwegenciaeu.int-maui.sb.karmalab.net/checkout-service/base/version
#HTTP POST
curl -X POST \
http://localhost:15672/api/exchanges/checkout/egencia.checkout.offline.operation5/publish \
-H 'Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=' \
-H 'Content-Type: application/json' \
-d '{
"properties": {
"content_encoding": "UTF-8",
"content_type": "application/json"
},
"routing_key": "air",
"payload_encoding": "string",
"content_type": "application/json",
"payload": "{\"record_locator\" : 50013401398,\"event_type\" : \"CANCEL\",\"product_id\" : 60000,\"event_source\" : \"SUPPLY\",\"product_type\": \"FLIGHT\" }"
}'
Debugging
Debugging
Bash - session 2
By Issam Hammi
Bash - session 2
Second session start with some simple scripts
- 567