Introduction to Automation and Shell Scripting in Linux (Part 03)


It will print name
It will create a folder
Inside folder it will create two files.
Writing First Basic Shell Script
Create a Shell Script file with extension .sh using Touch command.
Open file using command vi/vim.
Press esc i to insert/write something on file.
Start writing your Shell Script using specific indentation/syntax.
#!/bin/bash
Give details of the shell script.
Example :
Who is the author of this shell script.
When is this file written.
What this shell script will generate.
version
This is called metadata of the script.
Using the echo command, write code that can print name.
Using the cd command go to the place where you want to create a folder.
Using the mkdir command, create a folder named xxx, and inside that folder create two files xxx1 and xxx2.
Save your script by using the following commands.
!q for simply quiting
!wq save + quit
Run your script to see the result by using ./.
It will give the following error message.
That means the file you want to execute does not have execute permission. To grant the permission to the file, we will use chmod command.
Here, we have used chmod 777 to grant full permission to the file.
Now, execute the file again to see the result.
First we use the ls command to check if the myschool directory already exists to avoid error. There is no myschool directory.
Now we will execute our Shellscript using command the ./test.sh.
We can see the result: myschool directory has been created.
Use the ls myschool command to check Room1, Room2, Room3 files has been created.
Conclusion
In this blog, we have covered the essential steps for writing, executing, and troubleshooting a basic shell script. By following these guidelines, you'll be able to write simple shell scripts and troubleshoot them effectively. With practice, shell scripting becomes a powerful tool for automating tasks in the command line environment.
Subscribe to my newsletter
Read articles from Shahana Riaz directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
