Tuesday, 3 March 2015

Create and Run shell script in Linux/Ubuntu

There are three steps to run the shell script

1.Write Script

 Open Terminal in the linux/ubuntu machine
 (Shortcut Ctrl+Alt+T )
 Use any of your linux Editor to write the code
   eg.,
       1.vi
       2.vim
       3.nano
       4.gedit
 

Create file with extension of .sh
eg., sudo nano test.sh

Shell script must contain the below line at the top


#!/bin/bash

this is used to to get current shell interpreter

eg.,


  #!/bin/bash
  #Test shell script
  echo "Hello User"

2.Covert to executable file


Convert the .sh file to executable



sudo chmod +x file_name.sh
  or
sudo chmod 755 file_name.sh

It will ask password for sudo , Enter your password.

3.Execute the script

In terminal type



./file_name.sh
or
sh file_name.sh

That's it.

Note :
#! - it is not a comment used to indicate shell script
#  - for added comment
echo- display message