0

我有一个包含 2 个变量的脚本。其中一个变量是目录路径,另一个是用户输入变量。一旦用户输入变量,在本例中为证书名称,我使用 sed 将 xxxxx 的文本替换为由 / 分隔的 script_path 和 certfile 变量。

在编程方面仍然是新手,但我已经设法让我的脚本在下面的问题之外工作。我尝试将变量转义为“/”,但似乎没有任何效果。

我也尝试过更改 sed 使用的分隔符,但没有成功。我确实搜索了很多,并没有找到使用“/”和连接变量的任何具体内容,所以如果这已经解决,请提前道歉。

#!/bin/bash
script_path=/opt/ceflog

read -p 'Enter the name of the certificate file: ' certfile
sed -e "s/pkcs12_file = xxxxxx/pkcs12_file = $script_path/$certfile/g" \$script_path/cef.conf

应该看起来像下面这样。

pkcs12_file = /opt/ceflog/192.168.1.1_1.pkcs12

一如既往地提前感谢您的帮助。

4

2 回答 2

1

我猜你想做这样的事情

$ path='/opt/ceflog'; cert='192.168.1.1_1.pkcs12'; 
$ echo pkcs12_file = xxxxxx/pkcs12_file | 
  sed -E 's~(pkcs12_file =) (xxxxxx/pkcs12_file)~\1 '"${path}/${cert}"'~'


pkcs12_file = /opt/ceflog/192.168.1.1_1.pkcs12

使用与默认分隔符 ( ) 不同的sed分隔符(这里我选择) ,因为您的数据中可能有它。~/

于 2018-08-21T22:02:08.450 回答
0

所以看起来有2个问题。我逃脱了最后“\$script_path/cef.conf”变量的使用,并改变了sed的dilemeter,我能够让它工作。

sed -e "s|pkcs12_file = xxxxxx|pkcs12_file = $script_path/$certfile|g" $script_path/cef.conf

再次感谢大家。

于 2018-08-21T22:25:56.443 回答