350 likes | 371 Views
Learn the basics of Bash programming: variables, tokens, syntax. Understand comparison with cpp scripts and explore use cases. Delve into loops, quoting, and commands for efficient Bash programming. References and Q&A included.
E N D
Programming Languages BASH Jing Chan
Introduction • It is acronym for Bourne-again shell. • 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 • Unix machine or Windows machine • Unix-like operating system such as Cygwin
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
*HelloWorld Java: class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!") ; } } Bash: #!/bin/bash echo “Hello World”
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
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
Tokens in BASH • Backslash \ • Single quote ‘ • Double quote “ • Pipe | • Backtick `
Token (Backslash) • creating directory names, file names and etc. *Example: $ mkdir foo\ bar //creating a directory called “foo bar” $ rm –rf foo\ bar //remove
Backslash (cont’) • Escape Sequence; break down a long string Example: STRING=“ABCDEFGHIGK\ LMNOPQRSTUV” //STRING equals to both two lines
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.
Example* value=5 echo ‘I have $value dollars’ Output: I have $value dollars $value will not be interpreted
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
Quotes • What if echo without any quotes • echo '"This text is surrounded by double quotes."'
*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"
*Backtick ` • It is quotation of commands • 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 • What if we take out the ``?
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.
While Loop i="0" while [ $i -lt 10 ] do xterm & i=$[$i+1] done
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
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"
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; }
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
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
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; }
#!/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
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.
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
If you have a company that produces software, will you use bash? Why/ Why not?
Considerations of using Bash • 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#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