UNIX Shell Scripting - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

UNIX Shell Scripting

Description:

What is shell scripting? Interpreted (non-compiled) language ... In order to execute a shell script in a UNIX environment, you have to configure ... – PowerPoint PPT presentation

Number of Views:539
Avg rating:3.0/5.0
Slides: 18
Provided by: KSU5
Category:
Tags: unix | scripting | shell

less

Transcript and Presenter's Notes

Title: UNIX Shell Scripting


1
UNIX Shell Scripting
  • gt 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 files
    permissions with the chmod command
  • gt 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
gt hello.csh gt Hello World gt
!/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
Variability
  • Variables are defined as follows
  • variable_namevalue
  • There must be no whitespace between the operands
    and the operator (xy)
  • 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 commands statement, enclosed
    variables may be substituted, as in the following
    example, where x 12.

INPUT echo variable x x echo variable x
x
OUTPUT gt variable x 12 gt 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 neednt necessarily be an else, but if you
    use an if statement, you must conclude with a
    fi statement.

11
Ifthenelsefi
  • 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
Whiledodone
  • The following is a short while loop that counts
    to 3

!/bin/bash number1 echo -n "Testing..." while
"number" -lt 4 do echo -n
"number..." numberexpr number
1 done echo ""
gt test.csh gt Testing123 gt
14
Test conditions
  • Below are several different tests that can be
    done on numbers or strings - the left box is what
    were 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- xexpr 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/Introdu
    ction_to_bash_Shell_Scripting.html
  • http//pegasus.rutgers.edu/elflord/unix/bash-tute
    .html
  • http//www.devhood.com/tutorials/tutorial_details.
    aspx?tutorial_id468
Write a Comment
User Comments (0)
About PowerShow.com