需求: 有一个文件,根据指定的字符串,得到该字符串上两行的内容。
文件内容如下:
1 linux-56:# cat sys.ttconnect.ini 2 # Copyright (C) 1999, 2006, Oracle. All rights reserved. 3 4 ############################################################### 5 # This is a file used for the TimesTen Client. 6 # It contains entries for each server. 7 # GMPC connects TimesTen server over TCP. 8 ############################################################### 9 10 [LocalHost_mpc1] 11 Description=First TimesTen Server 12 Network_Address=localhost 13 TCP_PORT=17002 14 TTC_Timeout=300 15 16 [LocalHost_mpc1_1] 17 Description=First TimesTen Server 18 Network_Address=linux-56 19 TCP_PORT=17002 20 TTC_Timeout=300
根据hostname 得到 [LocalHost_ 后面的用户名。
具体代码:
1 #!/bin/sh 2 TT_DSN=`awk '/^setenv TT_DSN/ {print $3}' /export/home/oracle/.login | tail -1` 3 #echo $TT_DSN 4 HOST_NAME=`hostname` 5 6 TT_CONNECT_FILE=/opt/TimesTen/${TT_DSN}/info/sys.ttconnect.ini 7 start_line=`grep "Network_Address=$HOST_NAME" $TT_CONNECT_FILE -n | awk -F: '{print $1}'` 8 if [ "X" == "X$start_line" ]; then 9 start_line=`grep "Network_Address=localhost" $TT_CONNECT_FILE -n | awk -F: '{print $1}'` 10 fi 11 12 #echo $start_line 13 check_line=$[$start_line - 2] 14 #echo $check_line 15 DSN_TMP=`head -n $check_line $TT_CONNECT_FILE |tail -n +$check_line` 16 #echo $DSN_TMP 17 18 DSN=`echo ${DSN_TMP:11}` 19 DSN=`echo ${DSN%]}` 20 21 #echo $DSN 22 23 Result=`su - oracle -c "ttIsqlCS -connstr $DSN -v 1 -e 'call ttrepstateget;bye'"` 24 #echo $Result 25 26 if [[ "*ACTIVE*" != $Result ]]; then 27 echo "not ACTIVE" 28 exit 29 else 30 echo "ACTIVE" 31 fi
这里主要可以学习的几个点在于:
1、如何根据指定的字符串,得到当前行的行号
2、根据行号获取该行附近的内容
3、字符串的截取