0

I have a users.txt file with the following content:

[builders.ca]

UniqueID=DB@LqlFP

Login=buildca

Pass=5nFvZLwx

RelativePath=1

[DeluxeDoors.ca]

UniqueID=RgOkvU4Z

Login=DeluxDSM

Pass=L9pP3iaK

RelativePath=1

[Sonicwall.com]

UniqueID=JVpFoXad

Login=firewall

Pass=azasadsa

RelativePath=1

I wrote a script to replace all the passwords with random passwords in the file.

The script is:

users=($(cat users.txt | grep 'Login=' | cut -c 7-))

for user in "${users[@]}"; do

        pass=$(cat users.txt | grep -A 2 $user | grep 'Pass' | cut -c 6-)
        new_pass=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-zA-Z0-9' | head -c 8)
        echo $pass;
        echo $new_pass;
        #perl -pi -e 's/$pass/$new_pass/g' users.txt
        sed -i '' 's/"${pass}"/"${new_pass}"/g'  users.txt
done

But it is not updating the passwords in the users.txt. Any help would be highly appreciated.

Bhawan
  • 2,441
  • 3
  • 22
  • 47
  • 4
    haven't gone through entire post, but you are using double quotes inside single quotes... that is not going to do variable substitution... use double quote for entire expression... see https://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables – Sundeep Jan 25 '18 at 15:11

1 Answers1

0
sed -i '' 's/'"${pass}/${new_pass}"'/g'  users.txt
             ^                     ^

These seem to be missing, so the sed was getting ${pass} and ${new_pass} as literal strings, not the expanded variables.

Guy
  • 637
  • 7
  • 15