-
Notifications
You must be signed in to change notification settings - Fork 1
Shell scripts
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-unix.git
cd bootcamp-unix
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
.
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.
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.
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 into the variable line
.
We can pass arguments at the command line to shell scripts! Take a look at the script args.sh
:
#!/bin/bash
# Comments must start with the '#' character
# Command line arguments are found in $0, $1, $2, ...
# Note that $0 is always the name of the current program.
# '-z $1' will return true if the first argument is NOT set.
# '-o' will perform a logical OR
if [ -z $1 -o -z $2 ]
then
echo "usage: $0 arg1 arg2"
exit
fi
echo "hello $1 and $2!"
If we run it without arguments (./args.sh
), it will print the following message:
usage: ./args.sh arg1 arg2
If we provide the arguments (e.g. ./args.sh kenny saagar
), it will run the program:
hello kenny and saagar!
Go back to the main page.