1

I have a .conf file that another scripts reads containing only variables, actually only these three for the moment.

    pass=blahblahblah
    port=number 
    user=someusername

How can I write a script that will change/replace/rewrite the variables inside of the .conf file? I asked this question earlier in a different form and kinda got shut down for it. This is something I am very new to & it can be quite confusing before accomplishing a task like this for me at first. This is the last piece to a puzzle of mine.

Thank you, & any help is appreciated.

3 Answers 3

2

Once you get the value into the variable, you can use sed to do an in-place edit of the file, roughly along these lines:

$ cat test.conf
pass=blahblahblah
port=number
user=someusername

$ export NEWPASS='password'

$ sed -i -e"s/^pass=.*/pass=$NEWPASS/" test.conf

$ cat test.conf
pass=password
port=number
user=someusername

Update, as requested, an example within a bash script that captures the input. Since this is a password, we're using the -s option to read, so that the characters don't echo back to the screen. We also have some examples of primitive condition testing (empty password not permitted) and error trapping to terminate the script early. The superfluous "done" message at the end is to illustrate that the script really does terminate when errors are caught (preventing that final message from showing unless no errors occur). Changing the name of the file passed to sed to a nonexsistent filename will trigger the 'change attempt failed' message.

#!/bin/bash

read -s -p "Please specify the new password: " NEWPASS
echo ""

if test "$NEWPASS" = ""; then
        echo "$0: sorry, password cannot be blank" >&2
        exit 1;
fi

sed -i -e"s/^pass=.*/pass=$NEWPASS/" test.conf

if test $? -eq 0; then
        echo 'password changed' >&2
else
        echo 'change attempt failed' >&2
        exit 1
fi

echo "done" >&2

Running the example:

$ cat test.conf
pass=blahblahblah
port=number
user=someusername
$ ./pwchange.bash
Please specify the new password: # it wasn't echoed, but I typed 'foo'
password changed
done
$ cat test.conf
pass=foo
port=number
user=someusername
Sign up to request clarification or add additional context in comments.

2 Comments

It changes the pass in .conf file so thats what I want. Can you show using the read command so it prompts for user input
DEFINITELY, EXACTLY WHAT I WAS LOOKING. This can easily be applied to other situations. I looked for this answer all day. The extra condition testing features definitely blew what I had planned away. :)Thanks A LOT mate.
1

to change variables in a file for every variable you want to change you need a line like this

sed -e 's/\(^pass=\).*/\1my_new_password/' -e 's/\(^file=\).*/\1new_file_name/' /etc/file.conf 

just change the word inside the parenthesis and everything after the \1 before the final /' to match your case.

breaking it down part by part
-e means do a new script to sed
's/x/y/' means search and replace, find every occurrence of x and replace with y (though this happens once per line unless you write it as 's/x/y/g' which the g stands for globally
's/(x)/\1y/' the parenthesis are to back reference the x found, maybe x was some sort of regular expression that varies, the \1 talks about the first set of parenthesis and puts what was in them there. this would find every occurrence of x and replace it with xy
the ^ means match at the beginning of the line.
.* means match any character any number of times, this makes it so the rest of the line is "matched" or else with out that and a string like pass=blahblah it would become pass=new_passwordblahblah

oops below is a miss understanding but might answer the title of the question for future visitors
using read in bash. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-10.html

pass="blahblah"
read tmp_pass
[ "$tmp_pass" == "" ] || pass=$tmp_pass

if they just hit enter them tmp_pass will be empty, but if it is not empty it will end up setting pass to it's value

7 Comments

How would I get that to write out the variable(s) to the .conf, while making sure it replaces any similar variables in the files. So I don't have pass= line twice in file.
ah, you just want to go through a text file and replace stuff? I will try some tests out and get back to you. The command you will probably want is sed
I miss understood and thought you wanted to have a user input to change the value of a variable
say the .confile is located in /etc/file.conf
Yes to change the value of variable in file ;-)
|
0

I got it so this is how you create a basic prompt that will replace the value of a variable within a file.

 #! /system/bin/bash

 read -p "Enter newpass: " NEWPASS

 sed -i -e"s/^pass=.*/pass=$NEWPASS/" /etc/file.conf

I ran:

 cat /etc/file.conf 

& sure enough the values within the file were changed. Thank you to everyone who helped, I couldn't have done it without you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.