简体   繁体   English

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

[英]How to replace the user input file path using bash script

I am trying to replace the filepath in the file based on the user input. 我正在尝试根据用户输入替换文件中的文件路径。 I am not seeing any replacement happening in my file. 我没有看到文件中发生任何替换。

My file 我的档案

   paths:              
      - /var/log/*.log

My bash script 我的bash脚本

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

I am not sure where I am doing wrong. 我不确定我在哪里做错了。

Well, first of all, you are trying to get variable expansion inside single quotes, which is impossible. 好吧,首先,您试图在单引号内进行变量扩展,这是不可能的。 It should be like this: 应该是这样的:

#!/bin/bash

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM