Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark...

23
Beyond sh •Not everyone is as fond of UNIX as most other people. •The tutorial talks about the dark side of UNIX.

Transcript of Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark...

Page 1: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Beyond sh

•Not everyone is as fond of UNIX as most other people.

•The tutorial talks about the dark side of UNIX.

Page 2: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Print the types of all the files in the current directory.

file *

Check it. Correct?

Page 3: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Close, but it misses out on files beginning with a ".".

• ls is probably a better choice than the wildcard character "*".

• And how?

Page 4: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Like this?for f in `ls -A`

do

file $f

done

• Unfortunately, not all ls's have the -A option.

Page 5: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• No big deal, since they all have the -a option. • How do you know? Well, the earliest version has

it; so must all the subsequent versions.• So we have the following script

for f in `ls -a`

do

if [ $f != . -a $f != .. ] ; then

file $f

fi

done

Page 6: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Seems all right, until some weirdo comes by and protests, "my filenames have spaces!".

• Fortunately, sh is all too potent for trivia like this. • Weapon : IFS

– IFS is the "inter-field separator". We set it to the newline character instead of the default space character to make sure that each line (possibly containing spaces) of `ls -a` is considered a single filename. We need to add some double quotes too.

Page 7: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

So will the following script workIFS='

' # This is a new line quoted by two '

for f in `ls -a`

do

if [ "$f" != . -a "$f" != .. ] ; then

file "$f"

fi

done

First tale

Page 8: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Boom - it still cracks (breaking up filenames with spaces into pieces).

• After some head-scratching, we got the culprit nailed: the file command, a stiff-necked oddball out there, refuses to give spaces their due respect.

• Try anything you can (file "a file" ... file "'a file'" ... file a\ file ...), it just wouldn't do it.

Page 9: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

First tale

• Interestingly, if you put those problematic filenames in a file, file happily goes along, as follows.$ echo "a file" >ff$ file -f ffa file: ascii text

• What can you say? Looks like the script is not as simple as we first thought it is.

Page 10: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Beyond sh

• S.R. Bourne is probably still up and running, but the shell that bears his name, the Bourne shell (sh), has really gone pass its prime of life. Many had made their offer as a replacement, but only a few made it to the stage: csh tcsh ksh bash zsh

• Please refer to the article UNIX Shell Differences(http://www.softlab.ntua.gr/unix/shelldiff.ht

ml )for a comparison, and advice on which one you should trust and use.

Page 11: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Command-line editing– Default is emacs mode. If you want vi mode,

you type– $ set -o vi– Then you can edit your command line just like

a line in your vi session. Previous commands are like previous lines, and so you can use vi commands such as j, k to go back and forth.

Page 12: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• The fc command– fc -l lists the most recent commands in the

history list. – fc -s repeats the last command. – fc -s 27 repeats command #27. fc alone lets

you edit the last command. And so on.

Page 13: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• History expansion– You can use the up and down cursor

commands of vi or emacs to retrieve, edit, and execute previous commands, or the fc command, or the "!" notation.

– For example, "!!" repeats the last command.

Page 14: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Aliases– One of the most common aliases:

– $ alias ls="ls -l"

– If you append a space, then you can do the following.

– $ alias ls="ls -l "

– $ alias home="/usr/staff/flau"

– $ ls home

– ...

Page 15: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Prompt– Setting– PS12="\t | \w -> "– gives you a prompt containing the current time

and working directory.

Page 16: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Functions– Later versions of sh support functions. – Bash goes one step further: functions can have

local variables apart from $1, $2, ...

Page 17: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Command substitution– The back quotes are ugly looking and clumsy to use

when you have to nest them. Bash uses $( ) instead of ` `. Which one in the following d'you prefer?

for s in $(cd $d; file $(ls -A) 2>/dev/null |

fgrep $i directory | cut -d: -f1)– or

for s in `cd $d; file \`ls -A\` 2>/dev/null |

fgrep $i directory | cut -d: -f1`

Page 18: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Select– In addition to for, while, case, if, bash gives you the

handy select construct. Here is an example. Try it and find out why select is so neat.

#!/usr/local/bin/bashPS3='Please type a number: 'select x in yes no quit; do case $x in yes) echo YES ; break ;; no) echo NO ; break ;; quit) echo BYE ; break ;; "") echo ERROR - please try again ;; esacdone

Page 19: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Typed variablesThe following is self-explanatory.

$ x=2 y=5

$ z=x*y

$ echo $z

x*y

$ declare -i z

$ z=x*y

$ echo $z

10

Page 20: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Typed variables– You can also do the following.

$ echo $(( 2 + 3 * 15 ))

47

$ let z='2 * 5'

Page 21: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Job control– You can suspend a job, run a job in the

background, and exercise certain control over running or suspended background jobs.

Page 22: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

Bash-- A Better Shell

• Finally, something about the builtin function eval. – Simply put, eval evaluates (executes) an

expression that is supplied as a parameter. For example, the eval line in the following actually evaluates the expression "$2".

$ set hello world$ eval echo '$'$#world

Page 23: Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

The End

•Thank you for your patience.