首页 > > 详细

讲解ENGG1340、 C++编程语言辅导

Shell Script Module 2 p. 1/19
COMP2113 Programming Technologies
ENGG1340 Computer Programming II

Module 2: Shell Script
Estimated time to complete: 2 hours
Objectives
At the end of this chapter, you should be able to:
Understand what shell scripts are
Write Bash shell script with a sequence of shell commands
Table of Contents
1. Shell Script ................................................................................................................................................................ 2
1.1 What is Shell Script........................................................................................................................................... 2
2. Using Variables ......................................................................................................................................................... 5
2.1 Defining and Accessing Variables .................................................................................................................... 5
2.2 Read user input.................................................................................................................................................. 5
2.3 Quoting (single (' ') and double quote (" ")) ...................................................................................................... 6
2.4 Command Substitution ...................................................................................................................................... 7
2.5 Operations on Strings ........................................................................................................................................ 8
Get the length of a string ........................................................................................................................... 8
Substring ................................................................................................................................................... 8
Replace a part of a string ........................................................................................................................... 9
Treat the value in a variable as number .................................................................................................... 9
2.6 Get command line argument in shell script ..................................................................................................... 10
3. Flow of Control ....................................................................................................................................................... 11
3.1 If-else and condition........................................................................................................................................ 11
Example (check file existence) ............................................................................................................... 13
Example (check successful compilation) ................................................................................................ 13
Example (handle compilation error message) ......................................................................................... 14
3.2 For-loop ........................................................................................................................................................... 15
Example (Loop through files) ................................................................................................................. 15
4. Useful techniques in shell script ............................................................................................................................. 17
4.1 Hide unwanted command output in shell script .............................................................................................. 17
4.2 Output to standard error .................................................................................................................................. 18
5. Further reading ........................................................................................................................................................ 19
6. References ............................................................................................................................................................... 19
Shell Script Module 2 p. 2/19

1. Shell Script
1.1 What is Shell Script
Shell script (.sh) is a computer program designed to be run by the Linux shell. It is an interpreted language, but not a
compiled language. Unlike C++, we do not need to compile the shell into a binary executable format before executing
the program. The program written in a shell script is parsed and interpreted by the shell every time the program is
executed.
Interpreted languages allow us to modify the program more quickly by simply editing the script. However, the
programs are usually slower because parsing and interpretation are needed during execution time.
Consider we have a sequence of shell commands, we don’t want to re-type those commands whenever we want to
execute. We can save them in a file and call that a shell script.
Editing script file in Windows and importing to Linux may cause failure of execution because of the different end-
of-line (EOL) used in Windows. You should create and edit script files inside a Linux system (e.g. use vi editor in
SSH, gedit in X2Go). Otherwise, you should ensure the line ending option in your text editor in Windows
environment is set to UNIX format (LF) instead of Windows format (CRLF) if you import script files from
Windows.

Here, we create a hello.sh script.
$ vi hello.sh

#!/bin/bash
echo "Hello world!"
echo "Welcome to ENEG1340"
*echo is a shell command that prints the string or value of a variable.
Make the script executable by the user:
$ chmod u+x hello.sh

Here is a sample run of the script:
$ ./hello.sh
Hello world!
Welcome to ENEG1340


Shell Script Module 2 p. 3/19
All the scripts should start with #!. It indicates which program should be used to process the shell script. In this case,
it is the path to the bash program. From Module 1 we know that there are many different shells (e.g., C shell, Korn
shell, Bash shell). As we are using the Bash shell, we need to supply the path of the bash program so that the operating
system knows how to interpret the bash shell commands. The path is /bin/bash in this case.
You can use the command which -a to locate the correct path to the Bash shell. The flag -a stands for returning all
paths for the bash program.
$ which -a bash
/usr/local/bin/bash
/bin/bash

As you can see, the bash program maybe located in multiple locations. In the following, we will use the path
/bin/bash for our shell scripts.

Another Example (echo -n)
Use flag -n if you do not want to output the trailing newline. For example ex1_1.sh:
#!/bin/bash
echo -n "Hello world!"
echo "Welcome to ENGG1340!"
echo "bye"

Sample run:
$ ./ex1_1.sh
Hello world!Welcome to ENGG1340!
bye

Because there is a -n flag with the first echo command, there is no trailing newline in the first output. The second
output is then appended in the first line.

Shell Script Module 2 p. 4/19
One more Example (with C++ program)
Write a script to compile, run and display result of a C++ program
add.cpp
//add.cpp
#include
int main() {
int a;
int b;
std::cin >> a;
std::cin >> b;
std::cout << a + b;
}

input.txt
3 4

ex1_2.sh
#!/bin/bash

#Compile the code
g++ add.cpp -o add
#Run the code and display result
./add < input.txt > output.txt
cat output.txt

Run ex1_2.sh in shell:
$ ./ex1_2.sh
7


Shell Script Module 2 p. 5/19
2. Using Variables
There is only one variable type in shell scripts, which is string. Variable name is case sensitive. It can only contain
letters (a - z, A - Z), number (0-9) or the underscore character (_).
2.1 Defining and Accessing Variables
This is how we can define a variable pet with initial value “dog”.
pet="dog"
*No space is allowed before and after the = sign.
To access the value, use the dollar sign ($) with the variable name
#!/bin/bash

pet="dog"
echo $pet

The above script will give the following output.
dog

2.2 Read user input
The read command reads a string from the input and assigns it to the variable. For example, the following script read
a user input and finally print it out.
1
2
3
4
5
#!/bin/bash

