0% found this document useful (0 votes)
26 views13 pages

CSCI4140 4 - Scripting

Uploaded by

andychchow326
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views13 pages

CSCI4140 4 - Scripting

Uploaded by

andychchow326
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Scripting & Interpreted Language

CSCI 4140: Open-Source Software


Project Development
Prof. Wei Meng

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?

- Reload • Call CGI programs, can be


written in any language
- Click hyperlinks

- Submit forms • Mainstream: PHP, Python,


非同步
Perl, Ruby, ASP, JSP, etc.
- Send asynchronous requests
• How to manage application and
• How to manage display? user data?

- Dynamic update w/ DOM


• Files
scripting doument Objectmodel

• How to manage client data? • Databases

- Sessions / Cookies Our Focus


2
Compiled Programming Language
• We start learning programming with compiled
languages like C, C++, Java, etc.
Chrome _
Ct +

• Compiler: A software that translates program in one


programming language into another programming
language Modify wmponent Compile forlogtmelfewhoun )

single
Time ω
nsuming

_

Executable: A file that causes a computer to perform


predefined tasks encoded by instructions

Compile Execute

Source Code Binary / Executable


3
Scripting Language
• So, what is a scripting language?

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

- A sequence of tasks / orders / commands

- Automate the execution of the tasks for a special run-time


environment
Javaisompiledlunguage ?
- Each task is processed by a program IntrputedbyJaramnchine
lOtimus sloerthnnc

• Scripting Language Is Python a compiled


programming language?
Mo
- Often interpreted (rather than compiled)
SometimesCanbeompiled Motbinary
- Sometimes referred as very high-level programming languages

- Also used to refer dynamic high-level general-purpose


languages, e.g., Perl and Python.

5
Scripting Language (Cont.)
• Environments that can be automated

- Web browsers (ECMAScript, e.g, JavaScript)

- OS (Bash)

- Software applications (Visual Basic)

-
Marco ? ?
Games (Lua)

• What is an interpreter?

- A computer program that executes instructions (commands)


written in a programming language.

- The instructions are not required to be compiled into


machine language instructions.
6
Bash
rIntuface
• Bash is a shell and scripting language for Unix-like
Operating Systems
What is a Unix shell?

#!/bin/bash

echo “Hello World!” A command-line interpreter or a


date shell providing Unix-like command-
A shell script line user interface

~ ❯❯❯ ./script.sh
/bin/echo Hello World!
Wed Jan 17 13:07:47 HKT 2018
~ ❯❯❯

/bin/bash
/bin/date

7
Bash (Cont.)
#!/bin/bash

echo “Hello World!” Invoking:


date ./script.sh
A shell script

~ ❯❯❯ ./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

• Starting a new bash shell.


• Reading and executing the content of
“script.sh” in a line-by-line manner.

8
Shell Programming
• Syntax

- Each shell has its own set of syntax

- E.g., “/bin/bash” and “/bin/tcsh” have different sets of 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

`…` is called sub-shell. It means calling a shell to execute the command


specified and return the result.
10

Shell Programming (Cont.)
• Strings

- Double-quote: Strings that you know

- Single-quote: String without variable substitution or


escape processing, i.e., verbatim mode

Which languages treat


#!/bin/bash quotes like shell scripts?
~ ❯❯❯ ./script.sh
a=“String”
String
echo “$a” - Perl, PHP, Ruby, etc.

#!/bin/bash
~ ❯❯❯ ./script.sh Which languages treat
a=“String”
$a both quotes equally?
echo ‘$a’

- JavaScript, Python, etc.

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

- Strong abstraction from the details of the computer

• General-purpose Programming Language

- Writing general-purpose software, i.e., in a wide


variety of application domains

• And Being Interpreted

- A powerful and flexible language for server-side


programming

13

You might also like