![](/img/trans.png)
[英]How to read the column of text file using Linux shell script and copy the files to another path with in sub directories
[英]How to read a file and replace a string in another file using shell script in linux?
我試圖從文本文件中讀取密鑰的值。 讀完之后,我想將這個值與一些字符串連接起來,並在另一個文件中替換該行。 請找到以下代碼
source.txt file
USERNAME='cffde308-a1f8-436f-9750-619856e70b88'
PASSWORD='XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0='
目標.js 文件
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
password: 'ZEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
},
我想從源文件中復制用戶名和密碼並在配置文件中替換它。 我嘗試了下面的代碼
#! /bin/sh
source "/opt/source/source.txt"
USER="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USER
echo $PWD
sed '/username/c\$USER' target.js
sed '/password/c\$PWD' target.js
我得到的結果是
[l21m23 source]$ ./ClientDetailsSetup.sh
',ername: 'cffde308-a1f8-436f-9750-619856e70b88
password: 'XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
$PWD
},
如果盡早解決這個問題會有很大幫助
#!/bin/sh
source "/opt/source/source.txt"
USERN="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USERN
echo $PWD
sed -i "s/username:.*/$USERN/" target.js
sed -i "s/password:.*/$PWD/" target.js
有幾個問題:
USER
是一個保留的 shell 變量,所以你必須給它一個不同的名字s/.../.../
替換,並且您還需要匹配正則表達式中該行的 rest ,因此需要.*
或舊憑據仍在 Z78E6221F6393D1356DZF681DB393D1356DZF681DB393D1356DZ814CECE-i
選項'
分隔的字符串中執行,因此在此處更改為"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.