如何使用bash脚本替换用户输入文件路径

时间:2017-12-13 21:54:25

标签: bash shell

我正在尝试根据用户输入替换文件中的文件路径。我没有看到我的文件中发生任何替换。

我的档案

   paths:              
      - /var/log/*.log

我的bash脚本

echo 'Enter the log directory path'
read log_path
sed -i -e 's/    - \/var\/log\/*.log/    - $log_path/g' /envs/cfc/fmn.txt

我不确定我在哪里做错了。

1 个答案:

答案 0 :(得分:-1)

嗯,首先,你试图在单引号内进行变量扩展,这是不可能的。它应该是这样的:

#!/bin/bash

read -p 'Enter the log directory path: ' log_path
sed -i -e "s#/var/log/\*.log#$log_path#g" ./file.txt