deepak rao deepak rao - 1 year ago 74
Bash Question

How can I replace the username and password using a shell script

How can I update the shell script where I can replace username "u1" to any value from user input in a .txt file

'username' => "u1"

Below is the existing code:-

echo "\nEnter Username to replace: "
read name

sed -i -e s/u1/"$name"/g /var/1.txt

Answer Source

Try '$name' instead of "$name"

Best option is to use sed -i -r

 sed -i -r 's/u1/'$name'/g'  /var/1.txt

Another option you can try is save this to temp file and move it back

Example :

 sed 's/u1/'$name'/g' /var/1.txt > /tmp/temp

 mv /tmp/temp /var/1.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download