370 likes | 910 Views
UNIX Shell Scripting. > Echo “A quick how-to”. What is shell scripting?. Interpreted (non-compiled) language Slower compared to compiled languages Much more portable than compiled language though This presentation uses the bash scripting language. Configuring for execution.
E N D
UNIX Shell Scripting > Echo “A quick how-to”
What is shell scripting? • Interpreted (non-compiled) language • Slower compared to compiled languages • Much more portable than compiled language though • This presentation uses the bash scripting language
Configuring for execution • In order to execute a shell script in a UNIX environment, you have to configure the file’s permissions with the “chmod” command > Chmod 700 file1.csh • The script must be executed from the folder which it resides in.
Shortcut alias • You can set a script to be run from any folder by setting an alias for the file. • This can be done by editing the user file which contains aliases, for example, .bashrc alias run=‘/home/user/file1.csh’
Hello World > hello.csh > Hello World > #!/bin/bash echo “Hello World” • The #!/bin/bash line must be on the first line of all bash scripts - it indicates where the compiler for the script is located. • echo is the shell command to print out a line
All for one, and quotes around them all • In order to include whitespace in variables, and in several other cases, statements must be surrounded by quotation marks. There are three different types: “ ” (double quotes) - basic type, enclosed variables are substituted with their values ‘ ’ (single quotes) - same as doubles, except no variable substitution ` ` (back ticks) - used for evaluating enclosed commands
x=5 Variability • Variables are defined as follows: variable_name=value • There must be no whitespace between the operands and the operator (x=y) • Unlike most compiled languages, bash scripts do not have different variable types - so no declaring variables of type int, char, etc.
Echoing variables • Except at execution, when referring to variables, they must be prefixed with a $ • Depending on which type of quotation mark you use to enclose an echo command’s statement, enclosed variables may be substituted, as in the following example, where x = 12. INPUT echo “variable x = $x” echo ‘variable x = $x’ OUTPUT > variable x = 12 > variable x = $x
Control Structures • if - if (statement) is true, then do one set of instructions, otherwise, do a different one • while - while (statement) is true, do a set of instructions. • Also, there are the “for”, “case”, and “until” structures, but we will not go over these two.
If (wishes = fishes) • This structure basically goes: • if (x)…then (y)…else (z)…fi • There needn’t necessarily be an else, but if you use an “if” statement, you must conclude with a “fi” statement.
If…then…else…fi • In the following code segment, $from_address is a variable which contains the name of the sender of an email. … if [ “$from_address” = “Wendy” ] then echo “You have mail from Wendy” else echo “Wendy has not sent you any mail” fi …
While - the loopiest of the loops • The while control structure basically goes: • While (x)…then (y)…done • “While” indicates the beginning of the loop and tests the condition. • “Then” is executed as long as “While” is true. • “Done” indicates the end of the loop segment.
While…do…done • The following is a short while loop that counts to 3 #!/bin/bash number=1 echo -n "Testing..." while [ "$number" -lt 4 ]; do echo -n "$number..." number=`expr $number + 1` done echo "" > test.csh > Testing…1…2…3… >
Testing Numbers: Testing Strings: x = y x != y x > y x < y x -eq y x -ne y x -gt y x -lt y x is the same as y x is not the same as y x is not null x is null x = y x != y -n x -z x Test conditions • Below are several different tests that can be done on numbers or strings - the left box is what we’re used to, and the right box is the bash script code.
Readin’, ‘Riting, and ‘Rithmatic • There are two ways in a bash script to do simple arithmatic (add [+], subtract [-], multiply [*], divide [/], and modulus [%]) • The first way is using the expr command: x=$(expr 3 + 4) -or- x=`expr 3 + 4` • The second is using double parenthesis: x=$((3 + 4)) Note that both ways deal only with integers - neither handles deciman values.
Please enter your name: • In some cases, you might need to prompt for user input - this is done with the “read” command. • Format: “read variable_name” #!/bin/bash echo “What is your name?” read name echo “Hello $name.”
References • http://www.linuxnewbie.org/nhf/Programming/Introduction_to_bash_Shell_Scripting.html • http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html • http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=468