Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - The Shell and UNIX
MISCELLANEOUS

The Shell and UNIX
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2009-04-08

    Table of Contents:
  • The Shell and UNIX
  • Shell Programs
  • Shell Commands
  • Command execution

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    The Shell and UNIX - Shell Programs


    (Page 2 of 4 )

     

    A Shell program file (also known as shell script) is a simple file containing text. The shell will interpret this line by line. Every line will be a different command, just as you would have entered one command after another on the command line of a terminal. The shell will not go ahead until one of the lines is completed, if you do not specifically tell it to do so.  

    The syntax of a command line, as we learned in a previous article titled The Terminal in UNIX, obeys the following sequence: 

    [Command name] [Options] [Arguments] 

    The comment character is the # (hash mark). Nothing after this character in the current line will be interpreted. For example:

     

    #Here we will sort the file alfa.txt

    sort alfa.txt

     

    The first line of a shell script is special. Here we can specify with what interpreter we would like to execute the script. This happens by starting the first line with a comment and an exclamation mark followed by the interpreter we would like to use. For example, let us write a simple script that will display the output "Live with Passion." We will create a file with the name LWP.sh and enter within it the following lines: 

    #!/bin/bash

    echo Live with Passion

    There should be no space between the exclamation mark and the full path to the interpreter. We can run the script in two ways. First, we can just give it to the bash as argument.  

    bash LWP.sh

    When we do, we get this result: 

    Live With Passion

    Another option is to set the execute parameter to on for using chmod. You can read about this in the article titled Rights Management System in UNIX if you missed it. Now we will refer to the file as follows: 

    /LWP.sh

    The output should be the same. The latter example will figure out what interpreter to use with the help of the first line. Therefore, the execution is in fact the creation of a new shell that will get as input the new file. This will link its output to the output of the current shell (terminal), and as you can see, the results. If for some reason the first line is missing, the shell will try to use the default interpreter that lies in the environment variable $SHELL. 

    More Miscellaneous Articles
    More By Gabor Bernat

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap