您的位置:首页 > 运维架构 > Shell

Shell 基础知识

2010-12-30 15:54 337 查看
1 通配符:

Wild card /Shorthand
Meaning
Examples
*
Matches any string or group of characters.$ ls *
will show all files
$ ls a*
will show all files whose first name is starting with letter 'a'
$ ls *.c
will show all files having extension .c
$ ls ut*.c
will show all files having extension .c but file name must begin with 'ut'.
?
Matches any single character.$ ls ?
will show all files whose names are 1 character long
$ ls fo?
will show all files whose names are 3 character long and file name begin with fo
[...]
Matches any one of the enclosed characters$ ls [abc]*
will show all files beginning with letters a,b,c
Note:

[..-..] A pair of characters separated by a minus sign denotes a range.

Example
:

$ ls /bin/[a-c]*

2:读入数据:

Use to get input (data from user) from keyboard and store (data) to variable.

Syntax:

read variable1, variable2,...variableN

例子:

$ vi sayH

#

#Script to read your name from key-board

#

echo "Your first name please:"

read fname

echo "Hello $fname, Lets be friend!"


netxu26:~ # chmod 755 sayH

netxu26:~ # . sayH

your first name please:

dgfg

hello dgfg,thanks

3 一行多个命令:

Syntax:

command1;command2

To run two command with one command line.

netxu26:~ # date;who

Thu Dec 30 12:58:14 EST 2010

root pts/0 Dec 30 11:07 (10.0.50.78)

root pts/1 Dec 30 12:35 (10.0.50.54)

注意:必须有分号

4 重定向:

三种重定向符号: >,>>,<

简单讲下 > >>区别:

> 将会创建新的文件,或者如果文件存在,则覆盖,以前文件的内容丢失

>>
将会创建新的文件,或者如果文件存在,则追加到文件的末尾

(1) > Redirector Symbol

Syntax:

Linux-command > filename

To output Linux-commands result (output of command or shell script) to
file. Note that if file already exist, it will be overwritten else new
file is created. For e.g. To send output of ls command give

$ ls > myfiles

Now if 'myfiles
' file exist in your current directory it will be overwritten without any type of warning.

(2) >> Redirector Symbol

Syntax:

Linux-command >> filename

To output Linux-commands result (output of command or shell script) to
END of file. Note that if file exist , it will be opened and new
information/data will be written to END of file, without losing previous
information/data, And if file is not exist, then new file is created.
For e.g. To send output of date command to already exist file give
command

$ date >> myfiles

(3) < Redirector Symbol

Syntax:

Linux-command < filename

To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give

$ cat < myfiles

(4) 管道 Pipe:

Pipe Defined as:

"A pipe is nothing but a
temporary storage place where the output of one command is stored and
then passed as the input for second command

. Pipes are used to run more
than two commands ( Multiple commands) from same command line.
"
Syntax:

command1 | command2
http://www.freeos.com/guides/lsst/images/pipe.gif
例子:
netxu26:~ # who | grep pts/0

root pts/0 Dec 30 11:07 (10.0.50.78)
(process):

Process defined as:

"A process is program (command given by user)
to perform specific Job. In Linux when you start process, it gives a
number to process (called PID or process-id), PID starts from 0 to
65535.
"

跟process 相关的命令:
For this purpose
Use this Command
Examples*
To see currently running process ps$ ps
To stop any process by PID i.e. to kill process

kill {PID}$ kill 1012
To stop processes by name i.e. to kill processkillall {Process-name}$ killall httpd
To get information about all running processps -ag$ ps -ag
To stop all process except your shellkill 0$ kill 0
For background processing (With &, use to put particular command and program in background)linux-command &$ ls / -R | wc -l &
To display the owner of the processes along with the processes
ps aux$ ps aux
To
see if a particular process is running or not. For this purpose you
have to use ps command in combination with the grep command
ps ax | grep process-U-want-to see

For e.g. you want to see whether Apache web server process is running or not then give command
$ ps ax | grep httpd

To see currently running processes and other information like memory and CPU usage with real time updates.top

See the output
of top command.


$ top



Note

that to exit from top command press q.
To display a tree of processespstree$ pstree
*
To run some of this command you need to be root or equivalnt user.

NOTE
that you can only kill process which are created by yourself. A
Administrator can almost kill 95-98% process. But some process can not
be killed, such as VDU Process.

对shell程序进行debug:
Syntax:
sh option { shell-script-name }
OR
bash option { shell-script-name }
Option can be
-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: