zoukankan      html  css  js  c++  java
  • Shell 获取指定行的内容

        需求: 有一个文件,根据指定的字符串,得到该字符串上两行的内容。

        文件内容如下:

     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、字符串的截取

            4、判断一个字符串是否包含指定的子字符串

  • 相关阅读:
    dblink密码
    Select .. into .. 和 OCIBindByName
    在网上东逛西逛看到这篇文章,鼻子有点酸。
    奇怪的LINUX系统时间问题
    综合练习:简单日历
    15位到18位身份证的升级计算
    进入面向对象的世界:类就是代码,对象是内存。
    关于数据类型转换的补充知识
    数学函数
    抽象类和密封类
  • 原文地址:https://www.cnblogs.com/AndyStudy/p/6064641.html
Copyright © 2011-2022 走看看