Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2395554
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类:

2010-09-24 12:59:56


本篇包含命令:
sort file uniq cut tee basename dirname diff col


fdisk        分区
alias        别名化命令

别名化命令                    alias timedir="ls  -art"

shell        shell 控制命令


删除一个用户组时更改所有文件的组属性:
find / -gid -exec chgrp {} \;

分区:fdisk -- 创建分区;设定分区类型:83为linux,82为交换分区
    建文件系统:mkfs -t ext2 /dev/hda1
    建交换分区:mkswap -c /dev/hda2 ;swapon /dev/hda2

/dev/console
/dev/tty
/dev/null



测试一个shell文件,并看到它运行的所有步骤    sh  -x  script-name

shell 命令:
    read命令获得下一行的输入,并把它赋给变量        read  filename

使用命令行参数:    $0代表命令名称;$*代表所有参数列表;$#代表命令行参数的个数

命令行返回值:    也就是命令执行的返回值,同时$?保存命令返回值,0代表成功。   

case 循环判断结构
    case  $ver  in
        pattern|pattern|*  )    statements;;
    esac

if判断结构
    if    命令组(看最后一个的状态)
    then    命令组
    else    命令组
    fi

使用test命令        检查文件状态、用户权限、变量性质判断;! 用于取反
            - f | d | r | s | w | x
            - eq | ne | ge | gt | le | lt

for循环结构
    for i in
    do    作用于$i的命令组
    done

while循环
    while     命令组
    do    命令组
    done

新shell的运行
    无论何时发出一个命令,一个新的shell就启动了;它集成父shell的许多特性和环境
    export 变量    把一个shell输出或传递到子shell序列中。
   
多用户和多任务命令
    at    给定时间执行
    batch    当系统负载允许时执行
    cron    执行已安排好的命令
    crontab    单个用户
    kill    终止进程
    nice    在进程启动前调整它的优先级
    nohup    用户退出进程仍然执行
    ps    显示进程信息
    renice    调整正在运行的进程优先级
    w    显示谁已登录和他们正在做什么
    who    显示系统的登录用户

kill -9

861



kill -l        列出有效的信号
当用户推出时,Linux把一个挂起信号(信号值为1)发送给shell启动的所有后台进程。该信号终止那些进程,除非是nohup启动的

默认时,kill的信号是15,信号9为无条件撤销信号。(9立即杀死进程,进程无法完成一些动作会有副作用)

kill 0        为终止所有的后台作业

vi
    a添加光标后;i添加光标前
    w file_name            存文件
   
 
   watch命令,可以反复执行某个命令。常用于监测文件变化
[macg@machome ~]$ watch ls -l test
Every 2.0s: ls -l test                                                Sun Jul 16 21:53:59 2006
-rw-rw-r--  1 macg macg 1558 Jul 16 21:52 test


    whereis
[root@localhost macg]# whereis fdisk
fdisk: /sbin/fdisk /usr/share/man/man8/fdisk.8.gz


    r指令不跟任何参数,是执行上一个指令
$ls -l
$r
ls -l


    当你不确定command的具体拼写时,也可用man -k
$ man -k whoam
whoami          whoami (1b)     - display the effective current username
/usr/openwin/share/man/windex: No such file or directory
whoami          whoami (1b)     - display the effective current username

   clear 清屏
[macg@localhost tiptest]$ clear

   虚拟控制台 (telnet用不到)
Alt+F1~Alt+F4来访问

     Ctrl+Alt+F1三个键,从图形界面进入字符界面
     ctrl+alt+backspace,从图形界面退出到字符界面

    startx  手动起xwin
startx


    两个xwin下图形化应用
Clock& 
直接起时钟,图形化
admintool& 
图形化管理


   可以将多个指令用";"分开同时执行.如
#ls -l;uname -a;more hosts


    指令换行用\
常用于./configure这种参数很多的命令行,不同参数不同行
$./configure \
 --prefix=/usr/local/apache2 \
 --enable-so \
  --enable-deflate \
  --enable-ssl \
 --with-ssl=/usr/local \
 --enable-headers


    指令里也可带pattern
