Shell编程

来源:互联网 发布:java input赋值 编辑:程序博客网 时间:2024/05/20 00:37

Shell编程

A>.shell中if比较的用法:

比较两个字符串是否相等:

if [ "$test"x = "test"x ]; then

    关键点:

1 使用单个等号

     2 等号两边各有一个空格:这是unix shell的要求

     3 "$test"x最后的x,是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = testx,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected

二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.

    整数比较

    -eq 等于,如:if [ "$a" -eq "$b" ]

    -ne 不等于,如:if [ "$a" -ne "$b" ]

    -gt 大于,如:if [ "$a" -gt "$b" ]

    -ge 大于等于,如:if [ "$a" -ge "$b" ]

    -lt 小于,如:if [ "$a" -lt "$b" ]

    -le 小于等于,如:if [ "$a" -le "$b" ]

     大于(需要双括号),如:(("$a" > "$b"))

    >= 大于等于(需要双括号),如:(("$a" >= "$b"))

    小数据比较可使用AWK

    字符串比较

    = 等于,如:if [ "$a" = "$b" ]

    == 等于,如:if [ "$a" == "$b" ],与=等价

B>.shell中按行读取文件的用法:

shell脚本按行读取文件的方法有3种:
  1. #!/bin/bash  
  2.   
  3. echo "##### 方法 1 #####"  
  4. while read line1  
  5. do  
  6.     echo $line1  
  7. done < $1  
  8.   
  9. echo "##### 方法 2 #####"  
  10. cat $1 | while read line2  
  11. do  
  12.     echo $line2  
  13. done  
  14.   
  15. echo "##### 方法 3 #####"  
  16. for line3 in $(<$1)  
  17. do  
  18.     echo $line3  
  19. done  

运行结果
snail@ubuntu:5.read-line$ cat file.bin 
hello world
this is 1
this is 2
this is 3
snail@ubuntu:5.read-line$ ./read-line.sh file.bin 
##### 方法 1 #####
hello world
this is 1
this is 2
this is 3
##### 方法 2 #####
hello world
this is 1
this is 2
this is 3
##### 方法 3 #####
hello
world
this
is
1
this
is
2
this
is
3

<以上例程代码来自网络:此处>

请注意方法1和方法2均可以按行读取内容,而不需特别注意其他特定字符,而第三种方法除可以按行读取内容外,还会断开每行中所含有的空格,即以空格断开内容,使用时应留意,这也是在写脚本的过程中发现的哦,哈哈~

同时,也可以通过设置IFS值来实现方法3的按行读取,如:

  1. IFS=$'\n'  
  2.   
  3. echo "##### 方法 3 #####"  
  4. for line3 in $(<$1)  
  5. do  
  6.     echo $line3  
  7. done 

IFS的具体含义是shell常识,后面也会具体介绍.


C>.shell sed字符串操作

a 追加内容 用法:sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)
i 插入内容 用法:sed ‘/匹配词/i\要加入的内容’ example.file 将内容插入到匹配的行目标的上一行位置)
示例:

1mname=yangyangyang
mline=pipipi
#行前加
#在匹配[mline内容]的行前插入一行,内容为tab缩进的mline的值+换行+mname的值,相当于在匹配行前插入了两行内容;
sed -i "/$mline/i \    $mline\n\\$mname" $m1line.txt
2 
3#在匹配[mline内容]的行后插入一行,内容为mname的值;
sed -i "/$mline/a\\$mname" $mfile.txt
4 

D>.shell中的IFS 介绍

Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符。完整定义是The shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.

     Shell 的环境变量分为 set, env 两种,其中 set 变量可以通过 export 工具导入到 env 变量中。其中,set 是显示设置shell变量,仅在本 shell 中有效;env 是显示设置用户环境变量 ,仅在当前会话中有效。换句话说,set 变量里包含了 env 变量,但 set 变量不一定都是 env 变量。这两种变量不同之处在于变量的作用域不同。显然,env 变量的作用域要大些,它可以在 subshell 中使用。

     而 IFS 是一种 set 变量,当 shell 处理"命令替换"和"参数替换"时,shell 根据 IFS 的值,默认是 space, tab, newline 来拆解读入的变量,然后对特殊字符进行处理,最后重新组合赋值给该变量

查看变量 IFS 的值:

  1. $ echo $IFS  
  2.   
  3. $ echo "$IFS" | od -b  
  4. 0000000 040 011 012 012  
  5. 0000004 

直接输出IFS是看不到的,把它转化为二进制就可以看到了,"040"是空格,"011"是Tab,"012"是换行符"\n" 。最后一个 012 是因为 echo 默认是会换行的。

未完,不断更新中.....


0 0