eHow launches Android app: Get the best of eHow on the go.

How To

How to Write a Script in Unix

Contributor
By DigitalOtter
eHow Contributing Writer
(0 Ratings)

In Unix, the command line gives you access to a wide range of tools, as well as the ability to combine them to solve any task at hand. However, there are times when it may be useful to turn repeatedly called commands or lengthy sequences of commands into scripts. User-created scripts can then be called quickly from the command line, reducing repetition and the complexity of many programming tasks.

Difficulty: Moderately Easy
Instructions
  1. Step 1

    Create and open a new file with your text editor of choice and name it test_script.sh.

  2. Step 2

    Create the shebang line. This is the beginning of any Unix script and sets what interpreter will be used for the script. A shebang line looks like this:
    #! /bin/sh
    Type this is in as the first line. The line of text following #! is the path to the interpreter to be used. In this case it is the Bourne shell, which is found on all Unix systems.

  3. Step 3

    Add commands to the script. Commands and programming logic are added after the shebang line. In this script we'll write a message to the terminal. Type this into your script:
    echo "Hello test script!"

  4. Step 4

    Save the changes you've made to the file. Before closing it, make sure you have included the shebang line and that the path to the interpreter is correct for your system.

  5. Step 5

    Make the script executable. Move to the command line if you aren't already at it. Navigate to the directory you created the script in and enter the following to make your script executable:
    chmod 755 test_script.sh

  6. Step 6

    Run the script from the command line by typing this:
    ./test_script.sh
    You should see the text "Hello test script!" printed out to the command line.

Tips & Warnings
  • Note that any valid system command after the shebang line will be executed by the interpreter. Shell scripting allows the use of constructs typical to all programming languages. The documentation for each shell with show how to implement variables, conditionals and looping.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics