0
0

【csh】調査用シェル

Posted at

資産が増えてくると、影響調査が膨大になり、目検での確認が困難となる。
そこで引数に調査用キーワードを渡すことで、該当するソースを検索できるシェルを作成する。

調査用シェル

searchKeyword.csh
#!/usr/bin/csh -f
#***************************************************************
#
#プログラム名:ソース検索
#ファイル名称:searchKeyword.csh
#処理概要:キーワードでソースの検索を行う。
#入力:キーワード
#出力:なし
#作成者:
#作成日:
#================================================================
#更新履歴
#----------------------------------------------------------------
#バージョン:
#修正日付:
#修正者:
#修正内容:
#***************************************************************
#----------------------------------------------------------------
# Prameters
#----------------------------------------------------------------
# Exit code
#    0 : 正常終了
#    9 : 異常終了
#----------------------------------------------------------------

###--- Set Environment
source /home/yuya/csh_project/batch/etc/batch_envset.csh

#----------------------------------------------------------------
# 変数設定
#----------------------------------------------------------------
onintr SUSPEND
set noglob

# SEARCH LIST
set search_list_path = "${BEANS_BATCH}/script/searchKeyword.csh"
# SEARCH WORD
set search_word = "$1"
# LOGIN HOST
set current_host = `hostname`

#----------------------------------------------------------------
# チェック処理
#----------------------------------------------------------------

# 指定パラメータ
if ( $#argv == 0 ) then
    echo "ERR:引数に検索ワードが未設定です。"
    goto exit_point
else if ( $#argv != 1 ) then
    echo "ERR:引数の指定は1つまでです。"
    set exit_code = 9
    goto exit_point
endif

# 調査用シェルの存在チェック
if ( ! -e ${search_list_path} ) then
    echo "調査用シェルが存在しません。( ${search_list_path} )"
    set exit_code = 9
    goto exit_point
endif

#----------------------------------------------------------------
# 処理開始
#----------------------------------------------------------------
echo ""
echo "********************************************************"
echo "検索開始 ・・・"
echo "  検索日時  : `date +'%Y-%m-%d %H:%M:%S'`"
echo "  検索ワード : ${search_word} "
echo "********************************************************"

# 有効な行を抽出
# コメント行(行頭が"#")、空行は非対象
set valid_lines = `cat ${search_list_path} | grep -v '^#' | grep -v '^$'`

set remote_cmd = ""
while ( $#valid_lines )
    set current_line = $valid_lines[1]

    set type = `echo "${current_line}" | cut -f 1 -d "|"`
    set host = `echo "${current_line}" | cut -f 2 -d "|"`
    set user = `echo "${current_line}" | cut -f 3 -d "|"`
    set directory = `echo "${current_line}" | cut -f 4 -d "|"`
    set file_pattern = `echo "${current_line}" | cut -f 5 -d "|"`

    if ( $#valid_lines == 1 ) then
        set next_host = ""
    else
        set next_line = $valid_lines[2]
        set next_host = `echo "${next_line}" | cut -f 2 -d "|"`
    endif

    if ( "${type}" == "0" ) then
        shift valid_lines
        continue
    endif
    echo "********************************************************"
    echo "  検索ホスト : ${host}"
    echo "  検索ディレクトリ : ${directory}"
    echo "********************************************************"

    # srcディレクトリ
    if ( "${type}" == "1" ) then
        if ( "${current_host}" == "${host}" ) then
            find ${directory} -type f -name '*.pc' -o -name '*.h' | grep -v SCCS | xargs grep -il ${search_word}
        else
            set cmdline = "find ${directory} -type f -name '*.pc' -o -name '*.h' | grep -v SCCS | xargs grep -il ${search_word}"
        endif
    # その他ディレクトリ
    else
        if ( "${current_host}" == "${host}" ) then    
            find ${directory} -type f -name ${file_pattern} | grep -v SCCS | xargs grep -il ${search_word}
        else
            set cmdline = "find ${directory} -type f -name '${file_pattern}' | grep -v SCCS | xargs grep -il ${search_word}"
        endif
    endif
    
    if ( "${current_host}" != "${host}" ) then
        # リモートの場合、コマンドを連携し一括で行う。(PW入力を最小限にするため)
        if ( "${remote_cmd}" == "" ) then
            set remote_cmd = "${cmdline}"
        else
            set remote_cmd = "${remote_cmd};echo; ${cmdline}"
        endif

        if ( "${host}" != "${next_host}" ) then
            echo ""
            echo "*** 同一リモートホスト:${host}はまとめて検索 ***"
            echo ""
            set script_dir = `dirname $0`
            ${script_dir}/ssh/autsshcmd ${user} ${host} ${user} "${remote_cmd}"
            set remote_cmd = ""
        endif
    endif

    echo ""

    shift valid_lines
end

#----------------------------------------------------------------
# 処理終了
#----------------------------------------------------------------

echo ""
echo "********************************************************"
echo "続いてプロシージャの検索"
echo "********************************************************"
${SQLPLUS} ${DB_USER}/${DB_PW} @${BEANS_BATCH}/sql/search_procedure ${search_word}
echo "********************************************************"
echo "プロシージャの検索終了"
echo "********************************************************"

set exit_code = 0

exit_point:

switch ( ${exit_code} )
case 0:
    echo "********************************************************"
    echo "検索終了"
    echo "  終了日時  : `date +'%Y-%m-%d %H:%M:%S'`"
    echo "********************************************************"
    breaksw
default:
    echo "********************************************************"
    echo "検索中止"
    echo "  中止日時  : `date +'%Y-%m-%d %H:%M:%S'`"
    echo "********************************************************"
    breaksw
endsw

exit ${exit_code}
     

続いてプロシージャ検索用SQL

search_procedure.sql
/********************************************************/
/* プログラム名 : search_procedure.sql                  */
/* 作成日       : yyyy/mm/dd                            */
/* 作成者       : yuya                                  */
/********************************************************/

set echo off
set heading off

SELECT DISTINCT NAME, TYPE
FROM USER_SOURCE
WHERE UPPER(TEXT) LIKE UPPER('%&1%');

exit
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0