1 / 35

Programming Languages

Programming Languages. BASH Jing Chan. Introduction. It is acronym for B ourne- a gain sh ell. Created in 1987 by Brian Fox Shell scripting language written for the GNU Project. Where to run Bash. Default shell of most of Linux systems, and also Mac OS X

ckoehler
Download Presentation

Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 HELLO=Hello function hello { local HELLO=World 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: STRING=“ABCDEFGHIGK\ 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 can’t be used between single quotes, even when preceded by a backslash.

  12. Example* value=5 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 value=5 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 & i=$[$i+1] 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); n=0 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 i=1; i<= 100; i++ ) { for (int j=1; j<=10 ; j++ ) { for (int k=1; k<=100; k++) { n = n+i+j+k; } } } endtime = time(NULL); printf ("%d", n); difference = endtime - begintime; printf ("It took %d seconds", difference); return 0; }

  23. Result of the comparison

  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_BADARGS=65 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); tr a-z A-Z < "$1" > "$2" ENDTIME=$(date +%s) DIFF=$(( $ENDTIME - $BEGINTIME )) echo "It took $DIFF seconds" exit 0

  26. test2.cpp int main(int argc, char *argv[]) { if (argv[1]=="" || argv[2]=="") { printf ("Usage: %s sourcefile destfile", argv[0]); exit(1); } printf("C++ Program Starts...\n"); time_t begintime; time_t endtime; int difference; string line; ifstream file_op; file_op.open(argv[1],ios::in); ofstream file_out; file_out.open(argv[2], ios::out); while(!file_op.eof()) { getline(file_op, line); strupr((char *) line.c_str()); file_out<<line; } file_op.close(); file_out.close(); endtime = time(NULL); difference = endtime - begintime; printf ("It took %d seconds", difference); return 0; }

  27. Result of the 2nd comparison(coding time is not included)

  28. Can we make it faster?

  29. #!/usr/bin/bash # Changes a file to all uppercase E_BADARGS=65 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); tr a-z A-Z < "$1" > "$2" ENDTIME=$(date +%s) DIFF=$(( $ENDTIME - $BEGINTIME )) echo "It took $DIFF seconds" exit 0 cat test.txt | tr a-z A-Z > testA.out

  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-Intro-HOWTO.html#ss2.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?

More Related