Chinaunix

标题: Shell 替换问题 [打印本页]

作者: wangsiming519    时间: 2012-06-06 05:06
标题: Shell 替换问题
请教一个问题
我有一组文件,需要修改其中的某一行 这行具有 /home/informix/.profile/ 的特征
我需要把informix 改成 ***
请教:ed那段能不能用更高阶的方法 !!!!!!!!!!

脚本如下:

#!/bin/bash -x

PROD_SCR=/prod/scr
PROD_SCR=`dirname $0`/PROD_SCR
SCRIPTS="
  $PROD_SCR/create_next_working_day_file.ksh
"

function DoChangeProfileSource() {
        src_literal=$1
        dest_literal=$2

        ((rc=0))
        for script_name in `echo "$SCRIPTS"|grep .|grep -v '^#'` ; do
        echo "(editing $script_name)"
        ed $script_name <<! > /dev/null
g?^\. /home/$src_literal/\.profile\$?s?$src_literal?$dest_literal?
w
q
!
        if [ $? -ne 0 ]
        then
                echo "$script_name: error editing the script"
                ((rc=1))
        fi
        done
        return $rc
}


DoChangeProfileSource $SRC_USER $SRC_GROUP

运行结果如下:
root@psprod:siming# ./test.sh 1 informix nqb
+ pdir=/home/siming/
+ PROD_SCR=/prod/scr
++ dirname ./test.sh
+ PROD_SCR=./PROD_SCR
+ SCRIPTS='
  ./PROD_SCR/create_next_working_day_file.ksh
'
+ DoChangeProfileSource
+ mode=
+ src_literal=
+ dest_literal=
+ (( rc=0 ))
++ echo '
  ./PROD_SCR/create_next_working_day_file.ksh
'
++ grep .
++ grep -v '^#'
+ for script_name in '`echo "$SCRIPTS"|grep .|grep -v '\''^#'\''`'
+ echo '(editing ./PROD_SCR/create_next_working_day_file.ksh)'
(editing ./PROD_SCR/create_next_working_day_file.ksh)
+ ed ./PROD_SCR/create_next_working_day_file.ksh
+ '[' 2 -ne 0 ']'
+ echo './PROD_SCR/create_next_working_day_file.ksh: error editing the script'
./PROD_SCR/create_next_working_day_file.ksh: error editing the script
+ (( rc=1 ))
+ return 1


作者: dahaoshanhe    时间: 2012-06-06 08:16
好长的例子




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2