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 want to write a script where the above values would be set to "true" or "false" one by one.
For example: Filename:
Do you want to enable FTP? [Y/N] : y
Do you want to enable HTTP? [Y/N] : y
Do you want to enable RDP? [Y/N] : n
Do you want to enable SMB? [Y/N] :y
... so on till the end
I tried to used following code using multiple case but there is an issue:
Code:
#Enable/Disable PORTS [Ports]
#===>FTP
read -p "Do you want to enable FTP Port? [y/N]" responseFTP
case "$responseFTP" in
y|Y )
#Enable
sudo sed -i 's/\("ftp.enabled":\).*/\1 'true',/' mymy.conf ;;
n|N )
sudo sed -i 's/\("ftp.enabled":\).*/\1 'false',/' mymy.conf ;;
* ) echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m " ;;
esac;;
#===>HTTP
read -p "Do you want to enable HTTP Port? [y/N]" responseHTTP
case "$responseHTTP" in
y|Y )
#Enable
sudo sed -i 's/\("http.enabled":\).*/\1 'true',/' mymy.conf ;;
n|N )
sudo sed -i 's/\("http.enabled":\).*/\1 'false',/' mymy.conf ;;
* ) echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m " ;;
esac;;
I would also force case on your test variable so you do not have to worry how the user inputs the information:
Code:
read -p "Do you want to enable ${OPTION} Port? [y/N]" RESPONSE
case "${RESPONSE^}" in
Y)...
N)...
Also, TenTenths' example is fine but you will need to rework the quotes for the variables to work in the sed.
As usual, you only present a snippet of this imaginary conf file you keep using as an example, so I believe the seds can probably be made a lot simpler but would be unable to assist unless we get a better idea
of the original file.
for OPTION in ftp http rdp smb ; do
read -p "Do you want to enable ${OPTION} Port? [y/N]" RESPONSE
case "${RESPONSE}" in
y|Y )
#Enable
sudo sed -i 's/\("${OPTION}.enabled":\).*/\1 'true',/' mymy.conf ;;
n|N )
sudo sed -i 's/\("${OPTION}.enabled":\).*/\1 'false',/' mymy.conf ;;
* ) echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m " ;;
esac;;
done
Hi,
Thanks for the solution. Well I have used following line of codes using your example but i get error: (syntax check done in https://www.shellcheck.net/ )
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":\).*/\1 'true',/' mymy.conf ;;
n|N )
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'false',/' mymy.conf ;;
*) echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m " ;;
esac;;
done
Error:
Code:
$ shellcheck myscript
Line 1:
for PORTOPTN in ftp http rdp smb ; do
^-- SC1073: Couldn't parse this for loop.
^-- SC1061: Couldn't find 'done' for this 'do'.
Line 10:
esac;;
^-- SC1062: Expected 'done' matching previously mentioned 'do'.
^-- SC1072: Unexpected . Fix any mentioned problems and try again.
$
I would also force case on your test variable so you do not have to worry how the user inputs the information:
Code:
read -p "Do you want to enable ${OPTION} Port? [y/N]" RESPONSE
case "${RESPONSE^}" in
Y)...
N)...
Also, TenTenths' example is fine but you will need to rework the quotes for the variables to work in the sed.
As usual, you only present a snippet of this imaginary conf file you keep using as an example, so I believe the seds can probably be made a lot simpler but would be unable to assist unless we get a better idea
of the original file.
Hi,
Thanks for the concern and I do understand what you trying to say, but I am sharing only the part of configuration file which I am working on it to make it more clear about the issue which I am facing.
$ shellcheck myscript
Line 2:
read -p "Do you want to enable ${PORTOPTN} port? [y/N]" responsePORTS
^-- SC2162: read without -r will mangle backslashes.
Line 6:
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'true',/' mymy.conf ;;
^-- SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
Line 8:
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'false',/' mymy.conf ;;
^-- SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
$
...
read -p "Do you want to enable ${PORTOPTN} port? [y/N]" responsePORTS
...^-- SC2162: read without -r will mangle backslashes.
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'true',/' mymy.conf ;;
...^-- SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'false',/' mymy.conf ;;
...^-- SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
I am not familiar with shellcheck, but those are not actual errors, they are advisory warnings I think.
The first reminds you what read without -r will do - see man bash for that. Doesn't mean it is wrong.
The other two are telling you that true and false appear outside quotes, which may or may not be wrong. In your case I think it is wrong, based on your mymy.conf snippet format.
I would also question why the true / false are not included inside the sed start and end quotes?
Code:
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'true',/' mymy.conf ;;
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 true,/' mymy.conf ;;
One reason you might be getting an error is due to the fact that being outside the call to sed now means the shell is handling the calls to true / false which are commands the shell will execute.
I am not familiar with shellcheck, but those are not actual errors, they are advisory warnings I think.
The first reminds you what read without -r will do - see man bash for that. Doesn't mean it is wrong.
The other two are telling you that true and false appear outside quotes, which may or may not be wrong. In your case I think it is wrong, based on your mymy.conf snippet format.
I finally came up with following lines of code where execution is fine without any errors but it does not change any values in my config file.
Code:
#Variable
var1=true
var0=false
#Enable/Disable PORTS [Ports]
for PORTOPTN in ftp http smb mysql ssh rdp sip snmp ntp tftp telnet mssql vnc rdp;
do
read -p "Do you want to enable ${PORTOPTN^^} Port? [y/N]" responsePORTS
case "${responsePORTS}" in
y|Y )
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 "$var1",/' mymy.conf ;;
n|N )
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 "$var0",/' mymy.conf ;;
* ) echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m " ;;
esac
done
;;
I would also question why the true / false are not included inside the sed start and end quotes?
Code:
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 'true',/' mymy.conf ;;
sudo sed -i 's/\("${PORTOPTN}.enabled":\).*/\1 true,/' mymy.conf ;;
One reason you might be getting an error is due to the fact that being outside the call to sed now means the shell is handling the calls to true / false which are commands the shell will execute.
Thanks. Is there any other option or to rephrase my line?
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.
Last edited by MadeInGermany; 12-18-2016 at 08:56 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.