Jean - 记录成长历程

  • 关于
  • Language
  • RSS Feed
  • 微博
  • 豆瓣
24
Mar

shell中的判断

By admin|脚本编程|Be the first to comment!

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

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

?View Code SHELL
#!/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

在判断中使用正则

?View Code SHELL
    #!/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"

使用圆括号做运算判断

?View Code SHELL
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  文件可执行

无觅相关文章插件,快速提升流量

Tagged as: bash, shell
Add your comment →

0 Comments

Leave your comment below! Cancel Reply

点击取消回复

View More Posts:
  • ←
  • →

分类

  • Linux系统 (61)
  • Linux集群 (4)
  • Web服务器 (26)
  • 创业相关 (6)
  • 工作笔记 (34)
  • 我爱折腾 (16)
  • 日记 (46)
  • 网络 (3)
  • 脚本编程 (49)

最新日志

  • 使用w3 total cache给wordpress提速
  • 从零开始,我的创业感悟
  • 为大众创造更美好的日常生活
  • 我的站立式工作平台
  • 阿里云计算使用简评

最新评论

  • admin on 从零开始,我的创业感悟
  • Kovlala on 从零开始,我的创业感悟
  • jean on 从零开始,我的创业感悟
  • Kovlala on 从零开始,我的创业感悟
  • ElmaByrd35 on 解决Fedora 11中文字体难看问题

博客互联

  • HackLog
  • leapsoul
  • Myredhat
  • Ninth element
  • Ti’blog
  • William Hua
  • young001
  • 懒人运维
  • 曙光博客
  • 月夜
  • 月影
  • 杂货店一号铺
  • 王先进’s blog
  • 疯中舞步
  • 菜鸟的技术站
  • 运维进行时

技术

  • DBA BLOG
  • 回忆未来[张宴]
  • 扶凯
  • 王炜's Blog
  • 百变贝贝

Powered by the inLine Minimal WordPress Theme