2008年4月18日 星期五

[筆記]Wizard Boot Camp

Wizard Boot Camp
little-known topics that wizards should know


A shell is a command interpreter
A pair of single quotes (') is stronger than a pair of double quote ("). Nothing is substituded in a pair of single quote (').



Problematic Ways that Shells Get Commands
bash < script-file
command-generator | bash
the standard input, inherited by the child process that runs commands in script-file/command-generator, is redirected, causing the child process would read input from the script-file/command-generator.


Redirect multiple command outputs
curly-brace operator {}
crunch1 > crunches_out
crunch2 >> crunches_out
crunch3 >> crunches_out
is equivalent to
{ crunch1; crunch2; crunch3; } > crunches_out
注意{後、}前有一個空格,不然會出錯
這個與subshell ()不同,前後不需空格,且會fork新的child process,因此working directory不會被改到, ref Bash Command substitution & Subshell
C2QSERVER / # (cd root;ls;)>log; pwd
/
C2QSERVER / # { cd root;ls; }>log; pwd
/root
C2QSERVER root #
another example:
for prog in crunch{1,2,3}
do
$prog
done > crunches_out

Redirect input
ls | while read filename ;do echo $filename;done;

Copy file descriptor
m>&n

Redirect file descriptor of current process
exec > log 2>&1

default file descriptor
0: stdin
1: stdout
2: stderr

3-9
10 and above- used internally by shell


replace
get-dirnames |
while read dir
do
cd "$dir"

date >> start-times
echo "$dir" >> dirnames
...
done
with
get-dirnames |
while read dir
do
cd "$dir"
date 1>&3
echo "$dir" 1>&4

...
done 3>start-times 4>dirnames
more efficient


$$: current process ID
lsof -p $$


named pipe (FIFO)
mkfifo


ps
GNU options
BSD (berkeley) options - not start with a dash

x : all ttys and non-ttys
a : all users' processes
u : user oriented, use user ID (names)
l/-l : long
f: forest

-H : hierarchical
-F : extra full

output
TTY-? process doesn't have a controlling tty
STAT-R-running, S-waiting, ref ps(1) - PROCESS STATE CODES
UID
PRI
NI-being nice to other
VSZ-virtual memory size
RSS-resident set size
PID-process ID
PPID-parent PID

ps -o "pid ppid ruid euid rgid egid args"



如何找到sudo -s前的原user?
ps auxf
ps axfo "user pid ppid stat tname args"
ps afo "user pid args"
ps aux -H
ps ax -HF


for the su case
root      2209     1  0  2264  2720   0 Apr09 ?        Ss     0:00   /usr/sbin/smbd -D
root 2218 2209 0 2264 1104 0 Apr09 ? S 0:00 /usr/sbin/smbd -D
root 2220 1 0 1231 1084 0 Apr09 ? Ss 0:00 /usr/sbin/sshd
root 8737 2220 0 1925 2260 0 Apr25 ? Ss 0:00 sshd: test [priv]
test 8739 8737 0 1925 1624 0 Apr25 ? S 0:00 sshd: test@pts/2
test 8740 8739 0 1407 3144 0 Apr25 pts/2 Ss 0:00 -bash
root 8759 8740 0 929 1084 0 Apr25 pts/2 S 0:00 su
root 8760 8759 0 1006 1732 0 Apr25 pts/2 S 0:00 bash
root 10070 8760 0 6844 1104 0 00:25 pts/2 Sl 0:00 iperf -s
root 10073 8760 0 4795 1044 0 00:25 pts/2 Sl 0:00 iperf -s -u
root 10099 8760 0 851 948 0 03:29 pts/2 R+ 0:00 ps ax -HF
or the sudo -s case
root      5460     1  0  1054   920   2 Apr16 ?        Ss     0:00   /usr/sbin/sshd
root 12860 5460 0 1756 2108 0 Apr26 ? Ss 0:00 sshd: mac [priv]
mac 13848 12860 0 1791 1516 0 Apr26 ? S 0:00 sshd: mac@pts/0
mac 13867 13848 0 774 1632 0 Apr26 pts/0 Ss 0:00 -bash
root 17608 13867 0 741 1688 1 Apr26 pts/0 S 0:00 /bin/bash
root 2577 17608 0 1637 2836 1 01:46 pts/0 T 0:00 vi M
root 3830 17608 0 555 900 0 02:29 pts/0 R+ 0:00 ps ax -HF




Process control using signal

CTRL-C
CTRL-Z
SIGTERM(15) : terminate
SIGINT(2) CTRL-C : interrupt
SIGHUP(1) hang up

nohup

SIGQUIT (?) CTRL-\ : core dump before termination
SIGKILL (9) : forced termination

SIGSTOP (?)
SIGTSTP (?) CTRL-Z : terminal stop
SIGCONT (?)

%1 : job #1
fg %1
bg %1
kill %1
kill -sigkill %1
kill -kill %1
CTRL-Z : to foreground job
kill -stop %1 : to background job

Bourne-type shell (GNU Bash)
limited in tcsh
sophisticated in zsh (super-shell)

trap [todo] [signals]
trap 'echo $stat' 2 15
trap 'echo $stat' 0
注意單引號是不展開shell variable的,因此$stat會到執行時才被展開


Wizard Boot Camp, Part Six: Daemons & Subshells
http://www.linux-mag.com/id/5981

沒有留言: