CSCI4140 4 - Scripting
CSCI4140 4 - Scripting
https://course.cse.cuhk.edu.hk/~csci4140/
Two Sides of Web Programming
Client Side Ajax Server Side
• How to create requests? • How to generate responses?
Compile Execute
Follow what I
say.
/bin/bash
Command
/bin/ls
A Script
/bin/echo
A pool of programs
“A script is what you give the actors, but a program is what you give the
audience.” — Larry Wall (Creator of the Perl programming language)
4
Scripting Language (Cont.)
• A script
5
Scripting Language (Cont.)
• Environments that can be automated
- OS (Bash)
-
Marco ? ?
Games (Lua)
• What is an interpreter?
#!/bin/bash
~ ❯❯❯ ./script.sh
/bin/echo Hello World!
Wed Jan 17 13:07:47 HKT 2018
~ ❯❯❯
/bin/bash
/bin/date
7
Bash (Cont.)
#!/bin/bash
~ ❯❯❯ ./script.sh
Invoking:
yrimiawomepue
Hello World!
/bin/bash script.sh
Wed Jan 17 13:07:47 HKT 2018
~ ❯❯❯ /bin/bash script.sh
Hello World!
Wed Jan 17 13:07:57 HKT 2018
8
Shell Programming
• Syntax
- Variable
- $var means getting the value that is stored in the variable var
#!/bin/bash
a=“Hello World!” ~ ❯❯❯ ./script.sh
b=$a
@
$$ G
Message is Hello World!
echo “Message is $b”
#!/bin/bash
~ ❯❯❯ ./script.sh
a=“Hello World!”
Message is Curly braces
echo “Message is $a_ext”
define the start
#!/bin/bash and the end of
~ ❯❯❯ ./script.sh
a=“Hello World!” variable name.
echo “Message is ${a}_ext” Message is Hello World!_ext
9
一
一
Shell Programming (Cont.)
• Variable type
You need an external program to do
the addition, e.g., using “expr”.
- Only one type: STRING!
#!/bin/bash
~ ❯❯❯ ./script.sh
a=“2”+“3”
echo $a 2+3
#!/bin/bash
cith
~ ❯❯❯ ./script.sh
a=“2” + “3” ← Sepamted
echo $a
spaaea ./script.sh: line 2: +: command not found
causiy
#!/bin/bash
~ ❯❯❯ ./script.sh
a=`expr 2 + 3`
echo $a 5
#!/bin/bash
~ ❯❯❯ ./script.sh
a=`expr 2+3`
echo $a 2+3
#!/bin/bash
~ ❯❯❯ ./script.sh Which languages treat
a=“String”
$a both quotes equally?
echo ‘$a’
11
Bash CGI
#!/bin/bash
Not powerful enough for
echo "Content-type: text/html" writing CGI programs.
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Hello World</title>'
echo '</head>'
echo '<body>'
echo 'Hello World'
echo '</body>'
echo '</html>'
https://csci4140.cse.cuhk.edu.hk/cgi-bin/hello.sh
12
Interpreted High-level General-
purpose Programming Language
• High-level Programming Language
13