1 / 17

UNIX Shell Scripting

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.

hop
Download Presentation

UNIX Shell Scripting

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. UNIX Shell Scripting > Echo “A quick how-to”

  2. 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

  3. 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.

  4. 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’

  5. 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

  6. 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

  7. 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.

  8. 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

  9. 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.

  10. 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.

  11. 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 …

  12. 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.

  13. 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… >

  14. 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.

  15. 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.

  16. 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.”

  17. 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

More Related