Writing a Shell Script
A shell script is just a file with a sequence of UNIX commands in it.
.sh extension for these to get proper syntax coloring.
Here is an example. The
echo command just spits what is
handed it to
stdout. Create this file.
#!/bin/bash echo Hello, World!
Now make the file executable and it will run your UNIX command. Do that as follows.
$ chmod +x hello.sh $ ./hello.sh Hello, World! $
The first line is called the shebang line.
It basically says, "this is a shell script." After it,
just insert the UNIX commmands you want to have run. You can
put shell scripts in your
~/bin directory and run
them from anywhere in the file system.
Here is another example. It gives you date, time and a calender.
It shows the environment variables
HOSTNAME being accessed.
#!/bin/bash echo Hello, $LOGNAME! echo You are logged into the host $HOSTNAME echo The current date and time are date echo Here is a calendar for this month cal
$ ./time.sh Hello, morrison! You are logged into the host fluorine.ncssm.edu The current date and time are Fri Feb 24 10:47:16 EST 2017 Here is a calendar for this month February 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
You are going to write a shell script that performs various manipulations
with the file
scrabble.txt which you can download in the
There is also a skeleton file provided,
skeletonA.sh. All of
the problems are present in the skeleton file. Note that the pound
sign (#) is a one-line comment. The shell will ignore all characters on
a line after a #. In vi, comments turn blue.