[root@demo1 tftpboot]# umount /mnt/disk{1,2,3,4,5}

[root@demo1 tftpboot]# mkdir -p /mnt/disk{1,2,3,4,5}


   管道 "|" 后面的指令,取前面指令的输出做为其最后一个参数
下面的指令是等价的
netstat -nr | grep 192.168
netstat –nr > tmp
grep 192.168 tmp
grep "hello" file.txt | wc -l grep "hello" file.txt> tmp
wc -l tmp


   三层管道过滤命令 ,理解成一层一层向后送(从左向右) 
rpm –qa | grep ed | more   


    管道符号的缺陷-----后面的命令只能把前面命令的输出当作其“文件”参数,即后面的命令只能是文件处理命令如more,sort,grep等
如何使管道后面的非文件型命令把前面命令的输出当作自己的输入参数?xargs弥补了管道符号的缺陷.
比如下面,管道命令没起作用,就是单纯的ls -l
$find /export/home/macg -name "*.tar" | ls -l
total 5956
-rw-r--r--   1 macg     other    1356798 Mar  2 03:18 119254-34.zip
-rw-r--r--   1 macg     other      81408 Feb  1999 data.tar
drwxr-xr-x   7 macg     other       1024 Dec 28 22:33 mysqltmp
-rw-r--r--   1 macg     other       3294 Feb  1999 test3.c
$find /export/home/macg -name "*.tar" | xargs ls -l
-rw-r--r--   1 macg     other      81408 Feb  1999 /export/home/macg/data.tar
-rw-r--r--   1 macg     other      81408 Feb  1999 /export/home/macg/www/data.tar
-rw-r--r--   1 macg     other     122880 Feb 26 23:00 /export/home/macg/www/test.tar
find 到文件并且用file显示每个文件的类型
$find /export/home/macg -name "*.tar" | xargs file
/export/home/macg/data.tar:     USTAR tar archive
/export/home/macg/www/data.tar: USTAR tar archive
/export/home/macg/www/test.tar: USTAR tar archive



    标准文件0,1,2
  •    stdin 标准输入,文件描述符为0
  •    stdout 标准输出,文件描述符为1
  •    stderr 标准错误输出,文件描述符为2


   command  1>file1  2>file2
执行一个命令
将stdout(1)输出到file1
将stderr(2)输出到file2


    command  1>file1  2>&1
将stdout(1),stderr(2)都输出到file1

    引用标准I/O文件,要加&
>&1

   /dev/null 是一个特殊文件,所有输入到该文件的数据都会被丢弃
> /dev/null 的意思就是丢弃所有的输出内容,
用处在于,一些自动运行的程序,比如cron,不带> /dev/null ,会将输出发送到用户信箱中(send mail)

    > /dev/null 2>&1      最彻底的输出关闭

    一种全新的输出重定向(tail –f  file+ command >file)
 [macg@localhost tiptest]$ tail -f output &
[1] 4068
必须运行在后台,否则系统会“等待”
[macg@localhost tiptest]$ ls -l >> output 执行命令,其输出重定向到前面的文件
此时系统不回显命令输出
[macg@localhost tiptest]$ total 1304
-rw-rw-r--  1 macg macg     108 Jan 20 02:21 111-tmp.txt
-rw-rw-r--  1 macg macg 1269764 Jan  3 11:24 asian_0264_4.mpg
-rw-rw-r--  1 macg macg     175 Jan 21 00:51 autologin.sh
-rw-rw-r--  1 macg macg     156 Jan 20 10:05 in.sh
-rw-rw-r--  1 macg macg       0 Jan 21 01:32 output
-rw-rw-r--  1 macg macg     365 Dec 22  2006 testm.c
-rw-rw-r--  1 macg macg     156 Dec  2006 tt.c
-rw-rw-r--  1 macg macg     173 Jan 20 03:07 ttt1
[macg@localhost tiptest]$
 但后台运行的tail –f output回显命令输出

         
    重定向追加?
