So this thing is something I wanted to learn from soooooooo long, and yes finally I could learn it and would like to share the resources too you all as well 😀
Before starting, you should be aware of the following most used commands in Linux.
Why shell scripts? 🤔
- Many of the commands that need to be executed to complete a task can be grouped together in a script to help avoid repetition when typing the commands.
- It is better than having to remember, and type the commands every time the task is needed.
- It’s a series of commands written as a text file and ends with a .sh extension or none.
- You can execute your scripts in multiple manners :
- Executed by using bash command, for instance(assuming the script file is in your current directory while executing the command in cmd)
bash myscript.sh
- Having the file mode set as executable, make sure to make the file executable by changing the file permissions using chmod, in this case chmod a+x myscript.sh and adding the following line at the top of the file.
#!/bin/bash
and then executing by typing ./myscript.sh Incase you are wondering how adding this line did the magic, the story begins with something called as “sha-bang” which indicates the path where the interpreter to the shell exists (in our case the path was bin/bash) :
#!/fullpath/to/the/interpreter/executable
- Running your script as a command just like standard Linux commands, so this one was really exciting and interesting as it cleared my concepts on $PATH variable🔥
- Executed by using bash command, for instance(assuming the script file is in your current directory while executing the command in cmd)
Writing a shell script 🤓
-
Setting Values
var="Welcome everyone" #No spaces echo var # Output: var echo $var # Output: Welcome everyone var=filename_$(/bin/date+%Y-%m-%d).txt #Substitution echo $var #Output: filename_2018-06-08
Remember that there should be no spaces in between variable and operator “=”, else it will throw an error ⚠️
-
Basic Operations
Simple expressions can be evaluated like : $((expression))
chocolates=10 ice_cream=20 total_eatery=$((0.25 * $chocolates + 0.75 * $ice_cream)) echo $total_eatery
STRING="everyone loves shell scripting" echo $(#STRING) #Output: 30(length of string) start=5 length=3 echo $(STRING:$start:$length) #Output: one
-
Conditional Statements
var=0 if [ $var == 0 ]; then #start echo "Zero" else #execute else part echo "Not Zero" fi #finish # Output: Zero
Remember to leave space between the condition and square brackets in if-clause.
-
Loop structure
COUNT=4 while [ $COUNT -gt 0 ]; do echo "Value of count is: $COUNT" COUNT=$(($COUNT - 1)) done
- Functions
brownie_shake() { echo yummy } brownie_shake # Output: yummy
The arguments passed by console or to the function are passed via the dollar sign along with the position of the argument
brownie_shake() { echo brownie shake costs $1 } brownie_shake 100 # Output: brownie shake costs 100
-
Special Variables
$0 : Displays name of the script $* :All arguments passed to a function or through cmd $$ : Process ID of the current shell $# : Number of arguments passed to func or through cmd $n : Nth argument passed
Learnt all the basics, let’s figure out few other things that will save our lives 🤗
Shell scripts + Cron Jobs 😍
- Cron jobs – jobs that run at specific times on periodic basis.
- Combining these two superpowers can be awesome most of the time for instance your system needs some system checks at the end of every month, scripts along with cron jobs can automate the process and you can relax and chill 😎
Let’s put our learning to usage and code 🚨
Sample code 1 :
This one’s pretty easy, just clear the screen and print “I am learning shell scripting”
clear
echo "I am learning shell scripting"
Save and run the script file.
Great post! Shell scripts are really useful. And the best part – they don’t take too long to write 😀
LikeLike
Hey abhinav, I have to add some more codes there, thanks for appreciating ❤
LikeLike
Great ❤
LikeLiked by 1 person