In my previous post we had learn about some scripting standards including the way how to given permission to our scripts in order to make them executable,Now we are gonna create our very first script "printing 'Hello World' ". I know you want to learn some real world useful scripts.We will learn them also with complete explanation. But before that we have to understand how the scripts work. For that reason we have to start with our very basic script. So lets start.
i) Create a new file with your favorite text editor. I recommend VIM text editor.
#vim hello_world.sh
name could be anything but its should be related to our script.
ii) Now press 'i' key to edit the file
iii) Now type below lines in your script file.
#!/bin/bash
#Author: Name
#Date: ddmmyy
#modify_date: ddmmyy
#description:
echo "Hello World"
iv) Press 'Esc' key > then type :wq! > then hit enter button , to save the script.
v) Yet our script is save but still it can not execute until we don't give it an executable permission. run below command to give the executable permission.
chmod +x Hello_World.sh
vi) To run the script from relative path as below.
./Hello_World.sh
vii) To make your output more visible, use expression in echo command instead of just echo.
echo -e "\nHello World\n"
Where e denotes for expression & \n denotes a new line. se echo man pages for more information.
Understanding Hello_World Script:
As you successfully run your very first basic script. now just loot at your script to understand how it work.
- #! (pronounce as shibang) tells our script to use given path shell by default. you can add multiple shells in order to run your script.
-/bin/bash is the path of shell which will execute our script
- # sign is used by shell to pass comments in our scripts & completely skipped at the time of execution. Its helpful to write comments in our scripts to read about the purpose of script & understand the script.
- commands are the main part of our script , which actually perform the task. You can add multiple commands in order to run. Commands must be supported by the system in order to run our script.Use loops , conditions for our scripts.
-Exit generally you don't need to define the exit to get out from a scripts.If script will get stuck in infinite loop or hang in case of any bug. just press ctrl + c or ctrl +z to kill the process.
That's All, Hope I am able to explain every thing about this script. If you have any question then comment below. If you like my work pleaase share it with your friends.
0 Comments