Programming Languages BASH Jing Chan Introduction l It

Programming Languages BASH Jing Chan

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

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

Outline l l l l l 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
![*Hello. World Java: class Hello. World { public static void main (String[] args) { *Hello. World Java: class Hello. World { public static void main (String[] args) {](http://slidetodoc.com/presentation_image_h2/8bf8cd30db033846c08a216e29888232/image-5.jpg)
*Hello. World Java: class Hello. World { public static void main (String[] args) { System. out. println("Hello World!") ; } } Bash: #!/bin/bash echo “Hello World”

Variables l. A variable in bash can contain a number, a character, a string of characters l Not necessary to declare a variable l Assign a value to its reference l *Example: STRING="Hello World" echo $STRING

l Local Variables (cont) variables l Create local variables by using keyword local l Example #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO

Tokens in BASH l Backslash l Single quote ‘ l Double quote “ l Pipe | l Backtick `

Token (Backslash) l creating directory names, file names and etc. *Example: $ mkdir foo bar //creating a directory called “foo bar” $ rm –rf foo bar //remove

Backslash (cont’) l Escape string Sequence; break down a long Example: STRING=“ABCDEFGHIGK LMNOPQRSTUV” //STRING equals to both two lines

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

Example* value=5 echo ‘I have $value dollars’ Output: I have $value dollars $value will not be interpreted

Token (Double Quote) Quoting characters l Groping the space separated works together Example: cat “conference agenda. txt" l Interpreted the string Example value=5 echo “I have $value dollars” I have 5 dollars //output l

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

*Token (Pipe) l Allows you use the output of a program as the input of another one -program 1 | program 2 Examples: $ ls | grep test $ls -l | sed -e "s/[aeio]/u/g"

*Backtick ` l It is quotation of commands l What is the difference between $echo date and $echo `date`

*For Loop for i in `seq 1 10`; do echo $i done //print the sequence from 1 to 10 l What if we take out the ``?

While Loop Structure: While CONTRO-COMMAND; do CONSEQUENT-COMMANDS; done l CONTRO-COMMAND can be any commands which exit with a success or failure status. l CONSEQUENT-COMMANDS can be any programs, scripts or shell construct.
![While Loop i="0" while [ $i -lt 10 ] do xterm & i=$[$i+1] done While Loop i="0" while [ $i -lt 10 ] do xterm & i=$[$i+1] done](http://slidetodoc.com/presentation_image_h2/8bf8cd30db033846c08a216e29888232/image-19.jpg)
While Loop i="0" while [ $i -lt 10 ] do xterm & i=$[$i+1] done

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

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 echo $n ENDTIME=$(date +%s) DIFF=$(( $ENDTIME - $BEGINTIME )) echo "It took $DIFF seconds"
![test. cpp int main(int argc, char *argv[]) { printf("C++ Program Starts. . . n"); test. cpp int main(int argc, char *argv[]) { printf("C++ Program Starts. . . n");](http://slidetodoc.com/presentation_image_h2/8bf8cd30db033846c08a216e29888232/image-22.jpg)
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;

Result of the comparison Windows Linux test. sh > 30 seconds 3 seconds test. cpp 0 seconds

Another example l Basically, convert all the lower cases characters in a file into upper cases, and the output will be in a output file l test 2. sh and test 2. cpp l Run in Cygwin and SSH

#!/usr /bin/bash #!/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 test 2. sh
![test 2. cpp int main(int argc, []) argc, char *argv[]) { if (argv[1]=="" || test 2. cpp int main(int argc, []) argc, char *argv[]) { if (argv[1]=="" ||](http://slidetodoc.com/presentation_image_h2/8bf8cd30db033846c08a216e29888232/image-26.jpg)
test 2. cpp int main(int argc, []) 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;

Result of the 2 nd comparison (coding time is not included) Windows Linux test. sh 0 seconds 3 seconds test. cpp 0 seconds

Can we make it faster?
![#!/usr/bin/bash # Changes a file to all uppercase E_BADARGS=65 if [ -z "$1" ] #!/usr/bin/bash # Changes a file to all uppercase E_BADARGS=65 if [ -z "$1" ]](http://slidetodoc.com/presentation_image_h2/8bf8cd30db033846c08a216e29888232/image-29.jpg)
#!/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 > test. A. out

Why use bash l Any idea? l 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. l System administration is easier to use the existing tools available in bash than to write a new program every time.

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

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

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

References http: //en. wikipedia. org/wiki/Bash http: //www. faqs. org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO. html#ss 2. 1 https: //wiki. ubuntu. com/Spec/Enhanced. Bash http: //tiswww. case. edu/php/chet/bash. html http: //www. gnu. org/software/bash. html http: //www. dsj. net/compedge/shellbasics 1. html

Questions?
- Slides: 35