简体   繁体   中英

Replace specific variables in external bash-script

I am new to coding and already found a few topics about this on stackoverflow, but couldn't make it work, as they seem overextended to me. I might need some guidance.

I need to change some variables in an external bash script 'comlink.conf'. But only specific ones. Others should be left like they are.

ready=0
test=1
new=2

echo 'ready='$ready > comlink.conf
sleep 10

ready=1

echo 'ready='$ready > comlink.conf

If I do it like that, 'test=1' and 'new=2' will be overwritten completely and are gone from the file. That should not happen.

What would be the most easiest way to do this?

If I understand you right, you want to replace a line in a file. You shouldn't use echo for that.

Instead I would suggest using sed:

sed '/.*ready*/s/.*/ready=1/' comlink.conf

You can use sed with a different substitution like:

sed 's/ready=.*/ready=1/' comlink.conf > tmp
mv tmp comlink.conf

or if you are using GNU sed:

sed -i 's/ready=.*/ready=1/' comlink.conf

or BSD sed:

sed -i'' 's/ready=.*/ready=1/' comlink.conf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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