ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I did see the latest post in time.
My answer addressed the previous posts that have one 'string'.
Of course one "string" is also okay because the variables in it are substituted.
The main problem is that the shell does not substitute $var in a 'string'
Split into two strings so it becomes a concatenation of 'part1' "$var" 'part2'!
The shell then does the substution and removes all the quotes, so sed sees one string.
Code:
for PORTOPTN in ftp http rdp smb
do
read -p "Do you want to enable ${PORTOPTN} port? [y/N]" responsePORTS
case "$responsePORTS" in
y|Y )
sudo sed -i 's/^ *\('"$PORTOPTN"'\.enabled: *\)[^,]*/\1true/' mymy.conf
;;
n|N )
sudo sed -i 's/^ *\('"$PORTOPTN"'\.enabled: *\)[^,]*/\1false/' mymy.conf
;;
* )
echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m "
;;
esac
done
Comment on the previous post: the break ends the loop, a continue would continue the loop.
Hi,
I tried with above code in my machine and found out that it does not change any values in the configuration file i.e. mymy.conf
The first part of the sed I have shown is the part that searches for the line we want to change, so that is where you need to add the search item:
Code:
sudo sed -i "/$PORTOPTN.enabled/s/:.*/: $opt/" mymy.conf
This would search for any line containing say 'ftp.enabled', then the s/// part remove the ':' and everything after it and replaces with our new, perhaps same, value.
As mentioned by keefaz, this process is also inefficient as it calls sed multiple times on the same file. So an alternate could be that you use the while loop to create a temporary file
which sed could then use to make the necessary changes
The first part of the sed I have shown is the part that searches for the line we want to change, so that is where you need to add the search item:
Code:
sudo sed -i "/$PORTOPTN.enabled/s/:.*/: $opt/" mymy.conf
This would search for any line containing say 'ftp.enabled', then the s/// part remove the ':' and everything after it and replaces with our new, perhaps same, value.
As mentioned by keefaz, this process is also inefficient as it calls sed multiple times on the same file. So an alternate could be that you use the while loop to create a temporary file
which sed could then use to make the necessary changes
Thanks for the help. I have finally managed to fix it. I used following line:
Code:
sudo sed -i 's/\("'"$PORTOPTN"'\.enabled": *\)[^,]*/\1'"$opt"'/' mymy.conf
Is that because my example is not working? Your proliferation of quotes is not very maintainable or obvious, plus you will still have the same possible issue that you now have a command (true/false)
stored in your variable.
sudo sed -i "s/\($PORTOPTN\.enabled\": *\)[^,]*/\1$opt/" mymy.conf
# is much more readable than this:
sudo sed -i 's/\("'"$PORTOPTN"'\.enabled": *\)[^,]*/\1'"$opt"'/' mymy.conf
# the solution by grail is much more elegant
sudo sed -i "/$PORTOPTN.enabled/s/:.*/: $opt/" mymy.conf
# the idea from keefaz is much more efficient
# and finally you can store the settings in an associative array (not only the enabled ones, bot ports and others too) and finally you will be able to generate the result without sed or with one single sed.
Is that because my example is not working? Your proliferation of quotes is not very maintainable or obvious, plus you will still have the same possible issue that you now have a command (true/false)
stored in your variable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.