ps -ef > temp.log
ls -l > temp.log
overwrite   写入文件并覆盖旧文件
>>
[mac@machome ~]$ more test.sh
expr 3 '+' 2 
echo $?
[mac@machome ~]$ echo echo hello >> test.sh
[mac@machome ~]$ more test.sh
expr 3 '+' 2 
echo $?
echo hello
append      加到文件的尾部,保留旧文件内容


 
sort   对文件中的行按字母重新进行排序(显然只能针对文本文件)
$ more test.sh
expr 3 '+' 2  
echo $?
$ sort test.sh
echo $?         重新排序
expr 3 '+' 2    重新排序


    sort –n 按数字排序
尽管也能用sort字母法排数字,但还是建议用sort –n排数字,因为字母排数字,是一个一个字符排的,会出现"99">"101"的错误
# ls -l | gawk '{print $5,$9}'|sort
107 checkstring.c
11147 tmp
240 tiptest.c
255 testtmp.tmp
289 testtmp
3301 file.c
3648 tmp.c
4715 checkstring
4823 tiptest
54 Makefile            sort 比较的是首数字(首字母),所以出现54反而大于4823的错误
7852 tmp.o   

# ls -l | gawk '{print $5,$9}'|sort -n
54 Makefile            sort -n 才是真正的按整个数字排         
107 checkstring.c
240 tiptest.c
255 testtmp.tmp
289 testtmp
3301 file.c
3648 tmp.c
4715 checkstring
4823 tiptest
7852 tmp.o
11147 tmp   

   sort是行排序,sort +num是列排序
·  sort +num                    对第几列(从0开始,以空格分列)按字母排
[macg@machome]:/export/home/macg>$ls -l | sort +8
total 5956
                          8
-rw-r--r--   1 macg     other    1356798 Mar  2 03:18 119254-34.zip
drwxr-xr-x   7 macg     other       1024 Dec 28 22:33 mysqltmp
drwxr-xr-x   4 macg     other        512 Dec  3 23:15 shellsample
drwxr-xr-x   2 macg     other        512 Dec 25 17:24 temp
drwxr-xr-x   4 macg     other        512 Dec 21 13:40 test
drwxr-xr-x   5 macg     other       1536 Feb 26 23:00 www
                                                                              
·   sort +num  -n        对第几列(从0开始)按数字大小排------只对数字列有用
$ ls -l | sort +4 -n 
              4
-rw-rw-r--  1 macg macg     777 Jan 20  1999 expectsample.tar.gz
-rw-rw-r--  1 macg macg   10240 Dec 15 20:08 tiptest.tar
-rw-rw-r--  1 macg macg   30720 Dec 15 20:08 bigtest.tar
-rw-rw-r--  1 macg macg   81920 Dec 15 20:09 shellsample.tar
 

如何解决ls –l这种非字母、数字打头行的排列
-rwxr-xr-x   1 root root     538 Apr 25  2005 auto.smb
·法一。sort与awk合用
# ls -l | gawk '{print $5,$9}'|sort –n
·法二。sort +num 
以第num个分隔符的区域开始或结束排序。默认的分割符是空格


    sort -r  sort –nr 倒序,翻转排序的顺序
$ ls -l | sort +4 -nr
-rw-rw-r--  1 macg macg 1576448 Feb  1999 mysqltmp.tar
-rw-rw-r--  1 macg macg   81920 Dec 15 20:09 shellsample.tar
-rw-r--r--  1 macg macg   81408 Feb  1999 data.tar
-rw-rw-r--  1 macg macg   51200 Jan 20  1999 filetmp.tar
-rw-rw-r--  1 macg macg   30720 Dec 15 20:08 bigtest.tar
-rw-rw-r--  1 macg macg   10240 Dec 15 20:08 tiptest.tar
-rw-rw-r--  1 macg macg     777 Jan 20  1999 expectsample.tar.gz

    sort -f 在排列中忽略大小写

   cut -cnum1-num2 filename
