Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1379620
  • 博文数量: 247
  • 博客积分: 10147
  • 博客等级: 上将
  • 技术积分: 2776
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-24 15:18
文章分类

全部博文(247)

文章存档

2013年(11)

2012年(3)

2011年(20)

2010年(35)

2009年(91)

2008年(87)

我的朋友

分类:

2010-09-26 19:44:14

处理方法(备查)

一、构造字符串
直接构造
STR_ZERO=hello
STR_FIRST="i am a string"
STR_SECOND='success'

重复多次
#repeat the first parm($1) by $2 times
strRepeat()
{
local x=$2
if [ "$x" == "" ]; then
x=0
fi

local STR_TEMP=""
while [ $x -ge 1 ];
do
STR_TEMP=`printf "%s%s" "$STR_TEMP" "$1"`
x=`expr $x - 1`
done
echo $STR_TEMP
}

举例:
STR_REPEAT=`strRepeat "$USER_NAME" 3`
echo "repeat = $STR_REPEAT"

二、赋值与拷贝
直接赋值
与构造字符串一样
USER_NAME=terry

从变量赋值
ALIASE_NAME=$USER_NAME

三、联接
直接联接两个字符串
STR_TEMP=`printf "%s%s" "$STR_ZERO" "$USER_NAME"`
使用printf可以进行更复杂的联接

四、求长
获取字符串变量的长度:${#string}

求字符数(char)
COUNT_CHAR=`echo "$STR_FIRST" | wc -m`
echo $COUNT_CHAR

求字节数(byte)
COUNT_BYTE=`echo "$STR_FIRST" | wc -c`
echo $COUNT_BYTE

求字数(word)
COUNT_WORD=`echo "$STR_FIRST" | wc -w`
echo $COUNT_WORD

五、比较
相等比较 str1 = str2
不等比较 str1 != str2

举例:
if [ "$USER_NAME" = "terry" ]; then
echo "I am terry"
fi

小于比较
#return 0 if the two string is equal, return 1 if $1 < $2, else 2strCompare() { local x=0 if [ "$1" != "$2" ]; then x=2 localTEMP=`printf "%s\n%s" "$1" "$2"` local TEMP2=`(echo "$1"; echo "$2") |sort` if [ "$TEMP" = "$TEMP2" ]; then x=1 fi fi echo $x }

六、
判空   -z str
判非空  -n str

是否为数字
# return 0 if the string is num, otherwise 1
strIsNum()
{
local RET=1
if [ -n "$1" ]; then
local STR_TEMP=`echo "$1" | sed 's/[0-9]//g'`
if [ -z "$STR_TEMP" ]; then
RET=0
fi
fi
echo $RET
}

举例:
if [ -n "$USER_NAME" ]; then
echo "my name is NOT empty"
fi

echo `strIsNum "9980"`

七、分割
以符号+为准,将字符分割为左右两部分
使用sed
举例:
命令 date --rfc-3339 seconds 的输出为
2007-04-14 15:09:47+08:00
取其+左边的部分
date --rfc-3339 seconds | sed 's/+[0-9][0-9]:[0-9][0-9]//g'
输出为
2007-04-14 15:09:47
取+右边的部分
date --rfc-3339 seconds | sed 's/.*+//g'
输出为
08:00

以空格为分割符的字符串分割
使用awk
举例:
STR_FRUIT="Banana 0.89 100"
取第3字段
echo $STR_FRUIT | awk '{ print $3; }'

八、子字符串
字符串1是否为字符串2的子字符串
# return 0 is $1 is substring of $2, otherwise 1
strIsSubstring()
{
local x=1
case "$2" in
*$1*) x=0;;
esac
echo $x
}

Shell字符串截取

一、 shell 截取字符变量的前8位,有方法如下:

1.expr substr “$a” 1 8
2.echo $a|awk ‘{print substr($0,1,8)}’
3.echo $a|cut -c1-8
4.echo $
5.expr $a : ‘\(.\\).*’
6.echo $a|dd bs=1 count=8 2>/dev/null

