shell 读取文件 修改另外文件
2014-03-20 18:11 freefei 阅读(586) 评论(0) 收藏 举报#!/bin/bash # script shell used init config.js username=$(cat pass.txt|awk '{print $1}') password=$(cat pass.txt|awk '{print $2}') echo $username echo $password user=user:'$username' pass=password:'$password' sed -r -i "/user:/ s/.*/$user/g" config.js sed -r -i "/password:/ s/.*/$pass/g" config.js
可行版本代码
#!/bin/bash # script shell used init config.js name=($(cat pass.txt)); #读取文件赋值给name u=${name[0]}; # 读取name的第一行代码 p=${name[1]}; # d=${name[2]}; # #echo $u #echo '++++++++++++++++++++++' #echo $p user="user:\'${u}\'," #赋值 加上单引号 转义 pass="password:\'${p}\'," databass="database:\'${d}\'," sed -r -i "/user:/ s/.*/$user/g" config.js; #搜索含有“user:”的行 并吧该行 全部替换为变量 $user sed -r -i "/password/ s/.*/$pass/g" config.js; sed -r -i "/database:/ s/.*/$databass/g" config.js exit 0
#!/bin/bash # script shell used init config.js #nodo程序配置文件 dataPathConfig='/nodeim/app/conf/mysql.js'; thinkfig='/web/default/public_html/ThinkPHP/appconfig.php'; name=($(cat /nodeim/pass)); #========nodeim config================ u=${name[0]}; p=${name[1]}; d=${name[2]}; #=========thinkconfig================== #echo $u #echo '++++++++++++++++++++++' #echo $p user="user:\'${u}\'," pass="password:\'${p}\'," databass="database:\'${d}\'," sed -r -i "/user:/ s/.*/${user}/g" ${dataPathConfig}; sed -r -i "/password/ s/.*/${pass}/g" ${dataPathConfig}; sed -r -i "/database:/ s/.*/${databass}/g" ${dataPathConfig} #=======sed with think================ thk_user="\'DB_USER\' => \'${u}\',"; thk_pass="\'DB_PWD\' => \'${p}\',"; thk_name="\'DB_NAME\' => \'${d}\',"; sed -r -i "/DB_USER/ s/.*/${thk_user}/g" ${thinkfig}; sed -r -i "/DB_PWD/ s/.*/${thk_pass}/g" ${thinkfig}; sed -r -i "/DB_NAME/ s/.*/${thk_name}/g" ${thinkfig}; exit 0
浙公网安备 33010602011771号