Linux commands

1. 字符串相关操作

1.1 获取字符串长度
str="9.125.70.107 localhost domain"
# length
${#str}
1.2 字符串截取
  1.2.1 索引截取
str="9.125.70.107 localhost domain"
substr=${str:0:${#str}-3}
echo $subStr

output:
9.125.70.107 localhost dom

 

  1.2.2 索引某个子字符串之后的字符串
str="9.125.70.107_ localhost domain"
sub="9.125.70.107"
subStr=${str##*$sub}
echo $subStr

output:
_ localhost domain

 

1.3 获取IP address和domain name
http://www.cyberciti.biz/faq/how-to-find-out-the-ip-address-assigned-to-eth0-and-display-ip-only/ how to find out the IP address assigned to eth0 and display IP only
  
#host name
hostname=$HOSTNAME
# IP address
ipAddress=$(/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
#string contains domain
domainStr=$(host $ipAddress)
#get domain name
domainTmp=${domainStr##*$hostname}
domanName=${domainTmp:0:${#domainTmp}-1}

#could also use linux hostname command
ip=$(hostname -i)
fullname=$(hostname -f)
hostname=$(hostname -s)

  1. # get IP address of eth0 network interface
  2. ifconfig eth0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'
 1.4 sed 修改文件中所有匹配的字符串
#usage:
#
sed -i -e "s/oldstr/newstr/g" filename
#
example
sed -i -e "s/$ip/127.0.0.1/g" /etc/hosts

sed -i '/pass/ s/value=".*"/value="\*\*\*\*"/g' /ae/AR/ovf-env.ar(not work, so don't use double quotation marks around the RE)
sed -i '/pass/ s/value=.*/value=2/g' /ae/AR/ovf-env.ar(work)
cognosInstallDirTmp="\/opt\/CognosBI\/c10"
file="a.ats'
sed -i '/APPDIR/ s/.*/APPDIR=$cognosInstallDirTmp/g' $file (not work, so don't use single quotation marks instead of double quotation marks)
sed -i "/APPDIR/ s/.*/APPDIR=$cognosInstallDirTmp/g" $file (work)

转义字符的处理:
path=/opt/com/ae/as
sed -i "/APPDIR/ s#APPDIR=.*#APPDIR=$path#g" /install/linuxi38664h/response.ats.bak

 1.5 读取文件

 1 #/bin/bash
2 file=/opt/ibm/hosts
3 while read line
4 do
5 echo $line
6 if [ " '$line' | grep '9.125.70.103' " ];
7 then
8 echo 'yes'
9 else
10 echo 'no'
11 fi
12 done < $file

1.6 写文件

1 #!/bin/bash
2 #override content to the file
3 echo 'content' > fileName
4
5 #append content to the file
6 echo 'content' >> fileName

1.7 tr tool

tr用来从标准输入中通过替换或删除操作进行字符转换。 tr主要用于删除文件中控制字符或进行字符转换

tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] file

例如大小写替换:

cat oops.txt | tr "[a-z]" "[A-Z]" > result.txt
1.8 cut tool

  用法:cut -cnum1-num2 filename  
  说明:显示每行从开头算起 num1 到 num2 的文字。

  结合管道使用更为方便: echo $str | cut -c2-5

 

   cut -b list [-n] [file ...]
   cut -c list [file ...]
   cut -f list [-d delim][-s][file ...]
l    上面的-b、-c、-f分别表示字节、字符、字段(即byte、character、field);
l    list表示-b、-c、-f操作范围,-n常常表示具体数字;
l    file表示的自然是要操作的文本文件的名称;
l    delim(英文全写:delimiter)表示分隔符,默认情况下为TAB;
l    -s表示不包括那些不含分隔符的行(这样有利于去掉注释和标题)

2. grep

在调用变量时,应该使用双引号  grep "APPDIR=$DIR"

在调用模式匹配时,应该使用单引号 grep '^APPDIR.*'

 

3. user group management

3.1 add user to group

usermod -aG db2iadm1 root

 

4. 条件判断

#!/bin/bash
a="hello"
b="world"

c="hellow"
d="worldy"
if [ $a = $c -a $b = $d ];
then
echo 'right'
elif [ $a = $c -o $b = $d ];
then
echo 'partly'
else
echo 'wrong'
fi




posted on 2011-12-30 21:09  bannyle  阅读(296)  评论(0)    收藏  举报

导航