Ever wanted to Automate operations in your OS? Ever wanted to write a program that could create a file, copy that file to a directory? And using the Famous BASH Shell in Ubuntu? If the answers to both these questions areYes!, then this is the How-To for you!
We shall cover a simple example. How to create a Shell Script that displays the list of files present in a directory
EditSteps
- 1Launch the Terminal by clicking on Applications --> Accessories --> Terminal
Run Bash Scripts
Run and Schedule Routine Tasks across cloud and non cloud servers.
www.scalextreme.com - 2Next in the Terminal launch the vi/vim editor. My fav is vim.
- 3In the terminal window, type
vim ListDir
.sh
and hit enter. Once inside the Editor, type "I" or "i". This will allow you to insert / start typing your shell script. - 4At the top type the following code
#
!/bin/bash
. This is known as a Shebang line - 5Now type the code as shown in the figure
- 6Now to come out of the vim editor, type the following key combinations, Esc, : , wq! This will write the changes to the file and bring you back to the terminal.
- 7Now we need to make this Shell Script, executable. To do this, enter the following command
- 8chmod +x ListDir.sh and hit enter. You can see the results below
- 9To run this Shell Script, simply type ./ListDir.sh . You should get the list of directories and the message as shown below
- 10Congratulations on writing your 1st Shell Script using Bash in Ubuntu Linux!
No comments:
Post a Comment