Xnix 备忘录

来源:互联网 发布:mac双系统安装win10 编辑:程序博客网 时间:2024/06/09 16:29

I. Summary

  • In general, everything in UNIX is either a file or a process.

  • By convention, environment variables have UPPER CASE and shell variables have lower case names.

  • Shell functions use positional parameters and special variables like * and # in exactly the same way as shell scripts do.

  • 硬盘分区是独立于操作系统的,所以才有可能建立多引导的操作系统

 

II. System

  • File System: 一个/usr文件系统内含程序和不变的数据;一个/var 文件系统内含变化着的数据(如log 文件);一个/etc 目录中存储了与机器有关的配置文件,以及一个包含每个人的个人文件的/home文件系统。依赖于硬件的配置以及系统管理员的决策,文件系统的分布可以是不 同的;它甚至全都合在一个文件系统中。


  • Init Command:  Each time you login to a UNIX host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The C and TC shells uses two files called .login and .cshrc (note that both file names begin with a dot). At login the C shell first reads .cshrc followed by .login

    .login/.bash_profile set conditions which will apply to the whole session and to perform actions that are relevant only at login .cshrc/.bashrc set conditions and perform actions specific to the shell and to each invocation of it     GUIDELINE set ENVIRONMENT variables in the .login/.bash_profile file and SHELL variables in the .cshrc/.bashrc  file. WARNING NEVER put commands that run graphical displays (e.g. a web browser) in your .cshrc or .login file.

    If .bash_profile doesn't exist in your home directory, then bash will look for .bash_login. If that doesn't exist it will look for .profile. .bash_profile is read and executed only by the login shell. If you start up a new shell (a subshell) by typing bash on the command line, it will attempt to read commands from the file .bashrc


  • Input and Output: The UNIX I/O scheme is based on two dazzlingly simple ideas. First, UNIX file I/O takes the form of arbitrarily long sequences of characters (bytes). In contrast, file systems of older vintage have more complicated I/O schemes (e.g., "block," "record," "card image," etc.). Second, everything on the system that produces or accepts data is treated as a file; this includes hardware devices like disk drives and terminals. Older systems treated every device differently. Both of these ideas have made systems programmers' lives much more pleasant.


  • Script: A script (a file that contains shell commands) is a shell program. Your .bash_profile and environment files are shell scripts.

    Ways to run script Executable Needed PATH Needed Is Subshell source script.sh no no no script.sh yes yes yes
  • Subshell: There are many ramifications to using subshells. An important one is that the exported environment variables (e.g., TERM, EDITOR, PWD) are known in subshells, whereas other shell variables (such as any that you define in your .bash_profile without an export statement) are not.


  • The order of precedence for the various sources of commands when you type a command to the shell:

    1. Aliases

    2. Keywords such as function and several others, like if and for

    3. Functions

    4. Built-ins like cd and type

    5. Scripts and executable programs, for which the shell searches in the directories listed in the PATH environment variable

  • Pipes and signals were the only IPC mechanisms in early versions of UNIX. More modern versions like System V and BSD have additional mechanisms, such as sockets, named pipes, and shared memory.

 

III. Command

命令参数选项一般由”-”引导,后面跟一个字符(或者”--“,当选项超过一个字符时)。这样,”-”有点象DOS下的”/”。举个例子,输入命令 rm --help.

Shell command lines consist of one or more words, which are separated on a command line by blanks or TABs. The first word on the line is the command. The rest (if any) are arguments (also called parameters) to the command, which are names of things on which the command will act.

An option is a special type of argument that gives the command specific information on what it is supposed to do. Options usually consist of a dash followed by a letter; we say "usually" because this is a convention rather than a hard-and-fast rule. The command lp -h myfile contains the option -h, which tells lp not to print the "banner page" before it prints the file.

Sometimes options take their own arguments. For example, lp -d lp1 -h myfile has two options and one argument. The first option is -d lp1, which means "Send the output to the printer (destination) called lp1." The second option and argument are the same as in the previous example.

 

1. mount/unmount

$ mount /dev/hda2 /home
$ mount /dev/hda3 /usr

