Linux Shell Scripting

How to create shell script in vi editor that prints ''Hello World'' ?



What is Shell ?
Shell is the operating system's command interpreter and  the set of commands you use to communicate with the system. Shell reads command from user and tells Linux OS what users want.

What is shell script?
Shell script is a program that is written in shell programming language and is interpreted by a shell process. A shell script is a file that contains ASCII text. In order to create a shell script, a shell text editor is required. There are many text editors available for Linux system.

In this post I am going to use the vi editor.

What is a vi editor?
Vi stands for visual editor. It is a powerful Linux text editor which is available on all Unix / Linux system by default.

How to start vi?

  • Power on your virtual machine.
  • Login with your username and password.
  • Create a new file with .sh as extension. In order to do this type vi and the filename, e.g vi hello.sh
  • Now there are two modes: insert mode and command mode. By default vi is in command mode. Press i and type the following command : #!/bin/bash and then type echo "Hello World"
  • Next we have to switch back to the command mode, so go ahead press on the Esc key. 
  • Now we have to save the file and quit, in order to do this we need to type this command :wp then press enter key.
  • Next thing we have to do is to give the shell permission to execute the script. For this we have to type the following command chmod +x hello.sh and press enter.
  • To run type the following command ./hello.sh and press enter key. ''Hello World'' on your screen.
Details of commands used for this basic shell script:
vi hello.sh                     #To create new file.
#!/bin/bash                  #Explicitly tell the shell this is a bash script.
i                                      #To insert text.
echo                              #Command that tells the script to print.
:wq                                #To save program and exit.
chmod +x hello.sh     #Give the program permission to execute script.
./hello.sh                     #To run the program.


I hope this has been of great help, feel free to leave a comment.






Comments

Post a Comment

Popular posts from this blog

hackers.mu community meetup #1 #2 and #3

How to Install and Configure Git and Repositories on GitHub on Ubuntu using Command Line