You are not logged in.

#1 2011-03-19 23:18:29

JMO
Member
From: Argentina
Registered: 2006-04-08
Posts: 98

Using Bash script to edit config file

This is a really simple question, but given that I'm just learning Bash scripting and having this solved now would be really illustrative for me, I would really thank some help here.

I'm using uzbl, and running Tor+Polipo. So, as you will see below in the tail of the config file, there is a line to redirect the requests of uzbl through Polipo.

# === Post-load misc commands ================================================
sync_spawn_exec @scripts_dir/load_cookies.sh
sync_spawn_exec @scripts_dir/load_cookies.sh @data_home/uzbl/session-cookies.txt

# Set the "home" page.
#set uri = https://duckduckgo.com
# Local polipo proxy
set proxy_url = http://127.0.0.1:8123
# vim: set fdm=syntax:

What I want to accomplish is to comment in/out that line with a key shortcut on Awesome. I've thought of doing 2 scripts to do so and using 2 differente key shortcuts, but I want to "toggle" the proxy redirection with only 1 shortcut. To do so, I suppose that the script should go something like:

if
tool 'set proxy_url = http://127.0.0.1:8123' config_file
then
tool '#set proxy_url = http://127.0.0.1:8123' config_file
else
if
tool '#set proxy_url = http://127.0.0.1:8123' config_file
then
tool 'set proxy_url = http://127.0.0.1:8123' config_file
fi
fi

I know little about sed, but I think is the tool for this job. The most intriging part to me is to ask sed to print the regular expression when it finds it in the config file, and use that as an input in the conditional statement.

Well, this is a mess I have done here. Hope there is a simple answer to this.

Thanks in advance.-

Offline

#2 2011-03-20 09:57:11

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Using Bash script to edit config file

You can do this with a single sed command:

sed -i 's/^#set proxy_url/set proxy_url/;
        t end;
        s/^set proxy_url/#set proxy_url/;
        : end' config_file

This edits the file in-place (-i) and first tries to replace the commented with the uncommented line. If that suceeds, sed jumps to the "end" label. If not, it tries to replace the uncommented with the commented line. Thus you don't have to include any logic about the current state: if the first substitution succeeds, the line was obviously commented, if not, it was uncommented, and the second substitution should succeed.

Note that my knowledge of sed is very limited. There might be a simpler way to do this.

EDIT: For the sake of example, here's how to do the same in bash using regular expressions. Note how this script needs to use a temporary file to simulate in-place editing, how it needs to process the file line by line manually, etc. All things that sed does out of the box...

#!/bin/bash

tmp=test.conf.tmp
echo -n "" > "$tmp"

while read line; do
    if [[ "$line" =~ ^#set\ proxy ]]; then
        echo "${line/\#/}" >> "$tmp"

    elif [[ "$line" =~ ^set\ proxy ]]; then 
        echo "#$line" >> "$tmp"
    else
        echo "$line" >> "$tmp"
    fi
done < test.conf

mv test.conf.tmp test.conf

To answer your original question, the line

if [[ "$line" =~ ^#set\ proxy ]]; then

reads: if the line begins with a "#", followed by "set proxy", then...

Last edited by hbekel (2011-03-20 10:40:16)

Offline

#3 2011-03-21 01:44:15

JMO
Member
From: Argentina
Registered: 2006-04-08
Posts: 98

Re: Using Bash script to edit config file

Well,
sed worked flawlessly! It's a much simpler solution than that I thought of smile [Just one thing: the spaces are not escaped]
Anyhow, your Bash example is really what I was looking for, The line you remarked, understood under the while loop, gives me an idea about how to achieve this.

I'll keep practicing. In the meanwhile, thanks once more for the help!

Offline

Board footer

Powered by FluxBB