LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2016, 09:17 AM   #16
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306

Quote:
Originally Posted by MadeInGermany View Post
The main problem is that the shell does not substitute $var in a 'string'
I'm sorry, I do not understand what does it mean?
grail made a mistake, $var1 should be $opt I think, but otherwise it should work.
 
Old 12-19-2016, 07:50 AM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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.
 
Old 12-19-2016, 10:14 AM   #18
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would suggest another approach:
- Store each response value in variables
- Write values to configuration file at the end of prompted questions

I think it's not efficient to read and write the whole configuration file at each question...
 
Old 12-20-2016, 08:22 PM   #19
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MadeInGermany View Post
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
 
Old 12-20-2016, 09:11 PM   #20
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Well I would make a search for the port option and then use s/// to change just the true or false.
Something like:
Code:
#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 ) opt='true';;
    N ) opt='false';;
    * )  echo -e "\033[38;5;1mWrong option used. Operation terminated!!!\033[0m "
         break;;
  esac
  sudo sed -i "/$PORTOPTN/s/:.*/: $opt/" mymy.conf
done
Edit: Fixed variable as per pan64's catch
The sed line which you have mentioned seems to be looking for ftp.*, http.*, ...

Can't it be made for specific like $PORTOPTN.enabled only rather than $PORTOPTN.* ?

The configuration file also contains some other lines like

"ftp.enabled": true,
"ftp.port": 21,
"http.enabled": false,
"http.port": 80
..... so on
 
Old 12-20-2016, 11:09 PM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes you can add to the first portion if you need to make it more verbose due to overlaps
 
Old 12-20-2016, 11:18 PM   #22
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Yes you can add to the first portion if you need to make it more verbose due to overlaps
I did with following:
Code:
sudo sed -i "/$PORTOPTN/s/:.enabled/: $opt/" mymy.conf
the result still the same. Sorry If I did stupid..
 
Old 12-21-2016, 12:59 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
Old 12-21-2016, 02:29 AM   #24
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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
Cheers
 
Old 12-21-2016, 02:57 AM   #25
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 12-21-2016, 09:35 AM   #26
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Code:
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.
 
Old 12-22-2016, 02:40 AM   #27
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
Your is also working fine.

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] stty: standard input: Input/output error [bash] DoME69 Programming 7 05-22-2015 12:14 AM
Bash - Replace multiple lines in file based on user input from php? nicedreams Programming 9 04-22-2015 09:11 AM
Linux Bash while loop reading multiple field text input and running remote commands bong_water Programming 2 02-11-2015 10:28 PM
bash script to read multiple user input prasunjit Programming 1 03-31-2013 02:09 PM
read multiple input with bash zulsolar Programming 10 05-09-2010 02:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration