Intro To Shells
Intro To Shells
This document contains lecture notes for informal Unix seminar for ITT AES employees (Reston, VA). No information in this document is either endorsed by or attributable to ITT. This document contains no ITT Privileged/Proprietary Information.
What Is A Shell?
It is easier to port a shell than a shell script. Larry Wall
$ ls
shell
Interactive: you write something, and the system executes it immediately. You need: 1. Good process control mechanism 2. Good history and command line editing facilities Batch: you write a script, and the system executes it. You need: 1. Good programming facilities (language!) 2. Good redirection facilities
See http://www.faqs.org/faqs/unix-faq/shell/shell-differences/ Bourne shell: /bin/sh. Very good language, great redirection. Bad for interactive use. 90% scripts use /bin/sh C shell: /bin/csh. Tried to mimic C language. Badly. Most implementation buggy. Others are brain dead. Better for interactive use. No good for scripts.
3
New C shell: /bin/tcsh. Bug xed, new features added. Unfortunately, non standard. Korn shell: /bin/ksh. Tried to combine Bourne and C shells. Bournelike syntax, C-like interactive features. Unfortunately, not free and not bundled with commercial systems. Bourne Again Shell: /bin/bash. A free gift from GNU. Compatible with /bin/sh, but with nice features from C shell, /bin/ksh and other shells. Good for anything. Experimental shells: /bin/zsh, /bin/rc & others.
1. If you are a novice or have time to learnuse bash 2. Never write scripts in cshsee the famous paper by Tom Christiansen, http://www.perl.com/pub/language/versus/csh.html. Do yourself a favor, and if you have to write a shell script, do it in the Bourne shell. 3. If you do not have bash, try ksh 4. If you are a csh veteran, try tcsh. Still, you might benet form sh or bash
Interactive work requires reuse of commands. This is called command line history.
Setting Up
Setting the number of commands to remember
HISTSIZE=100 # bash, ksh set history=100 # csh
Remembering the history from session to session: Automatic in bash & ksh In csh use
set savehist=100
history gives the list of commands with numbers !number repeats the command with this number
boris@reston-0491:~$ history |tail 497 w 498 ls 499 exit 500 pwd 501 pwd 502 history 503 history |head 504 history |tail boris@reston-0491:~$ !500 pwd /home/boris
10
A real-life example:
boris@reston-0491:~$ history |grep gsfc 118 ssh class.gsfc.nasa.gov 506 history |grep gsfc boris@reston-0491:~$ !118 ssh class.gsfc.nasa.gov
11
12
It can be sophisticated. . .
boris@reston-0491:~$ pdflatex 6_shellsintro.tex ... boris@reston-0491:~$ !pdf:s/pdflatex/ls -ls/ ls -ls 6_shellsintro.tex 12 -rw-r--r-- 1 boris users 8763 Jun 26 15:15 6_shellsintro.tex
13
!:0 repeat the command name !* repeat all arguments !-5 repeat last 5 commands
14
Job Control
The dierence between a career and a job is about 20 hours a week.
15
16
xeyes
17
18
19
20
21
Ways to Kill. . .
To send a signal to a program:
kill -HUP %1 kill -1 %1
Some common signals Signal HUP KILL TERM STOP CONT Value 1 9 15 19 18 Meaning Hangup. Often causes reloading Kills politely Kills impolitely Suspends Allows to continue
22
Command, started form command line, use your keyboard as input and your screen as output. What if you put the command into background? 1. If it needs input, it stops 2. Output is still your screen
23
boris@reston-0491:~$ telnet kenny Trying 151.190.55.231... Connected to kenny.reston.aes.de.ittind.com. Escape character is ^]. SunOS 5.6 (kenny) login:
24
telnet kenny
25
26
What happens to our background jobs if we log out? 1. The program receives the HUP signal (from hangup. Remember old modems?) 2. If it tries to write to a screen, it dies What if we want to have it overnight?
nohup myprogram &