how to write a shell script using bash shell in ubuntu_ 10 steps

2
1/24/13 How to Write a Shell Script Using Bash Shell in Ubuntu: 10 steps www.wikihow.com/Write-a-Shell-Script-Using-Bash-Shell-in-Ubuntu 1/2 1 2 3 4 5 master script 1 How to 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 are Yes! , 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 Terminal Launch the Terminal by clicking on Applications --> Accessories --> Terminal Next in the Terminal launch the vi/vim editor. My fav is vim. 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. At the top type the following code # !/bin/bash . This is known as a Shebang line Now type the code as shown in the figure Steps

Upload: navneet-kumar

Post on 12-Apr-2015

31 views

Category:

Documents


4 download

DESCRIPTION

How to Write a Shell Script Using Bash Shell in Ubuntu_ 10 Steps

TRANSCRIPT

Page 1: How to Write a Shell Script Using Bash Shell in Ubuntu_ 10 Steps

1/24/13 How to Write a Shell Script Using Bash Shell in Ubuntu: 10 steps

www.wikihow.com/Write-a-Shell-Script-Using-Bash-Shell-in-Ubuntu 1/2

1

2

3

4

5

master script 1

How to Write a Shell Script Using Bash Shell inUbuntu

Ever wanted to Automate operations in your OS? Ever wanted to write aprogram that could create a file, copy that file to a directory? And using theFamous 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 displaysthe list of files present in a directory

Terminal

Launch the Terminal by clicking on Applications --> Accessories --> Terminal

Next in the Terminal launch the vi/vim editor. My fav is vim.

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.

At the top type the following code #!/bin/bash. This is known as a Shebang line

Now type the code as shown inthe figure

Steps

Page 2: How to Write a Shell Script Using Bash Shell in Ubuntu_ 10 Steps

1/24/13 How to Write a Shell Script Using Bash Shell in Ubuntu: 10 steps

www.wikihow.com/Write-a-Shell-Script-Using-Bash-Shell-in-Ubuntu 2/2

6

7

executable

8

9

script output

10

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.

Now we need tomake this ShellScript, executable .To do this, enter thefollowing command

chmod +x ListDir.sh and hit enter. You can see the results below

To run this Shell Script, simply type ./ListDir.sh . Youshould get the list of directories and the message asshown below

Congratulations on writing your 1st Shell Script using Bash in Ubuntu Linux!

By default, vim is not installed.

To install vim in your Ubuntu System, just issue the command -->

sudo apt-get install vim

enter your password and let Ubuntu do the magic

Next after the install is completed, issue the command

vim <yourfile.sh> to edit your shell scripts.

Ensure that the files you create, do not reside in any system directories of the / = root filesystem

For starters, place your shell scripts in your home directory as you begin your shell scriptjourney. Once comfortable, place them in your required directory system.

How to Install Software in Ubuntu

How to Organize Photos on Linux

How to Install TrueType Fonts on Ubuntu

How to Change the Timezone in Linux

How to Learn to Use Linux

How to Switch to Ubuntu

Tips

Warnings

Related wikiHows

xThank OurVolunteerAuthors.

Give wikiHow a +1.

Follow