Home

zhangyiqun

Thoughts, stories and ideas.

Research 2014年以前 スーパーマリオ 关于

24 Mar 2009
shell中的判断

2009-08-01 更名为shell中的判断,根据生产环境的脚本编程经验增加一些内容。

使用if判断进程是否存在,只是学个套路,实际上有简单的语句。

#!/bin/bash
remote=`ps aux | awk '$11 ~ /htt/{print $11}'|sed -n 2p`
if [ "X${remote}" != "X" ]
then
        echo "ok"
else
        echo "not ok"
fi

在判断中使用正则

#!/bin/bash
    # Scriptname: xcolors 

1   echo -n "Choose a foreground color for your xterm window: "
    read color
2   case "$color" in
3   [Bb]l??)
4        xterm -fg blue -fn terminal &
5        ;;
6   [Gg]ree*)
        xterm -fg darkgreen -fn terminal &
        ;;
7   red | orange)              # The vertical bar means "or"
        xterm -fg "$color"  -fn terminal &
        ;;
8   *)
        xterm -fn terminal
        ;;
9   esac
10  echo  "Out of case command"

使用圆括号做运算判断

1   $ x=2
    $ y=3 

2   (( x > 2 ))
    echo $?
    1 

3   (( x < 2 ))
    echo $?
    0 

4   (( x == 2 && y == 3 ))
    echo $?
    0 

5   (( x > 2 || y < 3 ))
    echo $?
    1

2009-03-24 这篇日志名为shell中的test,主要记录了使用test探测文件的方法。

测试操作符  测试结果为真时需满足的条件

–b filename  块专用文件

–c filename  字符专用文件

–d filename  目录存在

–e filename  文件存在

–f filename  普通文件存在且不是目录

–G filename  文件存在且属于有效组ID时为真

–g filename  Set–group–ID被设置

–k filename  Sticky位被设置

–L filename  文件是一个符号链接

–p filename  文件是一个命名管道

–O filename  文件存在且属于有效用户ID

–r filename  文件可读

–S filename  文件是一个socket

–s filename  文件大小非0

–t fd  如果fd(文件描述符)被一个终端打开则为真

–u filename  Set–user–ID位被设置

–w filename  文件可写

–x filename  文件可执行

Research 2014年以前 スーパーマリオ 关于