1 / 23

Beyond sh

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?. First tale. Close, but it misses out on files beginning with a ".".

alena
Download Presentation

Beyond sh

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. Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.

  2. First tale • Print the types of all the files in the current directory. file * Check it. Correct?

  3. First tale • Close, but it misses out on files beginning with a ".". • ls is probably a better choice than the wildcard character "*". • And how?

  4. First tale • Like this? for f in `ls -A` do file $f done • Unfortunately, not all ls's have the -A option.

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

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

  7. First tale So will the following script work IFS=' ' # This is a new line quoted by two ' for f in `ls -a` do if [ "$f" != . -a "$f" != .. ] ; then file "$f" fi done

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

  9. First tale • Interestingly, if you put those problematic filenames in a file, file happily goes along, as follows. $ echo "a file" >ff $ file -f ff a file: ascii text • What can you say? Looks like the script is not as simple as we first thought it is.

  10. 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.html )for a comparison, and advice on which one you should trust and use.

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

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

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

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

  15. Bash-- A Better Shell • Prompt • Setting • PS12="\t | \w -> " • gives you a prompt containing the current time and working directory.

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

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

  18. 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/bash PS3='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 ;; esac done

  19. Bash-- A Better Shell • Typed variables The following is self-explanatory. $ x=2 y=5 $ z=x*y $ echo $z x*y $ declare -i z $ z=x*y $ echo $z 10

  20. Bash-- A Better Shell • Typed variables • You can also do the following. $ echo $(( 2 + 3 * 15 )) 47 $ let z='2 * 5'

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

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

  23. The End • Thank you for your patience.

More Related