Skip to content
Kenny Yu edited this page Sep 25, 2013 · 9 revisions

What is a shell script?

So far, all the commands you've been writing were only executed in the terminal. Often, however, you want to save the commands so that you can use them later. We can do this by writing a shell script! A shell script is essentially a sequence of commands that you would normally just type into the command line, but saved into a file. Let's take a look at our first shell script!

First, make sure you already have the source code for this bootcamp:

git clone git://github.com/hcs/bootcamp-setup.git
cd bootcamp-setup

There should be a scripts directory that will contain the examples from this part of tutorial. You'll be able to run all the examples from that directory.

cd scripts

Take a look at our first script hello.sh (type cat hello.sh to dump the contents of the file, and ./hello.sh to run the script).

#!/bin/bash

echo "Hello bootcampers!"

Notice the #!/bin/bash at the very top. All shell scripts must start with this!!!. Also, if you run the command ls -l hello.sh, you'll see this:

-rwxr-xr-x  1 kennyyu  staff  39 Sep 24 22:59 hello.sh

Notice the x's. All shell scripts must have executable permissions!!!. Remember that to add executable permissions to a file, use chmod +x FILE.

Summary

A shell script:

  • MUST start with #!/bin/bash as the very first line
  • MUST have executable permissions. Use chmod +x FILE to add executable permissions to a file
  • Can contain any sequence of commands that you can enter normally at the command line.
  • Each line must be its own complete command that should run in a terminal. A complete command can be chain of piped commands, or a sequence of commands separated by ;.
  • By convention, shell scripts have the .sh extension, but this is not necessary.

Reading Input

Let's move onto our next script greetings.sh where we will read input from the user:

#!/bin/bash

echo "What is your name?"
read name
echo "Hi $name!"

When we run it (./greetings.sh), the read command prompts us for input, and it will save the input in the variable called name.

What is your name?
Kenny
Hi Kenny!

Note that you can use pipes and input/output redirection like any normal command:

echo "Kenny" | ./greetings.sh > hi.txt
cat hi.txt

This will send "Kenny" as input to the greetings.sh script, and all output of the script will be saved in the hi.txt file.

Loops

If we want to repeat an operation, we can use a loop. If you've programmed in C, C++, Java, Python, or any languages similar to these, you've probably seen the while and for keywords before. The terminal has both of these as well! We'll only discuss while for now. Let's take a look at the script loop.sh:

#!/bin/bash

while read line
do
    echo "hi $line!"
done

If we run it (./loop.sh), it will continuously prompt us for input until we press CTRL+D to signal the end of input:

Kenny
hi Kenny!
Karen
hi Karen!
Saagar
hi Saagar!

Everything between the do and done keywords is every time the while loop reads new input.

Command Line Arguments

#!/bin/bash explain the scraper loops variables mail merge example semicolon