cs390 unix programming - uah - college of science ...hlin/cs390_fall13/lectures/othershells.pdf ·...

26
10/24/2013 Slide #1 CS390 UNIX Programming Other Shells October 24, 2013

Upload: vanxuyen

Post on 06-Sep-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #1

CS390 UNIX Programming

Other Shells

October 24, 2013

Page 2: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #2

UNIX Shells

Bourne Shell -- sh

Written by At &T

A shell commonly used by the administrator when running as root

Simpler and faster than C shell scripts

Default shell prompt: $

C shell -- csh

Developed by Berkley with added extra features

• history, aliasing, etc

Default shell prompt: %

Korn shell -- ksh

Developed by David Korn at AT&T

Added more features from C shell

Default shell prompt: $

Page 3: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #3

Linux Shells

GNU Bourne Again Shell – bash

The Linux default shell

An enhanced Bourne shell

Most popular shells used by UNIX and Linux users today

Default prompt: $

TC shell -- tcsh

An enhanced but completely compatible version of

the Berkeley UNIX C shell, csh(1).

Default prompt: >

Dash – Debian Almquist Shell

Default shell in ubuntu systems, starting from ubuntu 6.10,

much smaller and faster

Don’t like it, how to change?

Page 4: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #4

Bourne Shell

The default UNIX shell on Version 7 UNIX system released on 1979

Developed by Stephen Bourne, of AT&T Bell Lab

It was designed to replaced Thompson shell, the first Unix shell introduced in by the first UNIX system in 1971

Executable name: sh

Default prompt: $

Open source version

“ash” Almquist sh, Bourne-compatible shell for Linux

A clone of Bourne shell developed by Kenneth Almquist

Page 5: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #5

What Bourne Shell Does Not Have?

No command history

!! Will not work

!134 does not work

Upper arrow for the previous commands does not work

No command line completion with “tab”

Very inconvenient

No “let” built-in command for arithmetic operations

Need to use expr

Command substitution

Only with `… `, NOT support $(….)

Page 6: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #6

File Name Substitution

Meta character Meaning

* Matches zero or more any characters

? Matches exactly one character

[abc] Matches one character in the set

[a-z] Matches one character in the range set

[!a-z] Matches one character not in the range set

Note: on bash, both [!a-z] and [^a-z] work, but on Bourne shell, [^a-z] is not recognized

What will be displayed to the screen with the following commands

ls ?? ls a?b? ls [!1-9]*

Page 7: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #7

C/TC Shells

C Shell - csh

This shell was written at the University of California, Berkley, the origin of BSD UNIX system

It provides a C-like language with which to write shell scripts

TC Shell - tcsh

enhanced C shell, available in the public domain

Default prompt: %

Notable features

Job control

Alias

command history

Shell command, filename completion by pressing the tab key

Page 8: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #8

Shell Start-up Files

The shell start up (initialization) files setup the shell environment

System start up files

/etc/csh.cshrc

/etc/csh.login

User shell start up files: to set up the user environment variables

~/.cshrc or ~/.tcshrc

• Is called every time a new shell is created

~/.login

• Called only once at the time of user login

• Normally contains environment variables and terminal settings

• PATH and alias are suggested to be defined in file .login

Page 9: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #9

aliases

Making another name for one or more commands

Command alias lists all current set of aliases

Creating aliases with shell built-in cmd alias Syntax: alias nickname ‘original commands’ alias sshpearl ‘ssh [email protected]

in bash:

alias sshpearl=‘ssh [email protected]

Multi-commands can be combined into one line separated by “;”

• alias project1 ‘cd cs390/project1; ls’

Delete alias: unalias nickname

Notice the syntax difference compare to bash

Page 10: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #10

stdin / stdout & Redirection

Display variable with echo echo “Thanks for coming. See you soon\!\!”

echo See you soon!!

• the previous command is executed after the echo

• (in csh/tcsh, !! is for the previous command)

Redirection Input redirection: command < file

Output redirection: command > file; cmd >>out

C/TC shells has no descriptor 2 for stderr, then how to catch error output? Stdout and stderr to one file: command >& file

Separate stdout and stderr

(command>file) >& log.err

Page 11: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #11

Variables

Variable names must begin with a letter or _

Declare local variable with set

Use lower case letter by convention

set varname = Apple

• Space is allowed around the equal sign, but if one size has space, the other side must also have space.

Declare global (environmental variables with setenv)

setenv PATH $PATH:/additional/path/

Declare an integer variable: @num

To test if a variable is set or not, insert ? between $ and variable name, i.e.

$?varname

• 0: not set; 1: set

Page 12: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #12

Arrays

Define array with

set fruit = ( apples pears peaches plums )

Index starts from 1 (starts from zero in other shell!)

Display elements of the array

$fruit[1] the first element of the array

$fruit[2-3] elements from 2th to 3rd (NOT in other shells )

$fruit[*] $fruit all elements

$#fruit number of elements

