码迷,mamicode.com
首页 > 其他好文 > 详细

Shell命令

时间:2014-06-03 06:46:46      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   com   

1.read 从键盘读入数据,赋值给变量
    例如: read USERNAME    
2.sh -x shell脚本  可以查看脚本执行过程

3.expr 算数运算
    例如: eppr 3 + 5
        expr $var1 - 5
        expr $var1 / $var2  //取整小数点以后的省略
        expr $var3 \* 10
        expr `expr 5 + 7` / $var4
        var4=`expr $val1 / $var2`
4.test 变量测试  测试变量是否相等 是否为空 文件类型
    格式:test 测试条件
    例如:test str1=str2
        test str1!=str2
        test str1
        test -n str1
        test -z str1
        
        test -a file  是否存在
        test -d file 是否目录
        test -w file  是否可写
        
        test -eq 0
    语句:
        if test -d $1 then
            ...
        fi
    简化:
        test -d $1 等价于 [-d $1 ]
5.流程控制
    if 条件1 then
        命令1
    elif 条件2 then
        命令2
    else
        命令3
    fi
    
    -a 逻辑与 and
    -0 逻辑或 or
    exit 退出
    exit 0 正常退出
    exit 数字  返回错误码
6.循环
    for 变量 in 名字表
    do
        命令列表
    done

  实例:循环的时候累加

1
2
3
4
5
6
7
8
#!/bin/sh
num=0
cat a.c | while read line
do
 echo $line
num=`expr $num + 1`
done
echo $num

 

Shell命令,布布扣,bubuko.com

Shell命令

标签:c   class   blog   code   a   com   

原文地址:http://www.cnblogs.com/mr-amazing/p/3760823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!