[glugot] translating `/' to `\/' in sed

  • From: Joe Steeve <joe_steeve@xxxxxxx>
  • To: GLUG Madurai <glug-madurai-discuss@xxxxxxxxxxxxxxxxxxxxx>, GLUGOT <glugot@xxxxxxxxxxxxx>
  • Date: Fri, 08 Oct 2004 14:59:10 +0530

I've been working on some custom utility scripts for mass user
account management., in my college. I've get the list of passwords
From a text file and allot it to each user i create and put it in
the passwd file (no shadow file). Initially I create a account
with disabled password using debian's `adduser`., then I use
htpasswd to generate the `user:pass` string and put it into the
passwd file using `sed`. However, I'm having a problem with
this. The passwords generated by htpasswd may contain a `/' or `&'
which have to be escaped with a `\'.

I tried using `sed` to translate `/' to `\/' and put the generated
password in another file which will be used as a input to another
instance of `sed` to modify the /etc/passwd. But this does not
seem to work. Well, I've included the script I wrote. The sed
statement that I used to do this is 
  $sed -e s/\//\\//g passwd.text

Am I doing the right thing?? or is there another workaround?

Cheers,
Joe

--
#!/bin/bash

# Check for number of parameters
if [ $# -ne 4 ]
then
    echo "Usage:"
    echo "pass-gen \"prefix\" <no-of-acc> <start-number> <passwds file>"
    exit
fi
prefix=$1
acc_count=$2
suffix_start=$3
i=0

passwds=`cat $4`
for acc_pass in $passwds
do
  # construct the user name
  let suffix=suffix_start+i  
  acc_name="$prefix$suffix"

  # I've to put the command for creating the account here.

  # construct the password and translate the '/' to '\/'. This does
  # not work.
  enc_pass=`htpasswd -nb $acc_name $acc_pass | sed -e s/\\//\\\//g`

  # generate a sed script to be run on the /etc/passwd to set the 
  # encrypted password., 

  echo "/$acc_name/s/$acc_name:*/$enc_pass/" # >> sed-script

  if [ $i -ge $acc_count ]
      then
      break
  else
      let i=i+1      
  fi
done

if [ $i -ne $acc_count ]
    then
    echo "ERROR: Insufficient passwords in $4"
fi

-- 
A proud GNU user
http://www.joesteeve.tk/ | http://gnukid.shyper.com/

Other related posts:

  • » [glugot] translating `/' to `\/' in sed