- Computers & Software»
- Operating Systems
Writing an UNIX shell script: A simple Bourne-Shell script
In UNIX, you can type commands and execute them in the shell one by one. For single or one type command very convenient to use commands and execute them in the shell. But do you know that you can easily write several commands and execute them sequentially ever time when you need them to execute. All that you have to write those commands in a file and name it and then run that. It's simple like a-b-c. This page tells you the way to write UNIX script in Bourne shell and so I want to say The Beginner Guide to Write an UNIX script.
What is a UNIX shell?
A shell is a command line interpreter. It takes commands and executes them.
Writing UNIX shell script
It's very easy to write shell script. To write any shell script, you need to follow the following steps:
Step 1: Creating a file for your UNIX script
Create a new file using any text editor and name it as welcome.sh.
Step 2: Creating a shebang line for your UNIX script
Each UNIX script begins with a line like #!/bin/sh. " #!/bin/sh" calls the shebang line. This line is the beginning of any Bourne shell script. Without it, UNIX does not recognize that the file is an UNIX script.
Step 3: Writing the command in your UNIX script
After the shebang line, you can write / add commands to your script. You can write as much command as you need. All the commands are executed sequentially.
Step 4: Saving the script
After writing the script, you need to save the script. Remember, you must have to save the script.
Step 5: Making the script execuatable
You need to make the script executable and so navigate to the
directory where your script is located from the command prompt and then
make it executable. Without make it executable your script won't
work. To make it executable, you have to change the script mode by
using chmod command. For example, for welcome.sh the command will look
like chmod 755 welcome.sh.
Step 6: Running the script
To run your script, you simply have to
type the script name end using ./ and then execute it by pressing Enter
key. Your script will be executed instantly.
Example to write a Bourne-shell script
You will find and learn an example to write a script in Unix Bourne shell. For example, we want to write a script which can present the following information when we execute that:
- Show a welcome message.
- Show the current date and time.
- Show the user's present working directory.
Actually, we are learning how to write a script. So, we now have to write a script which can execute the above information.
Write your desired script
Step 1: Firstly, create a file and name it as hello.sh using any text editor like vi editor.
Step 2. Write the shebang line #!/bin/sh in "hello.sh" file.
Step 3: Write the commands into the file. In the above discussed example, we have to present 3 types of information (a) Welcome message (b) current date and time and (c) current working directory. So, the UNIX script will looks like the following:
#!/bin/sh echo "Hello world" echo "Today's date and time is: $date" echo "Present working directory is: $pwd"
Step 4: Save the file hello.sh and close it.
Step 5: Change the mode of hello.sh file by using the command chmod 755 hello.sh.
Step 6: Open the terminal window and simply write the file name hello and hit Enter key. You will see the script is executed and presents all the desired information.
In this way, you can easily create and run your first UNIX Bourne-shell script. Here, I just explained the hello script which proved the information like welcome message, current date and time and present working directory. You can easily learn the ways to write and run any script if you understand this example clearly.
Hope, you can create and run script easily. If you don't understand in any part of this tutorial please tell me in the comment section. I will try to help you.
@Written by rancidTaste