What is Bash ?
- Its a Unix Shell & Command Language.
- Also default command interpreter on most Linux systems.
Bash என்றால் என்ன என்று சொல்லிட்டீங்க , but what is shell here ?
- Shell is a UNIX term for the interactive user interface with an operating system. The shell is the layer of programming that understands and executes the commands a user enters. In some systems, the shell is called a command interpreter.
- A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.
- Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems.
- Each flavor of shell has its own set of recognized commands and functions.
இவ்ளோ இருக்குன்னு பாக்காதீங்க இது எல்லாமே சின்ன சின்ன definitions which will be useful for interview.
- Bash stands for Bourne-Again SHell.
- As with other shells, you can use Bash interactively directly in your terminal, and also, you can use Bash like any other programming language to write scripts.
- Bash scripts are great for automating repetitive workloads and can help you save time considerably.
இப்ப உங்க projectல மொத்தம் ஒரு ஆறு பேர் இருக்கீங்க, அந்த ஆறு பேரும் ஒரே Java environment install பண்ணனும். ஒரு ஒரு machineல போய் பண்றதை விட , இதை நீங்க ஒரு script எழுதி , அந்த script ஓட்டிவிட்டா எல்லாம் machineல install ஆயிடும் இல்ல.
- We can use any Text editor like Sublime Text, VS Code, or a terminal-based editor like vim or nano for writing a Bash script.
NOW LET'S FALL INTO BASH SCRIPT
touch devdojo.sh
nano devdojo.sh
If you want to execute this bash script file with the bash shell interpreter , so we need to indicate the absolute path of the bash.
#! interpreter [optional-arg]
A shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.
Shebang என்ன சொல்லுதுன்னா , OS கிட்ட இது ஒரு bash shell scriptனு சொல்லுது அவ்வளவுதான்.
இப்ப இந்த fileல executableலா மாத்தணும்
Both ways we can run the script
./devdojo.sh
bash devdojo.sh
In Bash there is no data types BUT we can use variables ( it can also contain numbers too )
name="devops"
echo $name
echo ${name} --> Good Practice to use "{}"
Important : We can't have spaces before and after the "="
sign.
#!/bin/bash
name="devops"
echo "I am learning ${name}"
A quick review or rundown
Test with passing arguments :
Two arguments
Read from User Input
echo "what are you learning?"
read name
echo "He is learning $name"
echo "welcome to current technology"
Input கொடுத்தோம் இல்ல , அந்த value எடுத்துக்க READ ஆபரேஷன் use பண்ணனும்.
Codeஅ இப்ப reduce பண்ண போறோம்
read -p "what are you learning?" name
echo "He is learning $name"
echo "welcome to current technology"
echo ---> read -p
The read command used with -p flag will print a message before prompting the user for their input.
# This is a comment and will not be rendered on the screen
Why we are commenting using "#" ?
Code உனக்கு மட்டும் புரிஞ்சா பத்தாது மத்தவங்களுக்கும் புரியணும் . அதுக்கு தான் நம்ம COMMENTS # யூஸ் பண்றோம்.
INFO 1 : $1 --> First Argument , $2 --> Second Argument , $3 --> Third Argument , அப்ப $0 எங்க போச்சு ? இது ஒரு வாஸ்தவமான பேச்சு . $0 is used to reference the script itself.
#!/bin/bash
echo "The name of the file is: $0 and it is going to be selfdeleted."
rm -f $0
நல்ல Note பண்ணுங்க அந்த rm command கொடுத்ததினால் அந்த file delete ஆயிடுச்சு பாருங்க.
We need to be careful with the self deletion and ensure that you have your script backed up before you self-delete it.
ARRAYS
Arrays can hold several values under one name
- Initialize an array by values divided by space and enclosed by ().
- Using @ will return all arguments in the array.
- Prepending the array with a hash sign (#) would output the total number of elements in the array.
#!/bin/bash
my_array=("cat" "dog" "fish" "elephant")
echo ${my_array[1]}
echo ${my_array[2]}
echo ${my_array[-1]}
echo ${my_array[@]}
echo ${#my_array[1]}
echo ${#my_array[@]}
name=("sathishpy1808")
echo "${#name[0]}"
SLICING IN A STRING
#!/bin/bash
alphabets=("A""B""C""D""E")
#print all the elements of an array
echo ${alphabets[@]}
#print array from starting index 0 to 2 where 2 is exclusive
echo ${alphabets:0:2}
#will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0
echo ${alphabets::5}
#print from starting index 3 to end of array inclusive
echo "${alphabets:3}"
alphabets=("A""B""C""D""E")
alphabets=("A" "B" "C" "D" "E") --> In this SPACE will be counted , so be cautious in providing the values in a array while using Slicing.
CONDITIONAL EXPRESSIONS
- Conditional expressions are used by the [[ compound command and the [built-in commands to test file attributes and perform string and arithmetic comparisons.
- True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern matching as described above (Compound Commands).
CONDITIONAL EXPRESSIONS
- use "CONDITIONAL EXPRESSIONS" in if, if-else and switch case statements.
Syntax or Format :
if [[ some_test ]]
then
<commands>
fi
#!/bin/bash
# IF statement example
read -p "Enter the name: " name
if [[ -z ${name} ]]
then
echo "PLEASE enter your name"
fi
With ELSE
#!/bin/bash
# IF statement example
read -p "Enter the name: " name
if [[ -z ${name} ]]
then
echo "PLEASE enter your name"
else
echo "Welcome $name"
fi
With conditional expressions
#!/bin/bash
# IF statement example
name1="devops"
read -p "Enter the name: " name
if [[ ${name} == ${name1} ]]
then
echo "YOUR name is correct"
else
echo "Requesting to CHECK your name"
fi
INFO 2:
- An if statement which would check your current UserID and would not allow you to run the script as the root user.
- If you put this on top of your script it would exit in case that the EUID is 0 and would not execute the rest of the script.
#!/bin/bash
if [[ $EUID == 0 ]];
then
echo "Please do not run as root"
exit
fi
With Multiple Conditions
- In this example we want to make sure that the user is neither the admin user or the root user to ensure the script is incapable of causing too much damage.
- We'll use the or operator in this example, noted by ||. This means that either of the conditions needs to be true. If we used the and operator of && then both conditions would need to be true.
[TBD]
#!/bin/bash
admin="sathishpy1808"
read -p "Enter your username? " username
# Check if the username provided is the admin
if [[ "${username}" != "${admin}" ]] || [[ $EUID != 0 ]] ;
then
echo "You are not the admin or root user, but please be
safe!"
else
echo "You are the admin user! This could be very
destructive!"
fi
With Multiple Conditions avec elif
gt ---> greater than
lt ---> lesser than
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 0 ]] ; then
echo "The number is positive"
elif [[ $num -lt 0 ]] ; then
echo "The number is negative"
else
echo "The number is 0"
fi
Case Statement
[ TO BE CONTINUED ... ]
LINUX COMMANDS
- Daily இந்த command எல்லாம் use பண்ணி பாருங்க , அப்பதான் mindல நிக்கும் - நான் சொல்லல என் professor சொன்னாரு .
IMPORTANT NOTES:
https://www.shellcheck.net/ ---> Way to test your scripts is to use this fantastic tool. ( https://github.com/koalaman/shellcheck )
set -x (or) bash -x ./your_script.sh ---> Enable debug mode.
https://www.digitalocean.com/community/questions/what-are-your-favorite-bash-shortcuts
https://www.digitalocean.com/community/questions/how-to-check-if-running-as-root-in-a-bash-script
Top comments (0)