music
OSdata.com: programming text book 

OSdata.com

scripts

summary

    This subchapter looks at scripts.

    Scripts are the power tools for system administrators. Scripts allow you to automate tasks.

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

scripts

    This subchapter looks at scripts.

    Scripts are the power tools for system administrators. Scripts allow you to automate tasks.

shell initialization files

    When you start Terminal (or, in some cases, run login) the shell will run two initialization scripts:

        /etc/profile
        .profile

    The /etc/profile initialization script is maintained by the system administrator and is the first initialization script run for all users.

    Each user has their own .profile script in their home directory.

    The .profile includes three basic operations (set terminal type, set path to commands, and set path to man pages) and any customization created by the individual user.

.profile

    The .profile initialization script sets the terminal type:

        For example: TERM=vt100

    The .profile initialization script sets the path to the directory or directories that contains the commands and utilities.

        For example: PATH=/bin:/usr/bin

    Notice the colon character (:) is used as a separator between directories, allowing commands and utilities to be organized in many directories.

    The .profile initialization script sets the path to the directory or directories containing the manual (man) pages.

        For example: MANPATH=/usr/man:/usr/share/man

    Before the initialization scripts are run, the shell is considered to be an uninitialized shell.

interactive and noninteractive

    When you directly enter a command or commands and the shell performs the commands for you, that is the interactive mode.

    You can also have the shell run scripts or commands in a noninteractive or batch mode. This is great for a system administrator automating a server or other large scale UNIX/Linux/Mac OS X system.

    When an non-interactive shell (or subshell) finishes running a script, it exits.

making a script executable

    Scripts are just text files with lists of instructions.

    To make a text file into an executable script that you can run, simply type chmod a+x filename.

    $ chmod a+x filename
    $

    Also, add the magic line #!/bin/sh as the first line in the text file. This special magic line tells the shell which program to use to run the script (in this case, the basic shell program).

Shell Files and Interpreter Invocation
File Extensions
from Google Shell Style Guide

    Executables should have no extension (strongly preferred) or a .sh extension. Libraries must have a .sh extension and should not be executable.

    It is not necessary to know what language a program is written in when executing it and shell doesn’t require an extension so we prefer not to use one for executables.

    However, for libraries it’s important to know what language it is and sometimes there’s a need to have similar libraries in different languages. This allows library files with identical purposes but different languages to be identically named except for the language-specific suffix.

scripting languages

    Some of the scripting languages available on Mac OS X are: sh, bash, perl, PHP, python, and ruby.

    Examples of running scripts in these languages:

    $ sh scriptfilename
    $ bash scriptfilename
    $ perl scriptfilename
    $ php scriptfilename
    $ python scriptfilename
    $ ruby scriptfilename
    $

shell script example

    Create a directory (folder) for your personal scripts called scripts:

    $ mkdir scripts
    $

    Create a shell script called script.sh and save it in the new scripts directory (folder):

    #!/bin/sh
    echo "Hello World!"

    Use the chmod command to make the new file an executable script:

    $ chmod u+x /scripts/script.sh
    $

    Add the scripts directory to your command path:

    $ export PATH="$PATH:~/scripts"
    $

    Run your new script:

    $ script.sh
    Hello World!
    $

    You can run your script directly in a specific shell:

    $ rbash script.sh

    $ sh script.sh

    $ bash -x script.sh

php script example

    This example assumes that you have created the scripts directory.

    Create a php script called script.php and save it in the new scripts directory (folder):

    <?php
    echo "Hello World!"
    ?>

    Notice that we skip the chmod step.

    Run your new script by running the php program with your script as the file to execute:

    $ php ~/scripts/script.php
    Hello World!
    $

    Note that the shell does not render HTML, so if you run a web scirpt, you will see raw HTML, CSS, and JavaScript as plain text in your terminal window.

    You can run perl, ruby, and python scripts in the same manner.

    There is more complete information in the subchapter on php and shell.

When to use Shell
from Google Shell Style Guide

    Shell should only be used for small utilities or simple wrapper scripts.

    While shell scripting isn’t a development language, it is used for writing various utility scripts throughout Google. This style guide is more a recognition of its use rather than a suggestion that it be used for widespread deployment.

    Some guidelines:


comments, suggestions, corrections, criticisms

please contact us

your name:
email address:
phone number:
message:

free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2013, 2014 Milo

    Created: August 18, 2013

    Last Updated: January 2, 2014


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

previous page next page
previous page next page