Unix® Shell Programming读书笔记4(END)

61.The type Command
When you type in the name of a command to execute, it's frequently useful to know where that command is coming from.
$ type pwd pwd is a shell builtin

62.The vi Line Edit Mode
Make your bash like a vi editor:
$set -o vi
list below lists all the editing commands in vi mode. Note: [count] is any integer and may be omitted.
Input Mode Commands
Command Meaning erase
(Erase character, usually Ctrl+h or #); delete previous character.
Ctrl+w
Delete the previous blank-separated word.
kill
(Line kill character, normally Ctrl+u or @); delete the entire current line.
eof
(End-of-file character, normally Ctrl+d); terminate the shell if the current line is empty.
Ctrl+v
Quote next character; editing characters and the erase and kill characters may be entered in a command line or in a search string if preceded by a Ctrl+v.
Enter
Execute the current line.
Esc
Enter edit mode.
Edit Mode Commands
Command
Meaning
[count]k
Get previous command from history.
[count]-
Get previous command from history.
[count]j
Get next command from history.
[count]+
Get next command from history.
[count]G
Get the command number count from history; the default is the oldest stored command.
/string
Search history for the most recent command containing string; if string is null, the previous string will be used (string is terminated by an Enter or a Ctrl+j); if string begins with ^, search for line beginning with string.
?string
Same as / except that the search will be for the least recent command.
n
Repeat the last / or ? command.
N
Repeat the last / or ? command but reverse the direction of the search.
[count]l or [count]space
Move cursor right one character.
[count]w
Move cursor right one alphanumeric word.
[count]W
Move cursor right to next blank-separated word.
[count]e
Move cursor to end of word.
[count]E
Move cursor to end of current blank-separated word.
[count]h
Move cursor left one character.
[count]b
Move cursor left one word.
[count]B
Move cursor left to previous blank-separated word.
0
Move cursor to start of line.
^
Move cursor to first nonblank character.
$
Move cursor to end of line.
[count]|
Move cursor to column count; 1 is default.
[count]fc
Move cursor right to character c.
[count]Fc
Move cursor left to character c.
[count]tc
Same as fc followed by h.
[count]Tc
Same as Fc followed by l.
;
Repeat the last f, F, t, or T command.
,
Reverse of ;.
a
Enter input mode and enter text after the current character.
A
Append text to the end of the line; same as $a.
[count]c motion
Delete current character through character specified by motion and enter input mode; if motion is c, the entire line is deleted.
C
Delete current character through end of line and enter input mode.
S
Same as cc.
[count]d motion
Delete current character through the character specified by motion; if motion is d, the entire line is deleted.
D
Delete current character through the end of line; same as d$.
i
Enter input mode and insert text before the current character.
I
Enter input mode and insert text before the first word on the line.
[count]P
Place the previous text modification before the cursor.
[count]p
Place the previous text modification after the cursor.
[count]y motion
Copy current character through character specified by motion into buffer used by p and P; if motion is y, the entire line is copied.
Y
Copy current character through the end of line; same as y$.
R
Enter input mode and overwrite characters on the line.
[count]rc
Replace the current character with c.
[count]x
Delete current character.
[count]X
Delete preceding character.
[count].
Repeat the previous text modification command.
~
Invert the case of the current character and advance the cursor.
[count]_
Append the count word from the previous command and enter input mode; the last word is the default.
*
Attempt filename generation on the current word; if a match is found, replace the current word with the match and enter input mode.
=
List files that begin with current word.
\
Complete pathname of current word; if current word is a directory, append a /; if current word is a file, append a space.
u
Undo the last text modification command.
U
Restore the current line to its original state.
@letter
Soft function key—if an alias of the name _letter is defined, its value will be executed.
[count]v
Execute vi editor on line count; if count is omitted, the current line is used.
Ctrl+l
Linefeed and print current line.
L
Reprint the current line.
Ctrl+j
Execute the current line.
Ctrl+m
Execute the current line.
Enter
Execute the current line.
#
Insert a # at the beginning of the line and enter the line into the command history (same as I#Enter). Table A.4 lists all the editing commands in vi mode. Note: [count] is any integer and may be omitted.
Input Mode Commands
Command
Meaning
erase
(Erase character, usually Ctrl+h or #); delete previous character.
Ctrl+w
Delete the previous blank-separated word.
kill
(Line kill character, normally Ctrl+u or @); delete the entire current line.
eof
(End-of-file character, normally Ctrl+d); terminate the shell if the current line is empty.
Ctrl+v
Quote next character; editing characters and the erase and kill characters may be entered in a command line or in a search string if preceded by a Ctrl+v.
Enter
Execute the current line.
Esc
Enter edit mode.
Edit Mode Commands
Command
Meaning
[count]k
Get previous command from history.
[count]-
Get previous command from history.
[count]j
Get next command from history.
[count]+
Get next command from history.
[count]G
Get the command number count from history; the default is the oldest stored command.
/string
Search history for the most recent command containing string; if string is null, the previous string will be used (string is terminated by an Enter or a Ctrl+j); if string begins with ^, search for line beginning with string.
?string
Same as / except that the search will be for the least recent command.
n
Repeat the last / or ? command.
N
Repeat the last / or ? command but reverse the direction of the search.
[count]l or [count]space
Move cursor right one character.
[count]w
Move cursor right one alphanumeric word.
[count]W
Move cursor right to next blank-separated word.
[count]e
Move cursor to end of word.
[count]E
Move cursor to end of current blank-separated word.
[count]h
Move cursor left one character.
[count]b
Move cursor left one word.
[count]B
Move cursor left to previous blank-separated word.
0
Move cursor to start of line.
^
Move cursor to first nonblank character.
$
Move cursor to end of line.
[count]|
Move cursor to column count; 1 is default.
[count]fc
Move cursor right to character c.
[count]Fc
Move cursor left to character c.
[count]tc
Same as fc followed by h.
[count]Tc
Same as Fc followed by l.
;
Repeat the last f, F, t, or T command.
,
Reverse of ;.
a
Enter input mode and enter text after the current character.
A
Append text to the end of the line; same as $a.
[count]c motion
Delete current character through character specified by motion and enter input mode; if motion is c, the entire line is deleted.
C
Delete current character through end of line and enter input mode.
S
Same as cc.
[count]d motion
Delete current character through the character specified by motion; if motion is d, the entire line is deleted.
D
Delete current character through the end of line; same as d$.
i
Enter input mode and insert text before the current character.
I
Enter input mode and insert text before the first word on the line.
[count]P
Place the previous text modification before the cursor.
[count]p
Place the previous text modification after the cursor.
[count]y motion
Copy current character through character specified by motion into buffer used by p and P; if motion is y, the entire line is copied.
Y
Copy current character through the end of line; same as y$.
R
Enter input mode and overwrite characters on the line.
[count]rc
Replace the current character with c.
[count]x
Delete current character.
[count]X
Delete preceding character.
[count].
Repeat the previous text modification command.
~
Invert the case of the current character and advance the cursor.
[count]_
Append the count word from the previous command and enter input mode; the last word is the default.
*
Attempt filename generation on the current word; if a match is found, replace the current word with the match and enter input mode.
=
List files that begin with current word.
\
Complete pathname of current word; if current word is a directory, append a /; if current word is a file, append a space.
u
Undo the last text modification command.
U
Restore the current line to its original state.
@letter
Soft function key—if an alias of the name _letter is defined, its value will be executed.
[count]v
Execute vi editor on line count; if count is omitted, the current line is used.
Ctrl+l
Linefeed and print current line.
L
Reprint the current line.
Ctrl+j
Execute the current line.
Ctrl+m
Execute the current line.
Enter
Execute the current line.
#
Insert a # at the beginning of the line and enter the line into the command history (same as I#Enter).
Note that you can put this in your .profile or ENV file to automatically start up the shell with one of the edit modes turned on.

63.The fc Command
The fc command allows you to start up an editor on one or more commands from your history or to simply write a list of history commands to your terminal.
fc -l 510 515 writes commands 510 through 515 to standard output, whereas the command
fc -n -l -20 writes the last 20 commands to standard output,
Suppose that you've just executed a long command line and then decide that it would be nice to turn that command line into a shell program called runx. You can use fc to get the command from your history and I/O redirection to write that command to a file: fc -n -l -1 > runx

64.((....))----Integer Arithmetic
One advantage of using ((...)) rather than test is the capability to perform arithmetic as part of the test:
if (( i / 10 != 0 ))
then
...
fi

65.Numbers in Different Bases
The Korn shell and Bash allow you to perform arithmetic in different bases. To write a number in a different base with these shells, you use the notation base#number
For example, to express the value 100 in base 8 (octal) you write 8#100
66.The alias Command
An alias is a shorthand notation provided by the shell to allow customization of commands.
The format is alias name=string where name is the name of the alias, and string is any string of characters. For example,
alias ll='ls -l'
The unalias command is used to remove aliases from the alias list. The format is unalias name

67.Arrays
$ arr[O]=hello
$ arr[1]="some text"
$ arr[2]=/users/steve/memos
$ echo ${array[0]}
hello
$ echo ${array[1]}
some text
$ echo ${array[2]}
/users/steve/memos
$ echo $array
hello
The construct ${#array[*]} can be used to substitute the number of elements in the array array.
$ echo ${#array[*]}
3
Note that the number reported here is the actual number of values stored inside the array, not the largest subscript used to store an element inside the array.
You can declare an array of integers to the shell simply by giving the array name to typeset -i:
typeset -i data
Integer calculations can be performed on array elements using the ((...)) construc.
Summarize the various array constructs in the Korn shell and Bash.
Construct Meaning
${array[i]} Substitute value of element i
$array Substitute value of first element (array[0])
${array[*]} Substitute value of all elements
${#array[*]} Substitute number of elements
array[i]=val Store val into array[i]

68.Job Control
The shell's built-in kill command can be used to terminate a job running in the background. The argument to it can be a process number or a percent sign (%) followed by a job number, a + (current job), a - (previous job), or another % (also current job).
The preceding kill could have used %+ or %% to refer to the same job.

69.Other Features of the cd Command
The - argument to cd always means "the previous directory."

70.Order of Search
It's worthwhile listing the order of searching the shell uses when you type a command name:
1. The shell first checks to see whether the command is a reserved word (such as for and do).
2. If it's not a reserved word and is not quoted, the shell next checks its alias list, and if it finds a match, performs the substitution. If the alias definition ends in a space, it attempts alias substitution on the next word. The final result is then checked against the reserved word list, and if it's not a reserved word, the shell proceeds to step 3.
3. Next, the shell checks the command against its function list and executes it if found.
4. The shell checks to see whether the command is a built-in command (such as cd and pwd).
5. Finally, the shell searches the PATH to locate the command.
6. If the command still isn't found, a "command not found" error message is issued.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值