mount 命令带有两个参数。第一个是与包含该文件系统的磁盘或分区相关的设备文件。第二个是目录,在该目录下面文件系统将被加载。在执行完上面的命令后,两个文件 系统的内容看上去分别象是/home 以及/usr 目录中的内容。于是,可以说“/dev/hda2 加载到了/home”,对/usr也是类似的。为了看各个文件系统的内容,只要观察它所加载到的目录的内容,就好象这个目录是别的目录一样。请注意设备文 件/dev/hda2 和被加载目录/home 之间的区别。设备文件给出了访问磁盘上原始数据的能力,而被加载目录给出了访问磁盘上文件的能力。被加载的目录称为加载点(mount point)。

$ umount /dev/hda2
$ umount /usr

umount 有一个参数:设备文件名或是加载点
 

2. shell

你可以检查你现在正在运行的shell : echo $SHELL

你可以通过显示环境变量“shell level”(shell层次)看看你当前环境下的shell的堆栈:echo $SHLVL

 

3. sort

To output the sorted list to a file: %sort < unsorted.file > sorted.file

 

4. tar

tar -cf  tarfilename sourcefilename

tar -xf  tarfilename

 

5. export

Any variable can become an environment variable. First it must be defined as usual; then it must be exported with the command (Unless automatic exporting has been turned on by set -a or set -o allexport, in which case all variables that are assigned to will be exported):

 

export varnames

(varnames can be a list of variable names separated by blanks). You can combine variable assignment and the export into one statement:

export wonderland=alice

 

6. grep

Some of the options of grep are:

  • -v display those lines that do NOT match
  • -n precede each maching line with the line number
  • -c print only the total count of matched lines
  • -i ignore upper/lower case distinctions

You can use more than one option at a time, for example, the number of lines without the words 'science shit' or 'Science Shit' is

grep -ivc 'science shit' science.txt

for file in $(find . -name "*.sh"); do

    if grep -n 'cd' $file;then

        echo 'found in ' $file

    fi

done

 

IV. Util

0. Quickly create a file and input some data from console:

% cat > some.file //create

% cat >> some.file //append

 

1. Join (concatenate) two files into a new file:

% cat file1 file2 > somenew.file

 

2. 把标准输出同时写到文件和屏幕

% dir | tee filelisting.txt 

tee是“T型连接器”的模拟音,在管道中的主要的用途是分流。

 

3. script

Linux 使用标准的 Unix约定,在脚本文件的第一行包括解释程序的名字。所以一个典型的脚本文件可能开头是:

#!/usr/bin/wish

 

4. auto complete (emacs mode)

TAB

Attempt to perform general completion of the text

ESC-?

List the possible completions (ensures that the result will be a filename and not a function or command name)

ESC-/

Attempt filename completion

CTRL-X /

List the possible filename completions

ESC-~

Attempt username completion

CTRL-X ~

List the possible username completions

ESC-$

Attempt variable completion

CTRL-X $

List the possible variable completions

ESC-@

Attempt hostname completion

CTRL-X @

List the possible hostname completions

ESC-!

Attempt command completion

CTRL-X !

List the possible command completions

ESC-TAB

Attempt completion from previous commands in the history list

CTRL-U

Kills the line from the beginning to point

ESC-C

Capitalize word after point

ESC-U

Change word after point to all capital letters

ESC-L

Change word after point to all lowercase letters

 

5. special directory

Directory Symbol root directory / home directory ~ pre directory - current directory . parent directory ..

 

source somesc.sh

7. wildcards

command doesn't see the arguments until after the shell has expanded the wildcards, so you can use wildcards to generate arguments automatically. for example:

you can type "diff source*" instead of typing "diff source.cpp source.cpp.old" if there are only the two files match the "source*"

 

8. special characters within shell command lines only

Character

Meaning

~

Home directory

`

Command substitution (archaic)

#

Comment

$

Variable expression

&

Background job

*

String wildcard

(

Start subshell

)

End subshell

/

Quote next character

|

Pipe

[

Start character-set wildcard

]

End character-set wildcard

{

Start command block

}

End command block

;

Shell command separator

`

Strong quote

<">

Weak quote

<

Input redirect

>

Output redirect

/

Pathname directory separator

?

Single-character wildcard

!

Pipeline logical NOT

 

