1

I need to change a .config text file every time the program doesn't work as I want it to. I want to be able to do it in terminal preferably with an alias and with the following logic:

alias config='change line 17 to this THEN run this command THEN replace line 17 with what was at beginning'

So when I enter config into terminal the offending line will be changed, after which a command will be run, after which the line will be set to be what it was at beginning

4
  • Sounds like an unclean way to handle an error. Wouldn't it be better to try to solve the problem? Apr 6, 2014 at 15:56
  • I dont know how to solve disabling a dns when vpn is run and change back to normal dns when vpn exits: askubuntu.com/questions/444051/…
    – fdsfgdsdf
    Apr 6, 2014 at 16:05
  • If I understand well, you need to be able to toggle between two config file versions? that should not be too difficult. It would maybe even be easyer (and saver) to have two versions of the file and make a script that would replace the real config file, depending on the contents of the config file. where is the file stored? Apr 6, 2014 at 16:55
  • @Jacob the file is /etc/dhcp/dhclient.conf but I still dont know exactly how and what file will let me disable a dns from being used(please see Q above for detials)
    – fdsfgdsdf
    Apr 6, 2014 at 16:59

2 Answers 2

1

This sounds very much like a band aid and an XY Problem and you would be better off figuring out the core problem. Still, these commands will do what you need. I have written it as a shell function, not an alias, but just add these lines to your ~/.bashrc file:

fix_config(){
    ## Change current working directory to the directory where .config file is located
    cd /path/to/config_file
    ## The original 17th line
    old="$(sed '17q;d' .config)"
    ## The one you will replace it with
    new="new line"
    ## The sed command that does the replacing
    sed -i.bak "17 s/$old/$new/" .config
    ## Run whatever needs to be run with the modified file
    command
    ## Copy the file back
    cp .config.bak .config
    ## Go back to the old working directory
    cd $OLDPWD
} 

You can then run it by running fix_config.

1
1

Here is a verbose solution as a python script. Not literally what you were asking for, but yet a solution:

#!/usr/bin/python3

import os

# change the lines below to the correct paths
path_to_configfile = "path_to_configfile" # the real one in  /etc/dhcp/dhclient.conf
path_toconfigfile_a = "path_toconfigfile_a"
path_toconfigfile_b = "path_toconfigfile_b"
# change the line to the default line in the config file (leave the \n)
old_line = "old_line\n"
# change to the line number to check as an indicator
line_number = 17 # first line =  1
# change to the messages you'd like to see
config1_message = "config 1 is activated"
config2_message = "config 2 is activated"

with open(path_to_configfile) as defaultconfig:
    defaultconfig = defaultconfig.readlines()

if defaultconfig[line_number-1] == old_line:
    os.system('gksudo cp '+"'"+path_toconfigfile_a+"'"+" "+"'"+path_to_configfile+"'")
    os.system('zenity --info --text '+'"'+config1_message+'"')
else:
    os.system('gksudo cp '+"'"+path_toconfigfile_b+"'"+" "+"'"+path_to_configfile+"'")
    os.system('zenity --info --text '+'"'+config2_message+'"')

To use it:

  • create a folder in a secure directory to store the two versions of your config files.
  • create a ~/bin folder.
  • copy the code above into a file and call it toggle_config (or anything you like).
  • change the lines in the headsection to the correct paths, line and messages you'd like to see when the settings are toggled
  • save the file and make it executable. store it in ~/bin, or copy it to /usr/bin for more security.

After logout / login you will be able to toggle between the two setting versions, by the command

toggle_config

You will be asked for your password, and you will see a message like this:

enter image description here

Alternatively, you can create a .desktop file with the command and lock it to the launcher for quick switching between the settings.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .