0

我有两个文件。File1包含这样的用户名和密码:

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = Username1
password                      = password1
password2                     = password2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1

File2包含如下一行

http://link:port/username/password/12345

现在我有这个“代码”来更改File2中的用户名/密码:

UsernameOLD=Username1
PasswordOLD=password1
UsernameNEW=Username2
PasswordNEW=password2

sed -i -e "s/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/" /etc/enigma2/file2.cfg

现在我有不同的用户名,它们在File1中总是最新的。我现在正在寻找一种解决方案,将 File1 中的 Username 和 Password2 写入变量,然后在File2中设置这个新的 Username 和 Password2 。

所以作为一个菜鸟,伪代码应该是这样的:

find "username" & "password1" in file1 
set "username" as $UsernameNEW and
    "password1" as $PasswordNEW and
then just execute my sed command.

有人可以帮忙吗?我想我可以用grep这个?但老实说,我很高兴我得到了这个sed带有变量的命令:D

4

2 回答 2

0

这里有一些可以帮助您入门。

oscam.conf

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = xxx1
password                      = password1
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1


[reader]
label                         = anylabel
protocol                      = cccam
device                        = test.url,13377
user                          = yyy1
password                      = password1
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1

对于密码文件,我已经更改了一些格式,但可以使用原始格式完成。

passwd(格式 oldUser、newUser、oldPass、newUser)

xxx1,xxx2,passxxx1,passxxx2
yyy1,yyy2,passyyy1,passyyy2

awk 命令

awk -F, 'FNR==NR {usr[$1]=$2;pass[$1]=$4;next} FNR!=NR{FS=" = "}  /^user/ {t=$2;$2="= "usr[$2]} /^password/ {$2="= "pass[t]} 1' passwd oscam.reader

结果

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = xxx2
password                      = passxxx2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1


[reader]
label                         = anylabel
protocol                      = cccam
device                        = test.url,13377
user                          = yyy2
password                      = passyyy2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1
于 2021-08-14T14:42:49.563 回答
0

快速而肮脏——将旧密码加载到环境参数中:

set -- $(grep -m1 -A1 '^user' File1)
sed -i -e "s#/${UsernameOLD}/${PasswordOLD}#/$3/$6#;T;q" /etc/enigma2/file2.cfg

它是如何工作的: grep喷出六个空格分隔的项目,这些项目set变成命令行参数$1 $2 $3 ... $6。我们只需要$3$6

于 2021-08-15T22:43:36.040 回答