Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Languages

Description:

Comparison (.cpp and .sh run in both cygwin and linux) Why use bash. Why not use bash ... Run in Cygwin Bash shell and SSH. Guess which one is the most ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 36
Provided by: jin129
Learn more at: https://grothoff.org
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages


1
Programming Languages
  • BASH
  • Jing Chan

2
Introduction
  • It is acronym for Bourne-again shell.
  • Created in 1987 by Brian Fox
  • Shell scripting language written for the GNU
    Project

3
Where to run Bash
  • Default shell of most of Linux systems, and also
    Mac OS X
  • Unix machine or Windows machine
  • Unix-like operating system such as Cygwin

4
Outline
  • Variables
  • Tokens
  • Basic syntax and examples
  • Comparison (.cpp and .sh run in both cygwin and
    linux)
  • Why use bash
  • Why not use bash
  • Other considerations
  • Reference
  • Q A

5
HelloWorld
  • Java
  • class HelloWorld
  • public static void main (String args)
  • System.out.println("Hello World!")
  • Bash
  • !/bin/bash
  • echo Hello World

6
Variables
  • A variable in bash can contain a number, a
    character, a string of characters
  • Not necessary to declare a variable
  • Assign a value to its reference
  • Example
  • STRING"Hello World"
  • echo STRING

7
Variables (cont)
  • Local variables
  • Create local variables by using keyword local
  • Example
  • !/bin/bash
  • HELLOHello
  • function hello local HELLOWorld
  • echo HELLO echo HELLO

8
Tokens in BASH
  • Backslash \
  • Single quote
  • Double quote
  • Pipe
  • Backtick

9
Token (Backslash)
  • creating directory names, file names and etc.
  • Example
  • mkdir foo\ bar
  • //creating a directory called foo bar
  • rm rf foo\ bar
  • //remove

10
Backslash (cont)
  • Escape Sequence break down a long string
  • Example
  • STRINGABCDEFGHIGK\
  • LMNOPQRSTUV
  • //STRING equals to both two lines

11
Tokens (Single Quote)
  • Enclosing characters in single quotes preserves
    the literal value of each character within the
    quotes.
  • A single quote cant be used between single
    quotes, even when preceded by a backslash.

12
Example
  • value5
  • echo I have value dollars
  • Output
  • I have value dollars
  • value will not be interpreted

13
Token (Double Quote)
  • Quoting characters
  • Groping the space separated works together
  • Example cat conference agenda.txt"
  • Interpreted the string
  • Example
  • value5
  • echo I have value dollars
  • I have 5 dollars //output

14
Quotes
  • What if echo without any quotes
  • echo '"This text is surrounded by double
    quotes."'

15
Token (Pipe)
  • Allows you use the output of a program as the
    input of another one
  • -program1 program2
  • Examples
  • ls grep test
  • ls -l sed -e "s/aeio/u/g"

16
Backtick
  • It is quotation of commands
  • What is the difference between
  • echo date
  • and
  • echo date

17
For Loop
  • for i in seq 1 10
  • do
  • echo i
  • done
  • //print the sequence from 1 to 10
  • What if we take out the ?

18
While Loop
  • Structure
  • While CONTRO-COMMAND
  • do
  • CONSEQUENT-COMMANDS
  • done
  • CONTRO-COMMAND can be any commands which exit
    with a success or failure status.
  • CONSEQUENT-COMMANDS can be any programs, scripts
    or shell construct.

19
While Loop
  • i"0"
  • while i -lt 10
  • do
  • xterm
  • ii1
  • done

20
Comparison
  • Created 4 files
  • 2 .sh and 2 .cpp
  • -test.sh and test.cpp
  • -test2.sh and test2.cpp
  • Run in Cygwin Bash shell and SSH
  • Guess which one is the most expensive to run

21
test.sh
  • !/usr/bin/bash
  • echo Bash Program Begins...
  • BEGINTIME(date s)
  • n0
  • for i in seq 1 100
  • do
  • for j in seq 1 10
  • do
  • for k in seq 1 100
  • do
  • n(( n i j k ))
  • done
  • done
  • done
  • echo n
  • ENDTIME(date s)
  • DIFF(( ENDTIME - BEGINTIME ))
  • echo "It took DIFF seconds"

22
test.cpp
  • int main(int argc, char argv)
  • printf("C Program Starts...\n")
  • time_t begintime
  • time_t endtime
  • int difference
  • int n 0
  • begintime time(NULL)
  • for (int i1 ilt 100 i )
  • for (int j1 jlt10 j )
  • for (int k1 klt100 k)
  • n nijk

23
Result of the comparison
Windows Linux
test.sh gt 30 seconds 3 seconds
test.cpp 0 seconds 0 seconds
24
Another example
  • Basically, convert all the lower cases characters
    in a file into upper cases, and the output will
    be in a output file
  • test2.sh and test2.cpp
  • Run in Cygwin and SSH

25
test2.sh
  • !/usr/bin/bash
  • Changes a file to all uppercase.
  • E_BADARGS65
  • if -z "1" Standard check for command line
    arg.
  • then
  • echo "Usage basename 0 sourcefile destfile"
  • exit E_BADARGS
  • fi
  • if -z "2"
  • then
  • echo "Usage basename 0 sourcefile destfile"
  • exit E_BADARGS
  • fi
  • echo Bash Program Begins...

26
test2.cpp
  • int main(int argc, char argv)
  • if (argv1"" argv2"")
  • printf ("Usage s sourcefile destfile",
    argv0)
  • exit(1)
  • printf("C Program Starts...\n")
  • time_t begintime
  • time_t endtime
  • int difference
  • string line
  • ifstream file_op
  • file_op.open(argv1,iosin)

27
Result of the 2nd comparison(coding time is not
included)
Windows Linux
test.sh 0 seconds 3 seconds
test.cpp 0 seconds 0 seconds
28
  • Can we make it faster?

29
  • cat test.txt tr a-z A-Z gt testA.out
  • !/usr/bin/bash
  • Changes a file to all uppercase
  • E_BADARGS65
  • if -z "1" Standard check for command line
    arg.
  • then
  • echo "Usage basename 0 sourcefile destfile"
  • exit E_BADARGS
  • fi
  • if -z "2"
  • then
  • echo "Usage basename 0 sourcefile destfile"
  • exit E_BADARGS
  • fi
  • echo Bash Program Begins...
  • BEGINTIME(date s)

30
Why use bash
  • Any idea?
  • Because bash is already running, any additional
    bash scripts that you run are inherently
    memory-efficient because they share memory with
    any already-running bash processes.
  • System administration is easier to use the
    existing tools available in bash than to write a
    new program every time.

31
When not to use bash
  • While there are many small tasks to implement
  • So just put all the tasks in one program instead
    of using bash

32
  • If you have a company that produces software,
    will you use bash?
  • Why/ Why not?

33
Considerations of using Bash
  • Security issue
  • -Is the source code readable?

34
References
  • http//en.wikipedia.org/wiki/Bash
  • http//www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Int
    ro-HOWTO.htmlss2.1
  • https//wiki.ubuntu.com/Spec/EnhancedBash
  • http//tiswww.case.edu/php/chet/bash/bash.html
  • http//www.gnu.org/software/bash/bash.html
  • http//www.dsj.net/compedge/shellbasics1.html

35
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com