Topic Desciption
How to check in which shell you are /
How to see all the available shells
Another way to check your shell is
SHELL BASICS How to start your bash file?
How to define and store variable
Variables How to store the linux command in a variable?
How to use defined variable in use?
How to use defined variable in use with string?
How to get result of a command witin a string
Echo How to display the last typed command in terminal
How to Write
replace multiple
a string lines
with something else
String Operations cut a portion of string
User Interaction How
If we to taketoinput
need frommultiple
execute user commands, we can use block
Block of Code of code
We can define multipel values in array
Arrays Array with key-value pairs
TEST Condition How to check the conditions
How toare
What write
the if-else condition
different conditional opertaions we can use with
if-else
If you wanna check the existence of file or folder using IF
condition
IF-ELSE For multiple If-Else condition
How to write a for loop
Different
To iterateoptions
throughtoallwrite
the for loop passed while running
argument
script
Infinite loop using foor
FOR LOOP How to iterate the values from a file
How to write a while loop
While loop
While loop toto get
get the
the values
line by from
line content from
file which area not
file separated
by space?
WHILE LOOP Getting the values from csv file (as first row is column name)
UNTIL LOOP How to write until loop
EXPRESSIONS How to perform airthmetic operation in script
CASES How to write a cases-switch
Connection Check How to
How to check
check thethe connectivity
connectivity with
with the
the remote
remote server
server for
Connection Check multiple hosts defined in a separate file named hosts
How to create an alias for a command
How to create alias for a specific user
Alias How to create alias for all the user globally
How to get the no. of arguments passed to a script
ARGUMENTS
How to
How to display
display a the argument
specific passedex
argument, with script
arg1, arg2, arg3
ARGUMENTS IWe want to print arg1
can use sleep command to create a delay between two
SLEEP task
NULL DEVICE If you
If you want
dont wanna
to makeprintanythe output
gloabal of a command
variable inside a in script
script as
READONLY constant
shift then
can be1-B,
used2-Cto generate random passwords etc
RANDOM NOTE:
In orderIttoisavoid
a bash thevariable
repetative code in the script, you can
use the function
Functions How
If youtowant
use to thelogarguments/parameters in function
your content or messages of script into
LOGGER /var/log/messages file
When there is an argment passed, it will be stored in
GETOPTS OPTARG variable
Ways to perform mathematic operations
INFINITE LOOP How to make an infinite loop?
LOGICAL OPERATORHow to use logical operators !, &&, ||
basename Ternary Operator
dirname string
to operations
enable on pathwe can set some properties in the
the debugging
Debugging Propertiesbeginning of script
REMOTE ACCESS Different options to write for loop
We can provide the password while doing remote operations
Command
echo $0
cat /etc/shells
cat /etc/passwd
#!/bin/bash
var_name=value
var_name=$(hostname)
echo $var_name
echo my name is $name
echo "my name is ${name} and age is ${agevar}"
echo "my
Output: hostname
When is $(hostname)"
user run this script using ./script_name
o/p will be: User typed: ./script_name (which will be last typed )
.....
EOF
sliceString=${y:start_position:length}
To store the values in an array from a user
read -a myarray.
condition1 (user hostname}
&& {ls; pwd; need to provide space separated values)
myArray+=( 5 6 8 )
myArray[name]=Prashant
[[ 8 -eq 5 ]]
echo "Sorry you are not eligible to vote"
fi
-gt
-lt
if [ -r/w/x /file_path ] If path is readable/writable/executable or not
echo "You are from Earth"
fi
done
for ((i=0;i<=10;i++))
for i in $@
for ((;;))
echo $item
done let count++
done
echo $myvar
done
done < < file_path
file_path
cat filename
a=`expr $a| +
awk
1` '!NR=1 {print}' | while IFS="," read f1 f2 f3
done
bc<<<"scale=2;$x/$y"
esac
ping hostname
fi
done
alias d="ls -ltr | awk '{print \$9}'"
echo alias hh="hostname" >> /home/<user>/.bashrc
echo alias hh="hostname" >> /etc/bashrc
echo "you have passed ${#} arguments"
echo $@
echo $1
sleep 1s
sleep 1m
hostname > /dev/null
readonly var_name = 123
shift
echo $RANDOM
myfun
myfun 12 13
logger "hello"
done
while
[[ true ]]
$x=~y|yes
If 1 is true then execute2
else execute
dirname: 3 provide the path with filename, it remove the filename and
if you
provide the directory path
set -x
ssh (print
-t -o command before executing
StrictKeyHostChecking=No it in script)set -n (syntax
user@<hostname> check)set -e (exit script if any command fails)while
"date;hostname"
sshpass -f <file_path>
Remark
will print hostname
New Life
to display a text and get
the user input
values are space
separated
provide more
functionalities
Keep the spacing in mind
separated by space so
no need to specify IFS
ex: a) param1 | param2
| param3 ;;
were successful, and 1 if
they were
creating a unsuccessful.
shortcut of the
most used commands
it is calledand
constant as null
con'tdevice
be
change
1-A
under and (2-B 3-C)
man bash inside the
accessible
function