echo "What is your name?"
read name
echo "Hello $name"
Line 4: [Setting value] We are setting the value of variable name (as the user input value). Therefore we do
not need a dollar sign $ before name.
Line 5: [Retrieving value] A dollar sign is required when retrieving the value of a variable. Therefore we
have echo "Hello, $name" outputting "Hello, Kit"
Sample run:
$ ./ex2_2.sh
What is your name?
Kit
Hello, Kit

Shell Script Module 2 p. 6/19
2.3 Quoting (single (') and double quote ("))
Quoting is very important on the command line and in shell script. There are 3 ways to specify a string value:
Unquoted, Single quote and Double quote.
Unquoted
We can specify a string value without any quoting, but this method only works if the string value consists of a single
word.
For example, the following will set the value of variable a as cat.
a=cat

However, it is wrong to create a string with a space in the value. With space, “Apple” is interpreted as a shell
command. Therefore, the shell below will return “command not found”.
a=Apple pie

Quoted
Any value between the pair of single/double quotation will be set as value of the string. However, single quotation
does not support variable substitution.
With double quotation, a string can handles three special characters instead of directly including them into the strings.
Symbol Description
$ Dollar sign: variable substitution.
\ Backslash: Escape special character
`` *Back quotes: Enclose bash commands

For example, we declare a variable (name) and print the variable using Single quote and Double quote.
#!/bin/bash

name="Apple"
echo 'Hello, $name'
echo "Hello, $name"
echo "\$name = $name"

Sample run:
$ ./ex2_3.sh
Hello, $name
Hello, Apple
$name = Apple

* Back quote button is the
button on the left of the button
“1” in most keyboards.
Shell Script Module 2 p. 7/19
In the example, the $name in the first echo command 'Hello, $name' is NOT substituted by any variable as we use
single quote.
For the second echo command "Hello, $name", $name is substituted by the variable, so the output is “Apple” not
“$name”.
For the third echo command "\$name = $name", \$ is interpreted as a single dollar sign character so the output is
“$” followed by “name”. Same as above, $name is substituted by the variable (name).

2.4 Command Substitution
With backquotes (`), we can store the output of a shell command in a variable for further processing.
#!/bin/bash

a="`cat file.txt`"
echo $a

b="`wc -l file.txt | cut -d\" \" -f1`"
echo "There are $b lines in file"

file.txt
Apple
Banana
Cherry


Sample run:
$ ./ex2_4.sh
Apple Banana Cherry
There are 3 lines in file

In the above example, the command wc -l file.txt returns the result “3 file.txt”, we then use the cut
command to get the number of lines.
The cut command cut -d" " -f1 is to separate the fields by the delimiter “ ” (i.e., a space) and return the first field
(i.e., the output is “3”). However, the double quote in the command will match with the double quote at the beginning
of the line (b="). To avoid this, we need to escape the double quote by adding a backslash (\) before it.


Shell Script Module 2 p. 8/19
2.5 Operations on Strings
Get the length of a string
Getting the length of a string is very useful in shell script. Given a variable a, ${#a} returns the number of characters
in the value of variable a.
#!/bin/bash

a="Apple"
echo ${#a}

Sample run:
$ ./ex2_5_1.sh
5
${#a} returns the length of the string stored in variable a, which is 5.

Substring
Given any string variable a, ${a:pos:len} returns the substring of a starting from position pos and has length len.
#!/bin/bash

a="Apple Pie"
echo ${a:6:3}

Sample run:
$ ./ex2_5_2.sh
Pie
Note: The index for the first character in a string is 0. Therefore ${a:6:3} return the substring “Pie”.

Index 0 1 2 3 4 5 6 7 8
Character A p p l e P i e

3 chars

Shell Script Module 2 p. 9/19
Replace a part of a string
Given any string variable a, ${a/from/to} returns the string formed by replacing the first occurrence of from with
to.
#!/bin/bash

a="Apple Pie"
from="Pie"
to="juice"
echo ${a/$from/$to}

Sample run:
$ ./ex2_5_3.sh
Apple juice
The first occurrence of “Pie” in “Apple pie” is replaced by “juice”, therefore the output becomes “Apple juice”.

Treat the value in a variable as number
It is less common to use shell scripts for mathematical calculations. Nevertheless, we can still perform mathematical
operations using the let command. Normal operators like +, -, *, / and % are supported.
Example:
#!/bin/bash

a=10

let "b = $a + 9"
echo $b

let "c = $a * $a"
echo $c

let "d = $a % 9"
echo $d

Sample run
$ ./ex2_5_4.sh
19
100
1


Shell Script Module 2 p. 10/19
2.6 Get command line argument in shell script
Command line arguments are labelled as $0, $1, ... $9. In particular, $0 is the name of the shell script. Command line
arguments after $9 must be labelled as ${10}, ${11}, ..., as otherwise $10 will be interpreted as $1 and character 0.
The number of command line variables is given by $#.
For example, we have ex2_6.sh with the following content.
#!/bin/bash

echo "There are $# arguments"
echo "$0"
echo "$1"
echo "$2"
echo "$3"
echo "$4"

Sample run (with arguments):
./ex2_6.sh we are the world
There are 4 arguments
./ex2_6.sh
we
are
the
world

Shell Script Module 2 p. 11/19
3. Flow of Control
3.1 If-else and condition
In this section, we will learn decision making in shell script.
The basic syntax of the if-statement is shown below.
if [ condition ]
then
perform some action

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!