Basic Shell Programming in Unix.
Hello Everyone,
Today we will discuss about how to write simple shell program in Unix and in next blog we will discuss more about shell scripts. Before starting basic shell programming please go through my below blog related with Unix command so that you can able to write basic shell programs.
https://dheeraj60.blogspot.com/2020/08/basic-and-some-advance-command-in-unix.html
https://dheeraj60.blogspot.com/2020/08/basic-and-some-advance-command-in-unix.html
https://dheeraj60.blogspot.com/2020/08/advance-unix-command-part1-dotcmdb.html
https://dheeraj60.blogspot.com/2020/08/advance-unix-command-part-2-dotcmdb.html
I am writing one basic simple script which will list all the files in the current directory , also it will show the present working directory name as well as all the process running in the system. Suppose we create a test.sh script , the scripts would have the .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct that is #!/bin/sh as it tells the system that the commands follow are to be executed by the Bourne shell. # symbol is called a hash, and the ! symbol is called a bang that’s why it known as shebang construct.
Steps to write first test.sh script to see the present working directory , list of all the files with timestamp and the process which is running in the system.
1. We need to open vi editor by writing vi test.sh in command prompt.
2. Once vi editor open to write anything we need to go into insert mode by using command esc i at same time to write the script in vi window.
3. Writing the script in vi editor as below for requirement.
#!/bin/bash
# Author : Dheeraj Jha
# Script1 : To see all the files , process running and present working directory
pwd
ls-ltr
ps –ef
Note : Hope you all are aware about this command as we discussed in my previous blogs only #!/bin/sh you are not aware which i already described above in this blog that is shebang . and # means comment.
4. Once you write the script to save you need to put esc command after that use :wq to save the file.
Note : if you use :q then it will not save your script as it will quit the file but :wq means save the file .
5. After this we can see the test.sh file in current directory but for execution of this script we need to give the permission of this file by using chmod command as below
$ chmod +x test.sh
Note : We can use chmod 777 test.sh command as well to execute this script.
6. Now we are able to run the test script by simply using the below command and it will give you the output.
$ ./test.sh
Output of above shell script in Putty terminal is as below:
Writing another shell program script uses the read command which takes the input from the keyboard and assigns it as the value of the any variable like DOTC and finally prints it. We need to follow the same step which we have done for test.sh script . So I am writing test1.sh script in vi editor is as below.
#!/bin/sh
# Author : Dheeraj Jha
# Script2 : To Take input as variable
echo "Training Centre Name?"
read DOTC
echo "Hello, $DOTC"
After that we need to save this test1.sh script and to give permission to this script we need to use chmod +x test1.sh or chmod 777 test1.sh same we done for test.sh script then we run the command $./test1.sh .
Once we run the command output of the script is as below in Putty terminal.
Here we put input as Diksha Oracle Training Centre so output is as Hello , Diksha Oracle Training Centre.
Now writing one more simple test3.sh script for array program to access the name by index of the value to be accessed . Here in test2.sh script we particularly access 2 items from the array in Program.
Writing test2.sh is similar to previous script we have written in vi editor.
Below is the script
#!/bin/sh
# Author: Dheeraj Jha
# Script 3 : Accessing particular elements from Array
NAME[0]="Diksha"
NAME[1]="Oracle"
NAME[2]="Training"
NAME[3]="Centre"
NAME[4]="Madhubani"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
After writing this in vi editor save this by using :wq and then use chmod 777 test2.sh
In test3.sh script we can access all the items in an array by using below script :
Program we need to write in vi editor is as below:
#!/bin/sh
# Author : Dheeraj Jha
# Script 4 : Accessing all the elements from Array
NAME[0]="Diksha"
NAME[1]="Oracle"
NAME[2]="Training"
NAME[3]="Centre"
NAME[4]="Madhubani"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
After this we need to save test3.sh and give the permission by using chmod 777 test3.sh command .
Note : We use ${array_name[*]} and ${array_name[@]} in script to access all the elements .
Output of both test2.sh and test3.sh in Putty Terminal is as below :
This is the way you can write basic shell scripts in Unix and if you are familiar with C programming it’s easy for you to write several shell scripts also in Unix we can write C program as well as database script to run in the Unix operating system. In next blog I will come up with some advance shell script and also some scripts related with database. Hope in this blog you are familiar with writing small scripts by using vi editor.
Please read this blog carefully and let me know if you have any questions. Also I will suggest you to please go through my previous blogs related with Unix commands. In next blog I will come up with more shell scripting program so that you can easily interact with other languages in Unix Platform.
Thanks.
its a very very useful information thanks for the info
ReplyDeleteAzure Data Engineering Online Training
Azure Data Engineering Live Online Training