1、linux常用命令积累对文件内容的第三个域进行倒叙排序,域之间以“:”隔开sort -t: -r +2 index.jsp对文件内容的第一个域进行排序,域之间以“:”隔开sort -t: index.jsp对文件内容的第二个域进行数值排序,域之间以“:”隔开sort -t: +1n index.jsp对文件内容的第二个域进行数值排序,域之间以“:”隔开,去掉重复行sort -t: -u +1 index.jsp对文件内容先对第二个域进行排序在对第一个域进行,域之间以“:”隔开sort -t: -k2 -k1 index.jsp该命令意即开始以域0分类,忽略域2,然后再使用域3分类。Sort +
2、0 -1 +2以第1域最左边第3个字符开始分类Sort t: +1.2 index.jsp安第二个域分类,倒叙排序,并只显示第一条数据sort -t: -r +1 index.jsp |head -1安第二个域分类,倒叙排序,并只显示最后一条数据sort -t: +1 index.jsp |tail -1输出index.jsp文件的最后一行tail -1 index.jsp输出index.jsp文件的前两行Head -2 index.jsp安第二个域分类,只显示第一条数据,并且只显示这条数据中的第二个域sort -t: -k2 index.jsp |tail -1|awk -F: print
3、this is last $2linux查看ip信息,必须是root帐号Ifconfig去掉index.jsp文件中连续的出现的信息uniq index.jsp去掉index.jsp文件中连续的出现的信息,并显示重复的行数Uniq -c index.jsp去掉index.jsp文件中连续的出现的信息,只显示有重复出现的行,并显示重复的行数Uniq c -d index.jsp连接两个文件,以某个关键域作为连接条件join -j1 1 -j2 1 index.jsp sort.logjoin -e 1.1,2.1 index.jsp sort.log显示index.jsp文件中以“:”隔开的前三
4、个域cut -d: -f1,3 index.jsp显示index.jsp文件中的每一行的前6个字符cut -c1-6 index.jsp将文件index.jsp以1000字节split -b 1000 index.jsp-将文件index.jsp文件中的字符”a”换成“b”tr s a “b”test.txt-删除index.jsp文件中的a字符tr d a test.txt-文件index.jsp大写到小写转换tr a-z A-Z &2; exit 1; else echo yes,you are the root &1 exit 1;fi将参数传给系统命令DIRECTORY=$1;if t
5、est -z ls -a $DIRECTORY then echo the $DIRECTORY is EMPTY else echo the $DIRECTORY is not emptyfi按照输入的参数建立目录,如果目录存在则推出,如果目录不存在则提示用户是否建立,键入“y”则建立文件夹DIRECTORY=$1echo test=if $DIRECTORY = then echo error you must input the parameter ,Basename $0 &2; exit 1;fiecho $DIRECTORY =if -d $DIRECTORY then echo
6、the $DIRECTORY is exsit! : # do nothing else echo The directory does not exsit echo -n Create it now Y.N read ANS if $ANS = y | $ANS = Y then echo Creating now ; mkdir $DIRECTORY /home/jimmy/ly.log 2&1; if test $? != 0 then echo create dir $DIRECTORY error&2; exit 1; fi else : # do nothing fifi提示用户是
7、否要改变数据。如果是则提示输入数字,然后将输入的数字相加,最后返回COUNT=100echo do you want to change the $COUNTread tempif $temp = y | $temp = Y then echo enter the value read value expr $COUNT + 100 ./ly.log 2&1 Status=$? if $value = | $Status != 0 then echo there is some error ./ly.log 2&1 exit 1; fi COUNT=expr $COUNT + $value e
8、cho Now the count value is : $COUNTfi输入用户和密码登陆脚本。输入密码是不显示在屏幕上IVALID_NAME=yesIVALID_PASS=yes#将终端信息保存在变量中SAVEDSTTY=stty -gecho you will logging into sensitive area:echo -n please enter the username:read username#将终端的echo属性输入不显示在屏幕上stty -echoecho -n enter the password:read password#将终端的所有属性还原stty $SAVE
9、DSTTYif $username = lzw -o $username = ly then IVALID_NAME=nofiif $password = lzw then IVALID_PASS=nofiecho =$IVALID_NAME = $IVALID_PASSif $IVALID_NAME = yes -o $IVALID_PASS = yes then if $IVALID_NAME = yes then echo the username is undifined,you can not logging this area else if $IVALID_PASS = yes
10、then echo the password is not right,you can not logging this area fi fificase语句,匹配到后立即跳出case语句echo -nplease enter the number:read numcase $num in1) echo you selected 1: ;2) echo you selected 2: ;3) echo you selected 3: ;4) echo you selected 4: ;5) echo you selected 5: ;6) echo you selected 6: ;7) ec
11、ho you selected 7: ;8|11) echo you selected 8: ;91) echo you selected 91: ;匹配任意单字字符?)echo you input is not number is : $num;匹配任意字符*) echo you selected *: ;Esaccase语句参数传递if $# != 1 then echo the parameter count must only one: exit 1;fiparm=$1;case $parm instart|START) echo the programe is starting.;s
12、top|STOP) echo the programe is stoping.;*) echo the parameter is wrong.;Esac缺省值,如果输入信息只是回车换行,那么就取缺省值echo please input you name:read nameecho =$name=echo you name is .$name:=lyecho =$name=for循环echo this is loop programefor loop in 1 2 3 4 5 6 do echo $loopdonefor循环可用用字符串echo this is loop programefor
13、loop in jimmy ly lzw lzwly 5 6 do echo $loopdone用ls作为循环条件echo this is loop programefor loop in ls -a do echo $loopdonefor循环使用参数,将所有参数作为一个条件。循环一次echo this is loop programefor loop in $* do echo $loopdonefor循环使用参数,将每个参数作为循环条件,每个参数都循环一次echo this is loop programefor loop in $ do echo $loopdone用for循环ping
14、机器echo this is loop programefor loop in 172.16.1.2 172.16.1.45 172.16.1.9 172.16.1.1 do ping -c 2 $loopdonefor循环备份文件echo this is loop programefor loop in ls do if -f $loop then cp $loop $loop.bak fidone循环将所有文件中的小写换成大写字母echo this is loop programefor loop in ls do if -f $loop then cat $loop | tr a-z A
15、-Z|tee -a ./log.log fidoneuntil用法,直到用户输入自己的名字后退出echo please enter you name:read nameuntil $name #判断name是否为空do echo please enter you name: read namedone判断文件是否存在。如果存在则一直循环。知道删除了the_file=/home/jimmy/log.loguntil ! -f $the_file do sleep 1 echo the file is exsiteddoneecho the file is deletedwhile循环count=
16、1while $count -lt 1000000 do echo =$count count=expr $count + 1done将文件ping.txt中的内容循环读入,并以“。”分开显示,IFS 用来确定命令中什么样的分隔词被发送到原始的 Unix Bourne shell,并与其他环境变量一样被传递下去files=$IFSIFS=.while read NAME DEPT ID do echo $NAME t $DEPT t $ID t = done./ping.txtIFS=$files根据文件的值控制流files=$IFSIFS=.while read NAME DEPT ID d
17、o if $NAME = jimmy then echo the name is :$NAME exit 0 fi echo $NAME t $DEPT t $ID t = done./ping.txtIFS=$files循环读入数据,直到输入exit时才退出echo please enter the value:while read valuedo if $value = exit then exit 1 fi echo the value is $valuedone循环读入文件,以行的方式读入count=1while read aaaado echo $aaaa = $count coun
18、t=expr $count + 1done./ping.txtlinux启动的时候自动启动一些服务配置在/etc/rc.d/rc.local的最后添加要启动的服务即可/sbin/service vsftpd start/sbin/service crond startexecexec 命令:常用来替代当前 shell 并重新启动一个 shell,换句话说,并没有启动子 shell。使用这一命令时任何现有环境都将会被清除,。exec 在对文件描述符进行操作的时候,也只有在这时,exec 不会覆盖你当前的 shell 环境IO详解I/O重定向 详解及例子 1、基本概念a、I/O重定向通常与 FD有
19、关,shell的FD通常为10个,即 09;b、常用FD有3个,为0(stdin,标准输入)、1(stdout,标准输出)、2(stderr,标准错误输出),默认与keyboard、monitor、monitor有关;c、用 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案;e、0 是 的默认值,因此 与 0 与 1 是一样的;f、在IO重定向 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料;g、管道“|”(pipe line):上一个命令的 stdout 接到下一个命令的 stdin;h、tee 命令是在不影响原本 I/O 的情况
20、下,将 stdout 复制一份到档案去;i、bash(ksh)执行命令的过程:分析命令变量求值命令替代(和$( ))重定向通配符展开确定路径执行命令;j、( ) 将 command group 置于 sub-shell 去执行,也称 nested sub-shell,它有一点非常重要的特性是:继承父shell的Standard input, output, and error plus anyother open file descriptors。k、exec 命令:常用来替代当前 shell 并重新启动一个 shell,换句话说,并没有启动子 shell。使用这一命令时任何现有环境都将会被清
21、除,。exec 在对文件描述符进行操作的时候,也只有在这时,exec 不会覆盖你当前的 shell 环境。2、基本IOcmd file 把 stdout 重定向到 file 文件中cmd file 把 stdout 重定向到 file 文件中(追加)cmd 1 fiel 把 stdout 重定向到 file 文件中cmd file 2&1 把 stdout 和 stderr 一起重定向到 file 文件中cmd 2 file 把 stderr 重定向到 file 文件中cmd 2 file 把 stderr 重定向到 file 文件中(追加)cmd file 2&1 把 stderr 和 st
22、derr 一起重定向到 file 文件中cmd file2 cmd 命令以 file 文件作为 stdin,以 file2文件作为 stdoutcat file 以读写的方式打开 filecmd file cmd 命令以 file 文件作为 stdincmd &n 使用系统调用 dup (2) 复制文件描述符 n 并把结果用作标准输出&n 标准输入复制自文件描述符 n&- 关闭标准输出n&- 表示将 n 号输出关闭上述所有形式都可以前导一个数字,此时建立的文件描述符由这个数字指定而不是缺省的 0 或 1。如:. 2file 运行一个命令并把错误输出(文件描述符 2)定向到 file。. 2&1 运行一个命令并把它的标准输出和输出合并。(严格的说是通过复制文件描述符 1 来建立文件描述符 2 ,但效果通常是合并了两个流。)2
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1