Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263288
  • 博文数量: 74
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 21:01
文章分类

全部博文(74)

文章存档

2011年(1)

2010年(32)

2009年(32)

2008年(9)

我的朋友

分类: LINUX

2009-10-05 22:29:28

  • grep -o

man grep | less -p "-o"

       -o, --only-matching
              Print  only  the  matched  (non-empty) parts of a matching line,
              with each such part on a separate output line.
example:

ping 202.118.236.130 | grep -o "ttl=[0-9]*"

  • grep -F
  -F, --fixed-strings
              Interpret  PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by
              POSIX.)
exmaple:

  • #!/bin/sh -e
man sh | less -p "-e"
           -e errexit       If not interactive, exit immediately if any
                            untested command fails.  The exit status of a com‐
                            mand is considered to be explicitly tested if the
                            command is used to control an if, elif, while, or
                            until; or if the command is the left hand operand
                            of an “&&” or “||” operator.
example:

$ cat aaaaaa.sh
#!/bin/sh -e
cd /home/xulei/no-such-dir
echo "ok"
$ ./aaaaaa.sh
cd: 3: can't cd to /home/xulei/no-such-dir
$ cat aaaaaa.sh
#!/bin/sh
cd /home/xulei/no-such-dir
echo "ok"
$ ./aaaaaa.sh
cd: 3: can'
t cd to /home/xulei/no-such-dir
ok

  • mktemp mcookie
和随机数有关的两个命令
  • var1 = 123 # var1=123才行
变量赋值的时候=的左边和右边是不要空格的
  • ls my.file no.such.file &>/dev/null = ls my.file no.such.file 1>/dev/null 2>&1
  • set -o noclobber 不覆盖已存在的文件内容

$ ls 1.sh 1.sh

$ echo "aaa" > 1.sh

bash: 1.sh: cannot overwrite existing file

$ echo "aaa" >| 1.sh

$ cat 1.sh

aaa



那set +o noclobber 是什么意思呢?
  • tee
cm1 | cm2 | tee file | cm3
  • 在 IO Redirection 中,stdout和stderr会先准备好,然后才从stdin中读入数据
  • 在 pipe line 之间,前一个命令的 stderr 是不会进入下一命令的 stdin 的
  • $@ 和 $*的区别

若在 command line 上跑 my.sh p1 "p2 p3" p4 的话,
不管是 $@ 还是 $* ,都可得到 p1 p2 p3 p4 
但是,如果置于 soft quote 中的话:
"$@" 得到 "p1" "p2 p3" "p4" 这三个不同的词段(word)﹔
"$*" 则得到 "p1 p2 p3 p4" 这一整串单一的词段。

  • while :; do  中的" : "是 bash 的 null command ,不做任何动作,除了送回 true 这个 return value
  • $()=``命令嵌套,``这个"命令替换"命令是老UNIX机器上的shell语法,bash向后兼容了这种格式,但对它的支持依然含糊。在命令行处理的第7步──命令替换中,bash只对$()支持得好
  • 重定向中,严格来说,< 符号之前需要指定一个 fd 的(之间不能有空白),但因为 0 是 < 的预设值,因此 < 与 0< 是一样的﹗
    由于 1 是 > 的预设值,因此,1> 与 > 是相同的,都是改变 stdout
  • 在重定向中>>就是追加的意思,那 << 又是啥呢?這是所谓的 HERE Document ,它可以让我們输入一段文本,直到读到 << 后指定的字串
  • 正常来说,当我们执行一个 shell script 时,其实是先产生一个 sub-shell 的子进程,然后 sub-shell 再去产生命令行的子进程。
    所谓的 source 就是让 script 在当前 shell 內执行、而不是产生一个 sub-shell 来执行。
    exec 也是让 script 在同一个进程上执行,但是原有进程则被结束了。
  • {}与()的区别
( ) 是将 command group 置于 sub-shell 去执行,也称 nested sub-shell。
{ } 则是在同一个 shell 內完成,也称为 non-named command group。
  • cat /etc/resolv.conf 查看DNS
  • 查看系统版本信息
cat /etc/issue
cat /proc/version
cat /etc/debian_version
cat /etc/redhat-release
chattr +i filename
chattr a  filename
chattr t  filename
(i不可删除,a只可追加,t--粘行位 只有属主可以删除)
  • 删除文件中多余的空行
grep -v ^$ oldfile > newfile
cat file | tr -s ["\n"] > newfile
sed '/^$/d' filename
vim中,%s/^\n//g

  • eval和exec
eval: execute all shell replacement before issue the command.

~$ set a b c d
~$ eval echo The last argument is \$$#
The last argument is d
~$ echo The last argument is \$$#     
The last argument is $4


exec: open or close the standard input or output.

#!/bin/bash
exec < /tmp/file #open the /tmp/file
while read line;do
    echo ${line}
done

#will print file /tmp/file
if you want read from terminal after you close standard input you should specify the optional input
read answer < /dev/tty
  • watch -- 可以查看动态改变的信息
例如:
debian:~# watch -d "cat /proc/net/ip_conntrack | wc -l"
可以查看nat连接条数的动态变化情况



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

上一篇:shell一例

下一篇:Powerful CD Command Hacks

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