0

I want to add new input to the configuration file from terminal using bash script. This is what I tried:

echo Hello, please add the new text here
read varname
sed -i "s/\<my-images=>/& $varname/" /home/myconfig
echo Image $varname has been added to the configuration. Thanks!!

/home/myconfig has

id=1
max-mb=1000
my-images=customimage

And required output is

id=1
max-mb=1000
my-images=mynewtext customimage

So mynewtext should be added after my-images= Anyway to do this?

2
  • What happens when you run your solution? Commented May 12, 2020 at 19:13
  • No Change. Remains the same. @robinsax Commented May 12, 2020 at 19:15

1 Answer 1

1

The problem is with the regex match you're passing to sed. Try:

sed -i "s/my-images=/&$varname /" /home/myconfig

instead.

Sign up to request clarification or add additional context in comments.

2 Comments

When I enter my input as testthat/version1 it throws me an error. Error is :: Hello,please add the new text here testthat/version1 sed: -e expression #1, char 38: unknown option to `s' How to pass raw input? I tried using read -r -p "Enter Input 'Enter the text >>>' varname @robinsax
Without formatting I have no idea what that error is, but it's probably because the / makes the sed regex invalid. You'll need to escape /s prior to inserting them into the sed regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.