9. Quoting and Escape

  • $ echo '2 * 3 > 5' is a valid inequality.

  • $ echo 2 /* 3 /> 5 is a valid inequality.

  • $ echo /"2 /* 3 /> 5/" is a valid inequality

  • $ echo 'Hatter'/''s tea party'

  • To use a literal backslash, just surround it with quotes ('/') or, even better, backslash-escape it (//).

 

10. Define variables to be in the environment of a particular subprocess (command) only

by preceding the command with the variable assignment, like this: varname=value command

You can put as many assignments before the command as you want. For example, assume that you're using the emacs editor. You are having problems getting it to work with your terminal, so you're experimenting with different values of TERM. You can do this most easily by entering commands that look like(There is an obscure option, set -k, that lets you put this type of environment variable definition anywhere on the command line, not just at the beginning.):

TERM=trythisone  emacs  filename

 

11. Backgrounding/Foregrounding a process

You can suspend the process running in the foreground by holding down the [control] key and typing [z] (written as ^Z) Then to put it in the background, type

bg

To restart (foreground) a suspended processes, type

fg %jobnumber

 

12. Recall History

you can use the exclamation character (!) to recall commands easily.

% !! (recall last command)

% !-3 (recall third most recent command)

% !5 (recall 5th command in list)

% !grep (recall last command starting with grep)

 

13. Find a file

find / -name “netscape” 

以上命令从根目录“/”开始查找文件名叫“netscape”的程序。用这个命令可能速度更快一些: 

locate netscape 

locate命令运行速度更快是因为它依赖于系统预先建立的文件数据库。这个数据库由一个在背景运行的程序cron来建立。因为cron一般安排在夜间运行,所有如果你经常在夜晚关机,就不要依赖locate可以找到你要的文件,也不要巴望locate可以找到刚刚才安装的软件名称

如果你知道程序的名字但是不知道程序的全路径名,可以用以下命令: 

which  netscape 

就可以找到所有叫做netscape的文件的全路径名

whereis netscape 

 

显示"netscape"命令的二进制文件, 源文件和手册的位置.

 

14. Replace

cat file1 | tr oldstr newstr > file2

for dirs in $(echo $PATH | tr : "/n"); do ls -l $dirs; done

echo -e ${PATH//:/'/n'}

 

15. X Server

export DISPLAY=198.168.0.5:0.0

xhost 192.168.0.7

 

16. 删除所有指定名称的进程(Depend on output format of "ps")

for proc in $( ps | grep "${1:-notexit}" | cut -f1 -d' ' ); do kill -9 $proc; done

 

17. 创建符号链接,symbol link

for file in $(ls /some/folder/)
do
    ln -s /some/folder/$file $file
done
 

18. 在某个目录及其所属子目录的所有文件中查找字符串

在程序维护过程中,有时需要在某个目录及其所属子目录的所有文件中查找某一个字符串,为此可用下面两种方法(假设在*.cp文件中查找字符串"abc",结果放在文件out中):
    1. cat /dev/null > out

       find ./ -name "*.cp" -exec grep "abc"{} >> out

    2. find ./ -name "*.cp" | xargs grep "abc" > out

推荐使用第二种方法,因其系统开销小、速度快。

 

V. Script

1. A local statement inside a function definition makes the variables involved all become local to that function.

2. Exit Status: Every UNIX command, whether it comes from source code in C, some other language, or a shell script/function, returns an integer code to its calling process—the shell in this case—when it finishes. This is called the exit status. 0 is usually the OK exit status, while anything else (1 to 255) usually denotes an error.

using return N or exit N to return status from function.

3. Logical Operators: &&, ||, !

4. You can also use the special indices @ and *. These return all of the values in the array and work in the same way as for the positional parameters; when the array reference is within double quotes, using * expands the reference to one word consisting of all the values in the array separated by the first character of the IFS variable, while @ expands the values in the array to separate words. When unquoted, both of them expand the values of the array to separate words.

5. Putting the I/O redirector at the end of the function ,or the loop, like this:

while read dev termtype; do    if [ $dev = $line ]; then        TERM=$termtype        break;    fidone < /etc/terms

You can use this technique with any flow-control construct, including if...fi, case...esac, select...done, and until...done. This makes sense because these are all compound statements that the shell treats as single commands for these purposes.

But if you want to redirect I/O to or from an arbitrary group of commands without creating a separate process, you need to use command block. If you surround some code with { and }, the code will behave like a function that has no name. This is another type of compound statement.

6. Expand Variable

built-in: ${!varname}

simulate:  expvar(){ eval rt="/$""$1"; eval r="/$""$rt"; echo $r; }

 

VI. Principle

1. When in doubt, use single quotes, unless the string contains variables or command substitutions, in which case use double quotes.

 
原创粉丝点击