Bash replace lines in file that contain functions

I have a shell script that contains the following line

PROC_ID=$(cat myfile.sh | grep running)

which, after you echo out the value would be 1234 or something like that.

What I want to do is find and replace instances of this line with a literal value

I want to replace it with PROC_ID=1234 instead of having the function call.

I've tried doing this in another shell script using sed but I can't get it to work

STR_TO_USE="PROC_ID=${1}"
STR_TO_REP='PROC_ID=$(cat myfile.sh | grep running)'

sed -i "s/$STR_TO_REP/$STR_TO_USE/g" sample.sh

but it complains stating sed: 1: "sample.sh": unterminated substitute pattern

How can I achieve this?

EDIT: sample.sh should contain beforehand

#!/bin/bash
....

PROC_ID=$(cat myfile.sh | grep running)
echo $PROC_ID

....

After, it should contain

#!/bin/bash
....

PROC_ID=1234
echo $PROC_ID

....

The script I'm using as described above will be taking the in an arg from the command line, hence STR_TO_USE="PROC_ID=${1}"



Read more here: https://stackoverflow.com/questions/64406938/bash-replace-lines-in-file-that-contain-functions

Content Attribution

This content was originally published by Colonel Mustard at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: