Hardware Reference
In-Depth Information
Step 2: Start with an Empty Text File
Scripts are plain text files, so you should begin by creating one in a text editor. You can make
a shell script in TextEdit, BBEdit, or even Word, but that requires extra steps. So I suggest
using a simple command-line text editor called nano .
For the purpose of demonstration, we'll name the script test.sh . (The .sh extension isn't
mandatory, but it can help you keep track of which files are scripts.) Before you create this
file, I suggest typing cd (change directory) followed by Return to ensure that you're in your
home directory. (You can put scripts anywhere you want, but for now, this is a convenient
location.)
That done, type nano test.sh and press Return.
The nano text editor opens with a blank file.
Step 3: Type In the Script
A script can be anything from a single one-word command to thousands of lines of complex
logic. Let's start with a very simple, five-line script. Type this:
#!/bin/bash
echo "Hello! The current date and time is:"
date
echo "And the current directory is:"
pwd
The first line tells the script which shell program to use (in this case, bash ). The two echo
commands simply put text on the screen. The date command displays the date (surprise!),
and the pwd (print working directory) command displays your current directory. So, this
script displays four lines of text, two of which are static (the echo lines) and two of which are
variable.
Step 4: Close and Save the File
To save the file, press Control-O and press Return to confirm the file name. Then press
Control-X to exit nano .
Step 5: Enable Execute Permission
The only slightly tricky thing about running scripts—and the step people forget most
often—is adding execute (run) permission to the file. To do this, enter chmod u+x
test.sh .
Search WWH ::




Custom Search