A First Script

Shell script are powerful tools for DevOps

#!/bin/sh
# This is a comment!
echo Hello World        # This is a comment, too!

The first line tells Unix that the file is to be executed by /bin/sh. The # symbol still marks a comment; the # and anything following it is ignored by the shell. The file starts with #!is a special directive which Unix treats specially #!/usr/bin/python to tell your interactive shell that the program which follows should be executed by python.

Now run chmod 755 first.sh to make the text file executable, and run ./first.sh. Your screen should then look like this:

$ chmod 755 first.sh
$ ./first.sh
Hello World
$

Variables - Part I

variables - a symbolic name for a chunk of memory to which we can assign values, read and manipulate its contents. Note: There must be no spaces around the "=" sign: VAR=value works; VAR = value doesn't work. In the first case, the shell sees the "=" symbol and treats the command as a variable assignment. In the second case, the shell assumes that VAR must be the name of a command and tries to execute it.

#!/bin/sh
MESSAGE="Hello World"
echo $MESSAGE

variables may store strings, integers, real numbers. In truth, these are all stored as strings We can interactively set variable names using the read command. shell-builtin command read which reads a line from standard input into the variable supplied

#!/bin/sh
echo What is your name?
read NAME
echo "Hello $NAME - hope you're well.

There is a command called export which has a fundamental effect on the scope of variables.

Now run the script:

Run again.

VAR hasn't been set to any value, so it's blank.

We need to export the variable for it to be inherited by another program.

This is changing the value of VAR

Once the shell script exits, its environment is destroyed. But VAR keeps its value of Welcome within your interactive shell. In order to receive environment changes back from the script, we can source a script via the "." (dot) command:

The change has now made it out into our shell again! This is how your .profile or .bash_profile file works.

we enclose the variable itself in curly { } brackets:

Wildcards:

Think first how you would copy all the files from /folder/src into /folder/apps. All the .txt files? All the .html files? Hopefully you will have come up with:

Escape Characters:

The use of double quotes (") characters affect how spaces and TAB characters are treated

So how do we display: Hello "World" ?

Most characters (*, ', etc) are not interpreted (ie, they are taken literally) by means of placing them in double quotes (" ").

In the first example, * is expanded to mean all files in the current directory. In the second example, *txt means all files ending in txt. In the third, we put the * in double quotes, and it is interpreted literally. In the fourth example, the same applies, but we have appended txt to the string. However, ", $, `, and \ are still interpreted by the shell, even when they're in double quotes. The backslash \ character is used to mark these special characters so that they are not interpreted by the shell.

Last updated