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

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

Shell script must contain the below line at the top


this is used to to get current shell interpreter


  #Test shell script
  echo "Hello User"

2.Covert to executable file

Convert the .sh file to executable

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

It will ask password for sudo , Enter your password.

3.Execute the script

In terminal type

sh file_name.sh

That's it.

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