30ThreeDegrees
30ThreeDegrees

Reputation: 41

Replace a single placeholder in a .conf file using bash

Hi everyone I am still learning bash at the moment but I am writing a script for work that is being used to install Docker and CNTLM because we are running behind some proxies.

I have the installations working but am struggling to change 2 variable placeholders in the cntlm.conf file below.

cnlm.conf

#
# Cntlm Authentication Proxy Configuration
#
# NOTE: all values are parsed literally, do NOT escape spaces,
# do not quote. Use 0600 perms if you use plaintext password.
#

Username    $MyUserName
Domain      NTADMIN
Password    $MyPassWord
# NOTE: Use plaintext password only at your own risk
# Use hashes instead. You can use a "cntlm -M" and "cntlm -H"
# command sequence to get the right config for your environment.
# See cntlm man page
# Example secure config shown below.
PassLM          93409221097752460192457192457999
PassNT          08992693829837928372938729387229
### Only for user 'testuser', domain 'corp-uk'
PassNTLMv2      02793865976487363876348763873467

# Specify the netbios hostname cntlm will send to the parent
# proxies. Normally the value is auto-guessed.
#
# Workstation   netbios_hostname

I have been able to change the

by using replace line with 'sed' but I am unable to change the $MyUserName and $MyPassWord from the variables being used in the bashscript.

Any ideas on how I can do this?

Upvotes: 1

Views: 2534

Answers (1)

Wilfredo Pomier
Wilfredo Pomier

Reputation: 1121

There are various alternatives:

To replace them using sed on a "template" and creating a new file, you can do it like this:

sed 's/\$MyPassword/MySuperPassword/' cnlm.conf > cnlm.new.conf

Now, if you will replace into the same file and you don't know the last value of the password, you can do:

sed -ri 's/^(Password *).*$/\1MySuperPassword/' cnlm.conf

If your new password is in a shell variable, then you can execute the last command like this:

newPasswd="abcde" 
sed -ri "s/^(Password *).*$/\1${newPasswd}/" cnlm.conf

Finally, if you want to change the username and the password in the same command:

newUser="user123" 
newPasswd="abcde" 
sed -ri "s/^(Username *).*$/\1${newUser}/; s/^(Password *).*$/\1${newPasswd}/" cnlm.conf

Upvotes: 3

Related Questions