说明:显示每行从开头(位置0)算起 num1 到 num2 的文字
[root@nm testsh]# cat test.txt
test2
this is test1
[root@nm testsh]# cut -c0-2 test.txt
te
th

    wc      文件输出统计
$ ls -l | wc -l                      计算行数,一般用于编程
8

wc -l 统计行数
wc -w 统计单词数
wc -c 统计字节数
wc -m 统计字符数


    uniq 移除文件中重复的行
此功能对编辑一些/etc下文件很有用(如编辑/etc/hosts)

[root@nm testsh]# cat test.txt
test2
this is test1
test2
test2
this is test1
this is test1
test2
test2
this is test1
this is test1
test2
this is test1

[root@nm testsh]# uniq test.txt
test2
this is test1
test2
this is test1
test2
this is test1
test2
this is test1

    这指令不如Sed,awk好用



  cat 可跟多个文件参数,显示多个文件(且连续)
[root@nm mac]# cat host_tfn gogo
10.4.3.117
./tfn -f host_tfn -i 10.4.3.119 -c 8



   通过cat 显示多个文件的特性进行文件合并或合并同类项
  •     最普通的文件合并
[root@nm testtip]# cat t2
11111
[root@nm testtip]# cat t1 t2 > 1.txt
11111
2222
3333
4444
65555511111
11111
  •     文件合并,并且删除重复行(uniq 重复的行只保留一份)
这功能非常有用,既可实现合并,又可避免重复
[root@nm testtip]# cat t1 t2 | sort | uniq
11111
2222
3333
4444
65555511111
注意sort的作用:因为uniq只针对相临且相同的行做操作,所以必须用sort
换言之,uniq对不相临但相同的行,不认为是两个相同的行
  •   与上面相反,两个文件合并,只保留在两个文件中都有的行(uniq -d),但也是只保留一份
[root@nm testtip]# cat t1 t2 | sort | uniq -d
11111
  •     两个文件合并,删除交集(uniq -u),留下其他的行
[root@nm testtip]# cat t1 t2 | sort | uniq -u
2222
3333
4444
65555511111



   tee -------相当于read 和echo > file的组合:提供从键盘读入文件的功能
从STDIN读一段字符串,写入文件中
[root@nm testsh]# tee test.txt
hello            键盘输入
hello            屏幕回显

[root@nm testsh]# cat test.txt
hello            证明写入文件中

[root@nm testsh]# tee -a test.txt      a:append追加入文件
i m mac
i m mac
who are you
who are you

[root@nm testsh]# cat test.txt
hello

i m mac
who are you   


   file命令----得到文件类型的描述
$ file hosts
hosts: ASCII English text
$ file mail
mail: directory
$ file named.conf
named.conf: broken symbolic link to `/var/named/chroot/etc/named.conf'
$ file telnet-server-0.17-35.i386.rpm
telnet-server-0.17-35.i386.rpm: RPM v3 bin i386 telnet-server-0.17-35


   basename and dirname,一般用于编
basename file: 返回不包含路径的单一文件名
[mac@machome ~]$ basename /etc/init.d/portmap
portmap
dirname file: 返回文件所在路径
 [mac@machome ~]$ dirname /etc/init.d/portmap
/etc/init.d


   diff        文件比对
diff 命令很简单,就是一个普通的UNIX 文件指令,用于比较两个不同的文件
[root@macvm]# diff -c go1.txt go2.txt
*** go1.txt     2007-06-20 15:35:16.000000000 +0800
--- go2.txt     2007-06-20 15:35:34.000000000 +0800
***************
*** 1,3 ****
  1
  2
! 3
--- 1,3 ----
  1
  2
! 4444
此命令经常用于source code 升级


col -b 用于将man一类有pause屏的文本连续起来(去掉pause)
[root@nm mac]# man cp | col -b > cp.man








阅读(1916) | 评论(0) | 转发(0) |
0

上一篇:Perl 函数集

下一篇:CVS 分支管理

给主人留下些什么吧!~~