shell学习记录---3

来源:互联网 发布:淘宝违禁品暗语 编辑:程序博客网 时间:2024/05/02 04:31



[root@localhost Exercise]# `echo "he">myfile.c`[root@localhost Exercise]# cat myfile.c he[root@localhost Exercise]# 
反引号当作命令;



[root@localhost Exercise]# echo "hello \n\n">myfile.c[root@localhost Exercise]# cat myfile.c hello \n\n[root@localhost Exercise]# echo -e "hello \n\n">myfile.c[root@localhost Exercise]# cat myfile.c hello [root@localhost Exercise]#

[root@localhost Exercise]# echo "hello ! "hello ! [root@localhost Exercise]# echo -n "hello ! "hello ! [root@localhost Exercise]# 

read:


#!/bin/bash#readecho -n "your name :"read nameecho -n "your age :"read ageecho -e "your name is ${name}"echo -e "your age is ${age}"

[root@localhost Exercise]# ./myshell5your name :yikaiyour age :24your name is yikaiyour age is 24[root@localhost Exercise]# 

[root@localhost Exercise]# cat /etc/inputrc  |more


[root@localhost Exercise]# less /etc/inputrc 




df -k查看磁盘空间:

[root@localhost Exercise]# df -kFilesystem           1K-blocks      Used Available Use% Mounted on/dev/mapper/VolGroup00-LogVol00                      14093368   3737332   9628588  28% //dev/sda1               101086     11769     84098  13% /boottmpfs                   257736         0    257736   0% /dev/shm.host:/              102399996  72787000  29612996  72% /mnt/hgfs[root@localhost Exercise]# df -k -lhFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol00                       14G  3.6G  9.2G  28% //dev/sda1              99M   12M   83M  13% /boottmpfs                 252M     0  252M   0% /dev/shm[root@localhost Exercise]# 



上一个的输出 作为下一个的输入:

[root@localhost Exercise]# df -k |awk '{print $1}'Filesystem/dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]# 
过滤掉Filesystem:
[root@localhost Exercise]# df -k |awk '{print $1}' |grep -v "Filesystem"/dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]#

[root@localhost Exercise]# my=3[root@localhost Exercise]# echo $my|awk '{print $1}'3[root@localhost Exercise]# 


grep -v:


[root@localhost Exercise]# lsmyfile.c  myfirstshell  myshell2.sh  myshell3  myshell4  myshell5[root@localhost Exercise]# ls -l|grep "myshell5"-rwxr-xr-x 1 root root 140 Nov 27 22:09 myshell5[root@localhost Exercise]# ls -l|grep -v "myshell5"total 24-rw-r--r-- 1 root root  12 Nov 27 21:56 myfile.c-rwxr-xr-x 1 root root 429 Nov 25 21:27 myfirstshell-rwxr-xr-x 1 root root  53 Nov 25 21:40 myshell2.sh-rwxr-xr-x 1 root root 570 Nov 27 20:08 myshell3-rwxr-xr-x 1 root root 619 Nov 27 20:48 myshell4[root@localhost Exercise]# 


加 -a 参数 文件不会覆盖;

[root@localhost Exercise]# whoroot     pts/1        2012-11-27 19:30 (:0.0)[root@localhost Exercise]# who |tee myfile.c root     pts/1        2012-11-27 19:30 (:0.0)[root@localhost Exercise]# cat myfile.c root     pts/1        2012-11-27 19:30 (:0.0)[root@localhost Exercise]# who |tee -a myfile.c root     pts/1        2012-11-27 19:30 (:0.0)[root@localhost Exercise]# cat myfile.c root     pts/1        2012-11-27 19:30 (:0.0)root     pts/1        2012-11-27 19:30 (:0.0)[root@localhost Exercise]# 

[root@localhost Exercise]# df -k |awk '{print $1}' |grep -v "Filesystem"|tee info.txt/dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]# cat info.txt /dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]# lsinfo.txt  myfile.c  myfirstshell  myshell2.sh  myshell3  myshell4  myshell5[root@localhost Exercise]# 




sort  排序:

[root@localhost Exercise]# cat info.txt /dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]# cat info.txt |sort >info2.txt[root@localhost Exercise]# cat info2.txt 14093368/dev/mapper/VolGroup00-LogVol00/dev/sda1.host:/tmpfs[root@localhost Exercise]#
>>  追加:
[root@localhost Exercise]# pwd/home/yikai/Exercise[root@localhost Exercise]# pwd >> info2.txt [root@localhost Exercise]# cat info2.txt 14093368/dev/mapper/VolGroup00-LogVol00/dev/sda1.host:/tmpfs/home/yikai/Exercise[root@localhost Exercise]# 
>  可以创建新文件:
[root@localhost Exercise]# > hello.txt[root@localhost Exercise]# ls -al hello.txt -rw-r--r-- 1 root root 0 Nov 28 11:28 hello.txt[root@localhost Exercise]#
输出到屏幕:
[root@localhost Exercise]# sort < info.txt 14093368/dev/mapper/VolGroup00-LogVol00/dev/sda1.host:/tmpfs[root@localhost Exercise]# 
屏幕输入到文件保存:
[root@localhost Exercise]# cat mytxt.txt 12[root@localhost Exercise]# cat >> mytxt.txt <<inputOver> hello I am using $TERM terminal> and my username is $LOGNAME> BYE!!!> inputOver[root@localhost Exercise]# cat mytxt.txt 12hello I am using xterm terminaland my username is rootBYE!!![root@localhost Exercise]# 
查找字符:
[root@localhost Exercise]# cat >> hello.txt  <<inputOver> Are you ok ?> Bye!!!> inputOver[root@localhost Exercise]# lserr.txt    info2.txt  myfile.c      myshell2.sh  myshell4  mytxt.txthello.txt  info.txt   myfirstshell  myshell3     myshell5[root@localhost Exercise]# grep "Are" hello.txt Are you ok ?[root@localhost Exercise]# 

将错误信息输出到文件:

[root@localhost Exercise]# grep "Are" helloll.txt 2>err.txt [root@localhost Exercise]# cat err.txt grep: helloll.txt: No such file or directory[root@localhost Exercise]# 
将两个文件合并  并打印错误信息到文本:
[root@localhost Exercise]# cat info.txt  info3.txt 1>infoall.txt 2> txt.err[root@localhost Exercise]# ls -l info3.txtls: info3.txt: No such file or directory[root@localhost Exercise]# cat txt.errcat: info3.txt: No such file or directory[root@localhost Exercise]# 
读取名称文件的一二行:
#!/bin/bash#execexec 3<&0 0<$1read line1read line2exec 0<&3echo $line1echo $line2

[root@localhost Exercise]# ./exec.txt myfirstshell #!/bin/bash#myfirstshell[root@localhost Exercise]#