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 doesnt require an extension so we prefer not to use one for executables.
However, for libraries its important to know what language it is and sometimes theres 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 isnt 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:
- If youre mostly calling other utilities and are doing relatively little data manipulation, shell is an acceptable choice for the task.
- If performance matters, use something other than shell.
- If you find you need to use arrays for anything more than assignment of
${PIPESTATUS}
, you should use Python.
- If you are writing a script that is more than 100 lines long, you should probably be writing it in Python instead. Bear in mind that scripts grow. Rewrite your script in another language early to avoid a time-consuming rewrite at a later date.
comments, suggestions, corrections, criticisms
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).
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.
Names and logos of various OSs are trademarks of their respective owners.