Writing an UNIX shell script: A simple Bourne-Shell script
71In 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
PrintShare it! — Rate it: up down flag this hub
Recent pages of rancidTaste
- Fresh and unique contents for Search Engine Optimization: The secret talks of SEO and generating site contents
Millions of websites with billions of webpages are available in the cyberspace. - 3 weeks ago
- Free malware protection with Portable Anti Trojan Elite: The best malware blocker and malware remover for your compter
Recently malware is spreading very rapidly. - 3 weeks ago
- rundll32.exe - Application Error â The memory could not be "read". Click on OK to terminate the program.
Windows operating system is corrupted due to several virus infections. - 3 weeks ago
- PPV or PPC?: "Pay Per View or Pay Per Click?" - Which One is Better?
Pay per view (PPV) and pay per click (PPC) are two types of marketing advertising solutions which is used by the advertisers to reach targeted group of visitors. - 3 weeks ago
- Make Good Money with Good Articles and Get Quick Cash
Before going to the main content, I want to ask you tow question? - 4 weeks ago
- Hot Secrets Behind Good Articles: How and Why Good Writers Always Write Good Articles?
Before writing to Hubpages, I have no idea about good articles. - 4 weeks ago
- Backlinks And Backlinking: Search Engine Optimization and the Importance of Backlinks And Backlinking
If you do a small research on search engine optimization or high search engine ranking, then you must come across the term backlinks or backlinking. - 4 weeks ago
- Validate with a Validation List: Create you own Validation List in Excel
Excel enables you to restrict the values a user can enter in a cell. - 4 weeks ago