二、按指定的字符串截取
1、第一种方法:
  • ${varible##*string} 从左向右截取最后一个string后的字符串
  • ${varible#*string}从左向右截取第一个string后的字符串
  • ${varible%%string*}从右向左截取最后一个string后的字符串
  • ${varible%string*}从右向左截取第一个string后的字符串
“*”只是一个通配符可以不要

例子:
$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg

2、第二种方法:${varible:n1:n2}:截取变量varible从n1开始的n2个字符,组成一个子字符串。可以根据特定字符偏移和长度,使用另一种形式的变量扩展,来选择特定子字符串。试着在 bash 中输入以下行:
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:7}
abunga
这种形式的字符串截断非常简便,只需用冒号分开来指定起始字符和子字符串长度。

三、按照指定要求分割:
比如获取后缀名
ls -al | cut -d “.” -f2

shell (bash) 比较运算符

                                                                                        
运算符 描述 示例
文件比较运算符
-efilename 如果filename存在,则为真 [ -e /var/log/syslog ]
-dfilename 如果filename为目录,则为真 [ -d /tmp/mydir ]
-ffilename 如果filename为常规文件,则为真 [ -f /usr/bin/grep ]
-Lfilename 如果filename为符号链接,则为真 [ -L /usr/bin/grep ]
-rfilename 如果filename可读,则为真 [ -r /var/log/syslog ]
-wfilename 如果filename可写,则为真 [ -w /var/mytmp.txt ]
-xfilename 如果filename可执行,则为真 [ -L /usr/bin/grep ]
filename1-ntfilename2 如果filename1filename2新,则为真 [ /tmp/install/etc/services -nt /etc/services ]
filename1-otfilename2 如果filename1filename2旧,则为真 [ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符[size=-1](请注意引号的使用,这是防止空格扰乱代码的好方法)
-zstring 如果string长度为零,则为真 [ -z "$myvar" ]
-nstring 如果string长度非零,则为真 [ -n "$myvar" ]
string1=string2 如果string1string2相同,则为真 [ "$myvar" = "one two three" ]
string1!=string2 如果string1string2不同,则为真 [ "$myvar" != "one two three" ]
算术比较运算符
num1-eqnum2 等于 [ 3 -eq $mynum ]
num1-nenum2 不等于 [ 3 -ne $mynum ]
num1-ltnum2 小于 [ 3 -lt $mynum ]
num1-lenum2 小于或等于 [ 3 -le $mynum ]
num1-gtnum2 大于 [ 3 -gt $mynum ]
num1-genum2 大于或等于 [ 3 -ge $mynum ]

 

 

 

here string可以看成是here document的一种定制形式. 除了COMMAND <<<$WORD, 就什么都没有了, de>$WORDde>将被扩展并且被送入de>COMMANDde>的stdin中.

  1 String="This is a string of words."

3 read -r -a Words <<< "$String"
4 # "read"命令的-a选项
5 #+ 将会把结果值按顺序的分配给数组中的每一项.

7 echo "First word in String is: ${Words[0]}" # This
8 echo "Second word in String is: ${Words[1]}" # is
9 echo "Third word in String is: ${Words[2]}" # a
10 echo "Fourth word in String is: ${Words[3]}" # string
11 echo "Fifth word in String is: ${Words[4]}" # of
12 echo "Sixth word in String is: ${Words[5]}" # words.
13 echo "Seventh word in String is: ${Words[6]}" # (null)
14  # $String的结尾.
15 
16 # 感谢, Francisco Lobo的这个建议.


例子 17-13. 在一个文件的开头添加文本

  1 #!/bin/bash
2 # prepend.sh: 在文件的开头添加文本.
3 #
4 # Kenny Stauffer所捐助的脚本例子,
5 #+ 本文作者对这个脚本进行了少量修改.


8 E_NOSUCHFILE=65

10 read -p "File: " file # 'read'命令的-p参数用来显示提示符.
11 if [ ! -e "$file" ]
12 then # 如果这个文件不存在, 那就进来.
13  echo "File $file not found."
14  exit $E_NOSUCHFILE
15 fi
16 
17 read -p "Title: " title
18 cat - $file <<<$title > $file.new
19 
20 echo "Modified file is $file.new"
21 
22 exit 0
23 
24 # 下边是'man bash'中的一段:
25 # Here String
26 # here document的一种变形,形式如下:
27 #
28 # << 29 #
30 # word被扩展并且被提供到command的标准输入中.
阅读(1868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~