$fruit[$#fruit] the last element

Differences from Bourne shell ( and bash)

Index starts at 1 instead of 0

Give an error instead of “empty” value when index is out of range

No curly braces needed in referencing values of variables

Common Mistakes

echo $fruit[0] nothing, element index starts from 1

echo $fruit[9] display error subscript out of range

Page 13: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #13

Shift on Arrays

The built-in shell command shift can take array as argument

set fruit = ( apple pear peach grape )

shift fruit

echo $fruit pear peach grape

it shifts off (to the left) the first element of the array, the length of the array is decreased by one

Not available in Bourne shell (and bash)

Page 14: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #14

Read User Input

Special variable: $<

reads a line from standard input up to (but not including) the new line and assigns the line to a variable

Read a word w/o space: set name = $<

Read a string w/ spaces: set name = “$<”

How to read multiple variables?

dakota > cat read.tcsh

#!/bin/tcsh

echo -n "What is your name? "

set name = “$<“

set names=($name)

echo Welcome $names[1], $names[2]

Page 15: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #15

C/TC shell programming

First line:

#!/bin/csh or #!/bin/tcsh

Script naming convention

scriptname.csh or scriptname.tcsh

To run the script

chmod +x script.tcsh;

./script.tcsh

Page 16: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #16

Position Parameters

$0: the name of the script

$1, …, $9, ${10}: the command line arguments

$*: all the positional parameters

$argv[0]: not valid

$argv[1], $argv[2]: command line arguments

$argv[*]: all the arguments

$argv: all arguments

$#argv: number of arguments

$argv[$#argv]: last argument

Page 17: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #17

Arithmetic

Integer only

@ symbol is used to assign the results to numeric variables

Operations: + - * / % << >>

Shortcut operators: have to have space after @

@ num += 2; @ num -= 2

@ num *= 4; @ num /= 2

@ num++

@ num--

@ num = $num + 2

Must have space after @

Must have space around the operators

Page 18: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #18

Comparison

String or number comparison

$x == $y

$x != $y

$x > $y

Patten matching

$ans =~ [Yy]*

$ans !~ [Yy]*

Page 19: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #19

Conditional Constructs

Use ( ) instead of [ ]

Testing expressions

Table 10.5 on page 536 of Textbook

Must have space around the operator

The goto command p547

It allows you to jump to some label in the program and start execution at that point

if ( expression) then

command

endif

if ( expression ) then

commands

else

commands

endif

Page 20: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #20

The switch

switch (“$color”)

case blue:

commands

breaksw

case red:

commands

breaksw

default:

breaksw

endsw

# in bash

case $color in

blue) cmd1; cmd2

;;

red) cmd1; cmd2

;;

*) commands

;;

esac

Page 21: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #21

Loops and Controls

Looping control commands, see page 566-567

shift (for command line arguments and array)

break

continue

exit

The “foreach” loop on p561

foreach file (*.cpp) c++ $file –o $file:r End

#:r causes the .cpp extension to be removed!

while ($num < 10) echo $um @ num++ end

Page 22: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #22

The noclobber/noglob Variable

noclobber

a shell built-in variable used to protect you from clobbering files in using stdout redirection

When it is set, the following command will fails if the file exits

• ls -1 > file

If not set, the commdn ls -1 >> file will fail

To set and unset the variable

• set noclobber (in bash: set –o noclobber)

• unset noclobber (in bash: set +o noclobber)

noglob

A shell built-in variable for shell metacharacters’ on/off

When it is set, metacharacters are themselves, not metacharacters

• set noglob (set –o noglob in bash)

• Unset noglob (set +o noglob in bash)

ls a* list file name a*, not any files starting with letter “a”

Page 23: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #23

Debugging Scripts

Three options ( similar with that in bash )

csh/tcsh –x scriptname

• Display each line with variable substitution before execution

csh/tcsh –v scriptname

• Display each line literally before execution

csh/tcsh –n scriptname

• Interpret but do not execution commands

Use “set” command

set echo (in bash, it is set –x)

set verbose (in bash, it is set –v)

To turn off debugging option

unset echo (in bash, it is set +x)

unset verbose

Page 24: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #24

Interactive korn Shell

Developed by David Korn of AT&T Bell Lab in early 1980s

ksh88 is the most widely used version

ksh93 version is a better compliance of POSIX and with more new features

Completely compatible with the Bourne shell

Including many features of the C shell

Executable name: ksh

Default prompt: $

It is not freely available, and is upgraded only every few years

Open source version: pdksh

Page 25: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #25

What we have talked about bash

all apply to Korn shell

Reference Chapter 2 in your textbook for a comparison of the shells

Page 26: CS390 UNIX Programming - UAH - College of Science ...hlin/cs390_fall13/lectures/othershells.pdf · CS390 UNIX Programming Other Shells October 24, ... Korn shell -- ksh Developed

10/24/2013 Slide #26

POSIX

Refers to Portable Operating System Interface for uniX

The name POSIX was suggested by Richard Stallman

A family of IEEE standards supporting portable programming for

software compatible with variants of the Unix Operating systems.

POSIX 1003.1 in 1988, covers low-level issues at the system-call level

POSIX 1003.2 in 1992, covers the shell, utility programs, and user

interface issues

The vendors and developers are not forced to follow these standards

POSIX compliancy

Comply with the POSIX standard

Most UNIX and Linux vendor try to

BASH is almost 100% compliant.

Overall, not forced to be followed