1. Microsoft PowerShell
Tag: Bash
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
How to get bash or ssh into a running container in background mode?
Shell Scripting (Bash) Training | Bash/Shell Scripting Course
Introduction
- Introduction
- Why Shell Scripting?
- Linux Internal
- What to Expect From This Course?
- Prerequisites
Git fundamental
- Introduction of git
- Git setup
- Basic operations in git
- Github
- Summary
A First Look At Shell Scripts
- Introduction
- Creating A Shell Script
- Demo: A Note-Taking Script
- Calling The Script
- The Shebang
- Naming Your Script
- Demo: The type Command
- Summary
Variables
- Introduction
- Demo: Variables
- Using Variables in A Script
- Using Variables: Good Habits
- Reading Input
- Debugging your Script
- Summary
If, Then, Else
- Introduction
- Demo: The If Statement
- The If Statement
- Return codes
- The Conditional Expression
- Demo: The Conditional Expression
- The Conditional Expression 2
- Arithmetic Tests
- Demo: Arithmetic Tests
- The If Statement Revisited
- And, Or, Not
- Summary
Input and Output
- Introduction
- Output: echo and printf
- Input: read revisited
- Standard Streams and Redirection
- Demo: Redirection
- Summary
Control Flow
- Introduction
- While and Until
- The Classic For Statement
- The C-Style For Statement
- Break and Continue
- The Case Statement
- && and ||
- Summary
Variables 2
- Introduction
- Integer Variables
- Arithmetic Expressions
- Arithmetic Expressions 2
- Read-only Variables
- Exporting Variables
- Arrays
- Summary
Handling Script Parameters
- Introduction
- Special Variables
- Shift
- Getopts
- Getopts: Handling Errors
- Summary
Shell Functions
- Introduction
- Shell Functions
- Shell Functions 2
- Functions: Demo
- Some Miscellaneous Remarks
- Summary
Fun with Strings
- Introduction
- Removing Part Of A String
- Search and Replace
- Setting A Default Value
- Conditional Expression Patterns
- Regular Expressions in The Conditional Expression
- End of Options
- Summary
Many Ways to Run Your Script
- Introduction
- Running your Code
- Nohup and The Background
- Exec
- At and Cron
- Set and Shopt
- Summary
BASH aliases for GIT | Bash Git Aliases Reference
BASH aliases for GIT
Following BASH aliases can be used in ~/.BASHRC
alias g = “git status”
alias ga = “git add”
alias gaa = “git add .”
alias gc = “git commit -m”
alias gca = “git commit -am”
alias gb = “git branch”
alias gbd = “git branch -d”
alias gco = “git checkout”
alias gcob = “git checkout -b”
alias gm = “git merge”
alias gr = “git rebase”
alias gl = “git log”
alias gs = “git show”
alias gd = “git diff”
alias gbl = “git blame”
alias gps = “git push”
alias gpl = “git pull”
Understand Shell Script Parameters – Reference
A parameter is an entity that stores values. It can be a name, a number or some special characters.
Bash shell provides two kind of parameters.
Positional Parameter and Special Parameter
Bash Positional Parameter – $0, $1, $2 ..
Positional parameters are the arguments given to your scripts when it is invoked. It could be from $1 to $N. When N consists of more than a single digit, it must be enclosed in a braces like ${N}.
The variable $0 is the basename of the program as it was called.
The following example gets two arguments and provides arithmetic operations result between those two integers.
First, create the arithmetic.sh shell script as shown below.
$ cat arithmetic.sh
#!/bin/bash
echo -e “\$1=$1”
echo -e “\$2=$2”
let add=$1+$2
let sub=$1-$2
let mul=$1*$2
let div=$1/$2
echo -e “Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n”
Next, execute the arithmetic.sh with proper parameters as shown below.
$ ./arithmetic.sh 12 10
$1=12
$2=10
Addition=22
Subtraction=2
Multiplication=120
Division=1
In the above output $1 has the value 12, and $2 has 10.
Shell built-in ‘let’ allows arithmetic operation to be performed on shell variables. The above script does the arithmetic operations such as addition, subtraction, multiplication and division on the given parameters.
Set / Unset Bash Positional Parameters
The built in set command is used to set and unset the positional parameter.
First, create the positional.sh shell script as shown below.
$ cat positional.sh
#!/bin/bash
# From command line
echo -e “Basename=$0”
echo -e “\$1=$1”
echo -e “\$2=$2”
echo -e “\$3=$3”
# From Set builtin
set First Second Third
echo -e “\$1=$1”
echo -e “\$2=$2”
echo -e “\$3=$3”
# Store positional parameters with -(hyphen)
set – -f -s -t
echo -e “\$1=$1”
echo -e “\$2=$2”
echo -e “\$3=$3”
# Unset positional parameter
set —
echo -e “\$1=$1”
echo -e “\$2=$2”
echo -e “\$3=$3”
The above script prints the command line arguments first, then set command sets the positional parameter explicitly. Set with the – refers end of options, all following arguments are positional parameter even they can begin with ‘-’. Set with ‘–’ without any other arguments unset all the positional parameters.
Next, execute the positional.sh as shown below.
$ ./positional.sh
Basename=positional.sh
$1=12
$2=10
$3=
$1=First
$2=Second
$3=Third
$1=-f
$2=-s
$3=-t
$1=
$2=
$3=
Use Bash $* and $@ to Expand Positional Parameters
This example shows the value available in $* and $@.
First, create the expan.sh as shown below.
$ cat expan.sh
#!/bin/bash
export IFS=’-‘
cnt=1
# Printing the data available in $*
echo “Values of \”\$*\”:”
for arg in “$*”
do
echo “Arg #$cnt= $arg”
let “cnt+=1”
done
cnt=1
# Printing the data available in $@
echo “Values of \”\$@\”:”
for arg in “$@”
do
echo “Arg #$cnt= $arg”
let “cnt+=1”
done
Next, execute the expan.sh as shown below to see how $* and $@ works.
$ ./expan.sh “This is” 2 3
Values of “$*”:
Arg #1= This is-2-3
Values of “$@”:
Arg #1= This is
Arg #2= 2
Arg #3= 3
· The above script exported the value of IFS (Internal Field Separator) with the ‘-’.
· There are three parameter passed to the script expan.sh $1=”This is”,$2=”2″ and $3=”3″.
· When printing the each value of special parameter “$*”, it gives only one value which is the whole positional parameter delimited by IFS.
· Whereas “$@” gives you each parameter as a separate word.
Use $# to Count Positional Parameters
$# is the special parameter in bash which gives you the number of positional parameter in decimal.
First, create the arithmetic.sh as shown below.
$ cat arithmetic.sh
#!/bin/bash
if [ $# -lt 2 ]
then
echo “Usage: $0 arg1 arg2”
exit
fi
echo -e “\$1=$1”
echo -e “\$2=$2”
let add=$1+$2
let sub=$1-$2
let mul=$1*$2
let div=$1/$2
echo -e “Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n”
If the number of positional parameters is less than 2, it will throw the usage information as shown below,
$ ./arithemetic.sh 10
Usage: ./arithemetic.sh arg1 arg2
Process related Parameters – $$ and $!
The special parameter $$ will give the process ID of the shell. $! gives you the process id of the most recently executed background process.
The following script prints the process id of the shell and last execute background process ID.
$ cat proc.sh
#!/bin/bash
echo -e “Process ID=$$”
sleep 1000 &
echo -e “Background Process ID=$!”
Now, execute the above script, and check the process id which its printing.
$ ./proc.sh
Process ID=9502
Background Process ID=9503
$ ps
PID TTY TIME CMD
5970 pts/1 00:00:00 bash
9503 pts/1 00:00:00 sleep
9504 pts/1 00:00:00 ps
$
Other Bash Special Parameters – $?, $-, $_
· $? Gives the exit status of the most recently executed command.
· $- Options set using set builtin command
· $_ Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.
$ cat others.sh
#!/bin/bash
echo -e “$_”; ## Absolute name of the file which is being executed
/usr/local/bin/dbhome # execute the command.
#check the exit status of dbhome
if [ “$?” -ne “0” ]; then
echo “Sorry, Command execution failed !”
fi
echo -e “$-“; #Set options – hB
echo -e $_ # Last argument of the previous command.
In the above script, the last echo statement “echo -e $_” ($ underscore) also prints hB which is the value of last argument of the previous command. So $_ will give the value after expansion
$ ./others.sh
./others.sh
/home/oracle
Sorry, Command execution failed !
hB
hB