Thursday, May 17, 2012

How to Write a Shell Script Using Bash Shell in Ubuntu: 10 steps

How to Write a Shell Script Using Bash Shell in Ubuntu: 10 steps [article taken from the Anil Mahadev & 4 others]


How to Write a Shell Script Using Bash Shell in Ubuntu

Edited byAnil Mahadev and 4 others
Article
Edit
DiscussHistory
Write a Shell Script Using Bash Shell in Ubuntu
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

  1. 1
    Terminal
     Terminal
    Launch 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
  2. 2
    Next in the Terminal launch the vi/vim editor. My fav is vim.
  3. 3
    In 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.
  4. 4
    At the top type the following code #!/bin/bash. This is known as a Shebang line
  5. 5
    master script 1
     master script 1
    Now type the code as shown in the figure
  6. 6
    Now 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.
  7. 7
    executable
     executable
    Now we need to make this Shell Script, executable. To do this, enter the following command
  8. 8
    chmod +x ListDir.sh and hit enter. You can see the results below
  9. 9
    script output
     script output
    To run this Shell Script, simply type ./ListDir.sh . You should get the list of directories and the message as shown below
  10. 10
    Congratulations on writing your 1st Shell Script using Bash in Ubuntu Linux!

No comments:

Post a Comment