[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: adding multiple users, newuser? and vim



On Sat, Aug 25, 2001 at 08:53:07PM -0700, Karsten M. Self wrote:
> Actuallly, I should be forcing myself to use Python, but habits die hard
> and I just know my way around sed and awk pretty well, so they're the
> first thing I turn to when a shell script stops being what I want.

Understand your feeling.  Can you do the same with 'sed'?  I would be
curios how to get uid created.

> I've actually updated my AWK script to something more full-featured.
> Attached.

Just for fun, I wrote first usable equivalent python text persing script.

See attached.  It is loner than equivalent awk, perl or bash scripts.  It
certaily needs some idioms to get us started.  But extending Python
script seems easier when it get larger ;-)

Took me a while to get this working.  Not bad excersize reading "Lerning
Python".  Does any one have suggestions of better python coding practice?

-- 
~\^o^/~~~ ~\^.^/~~~ ~\^*^/~~~ ~\^_^/~~~ ~\^+^/~~~ ~\^:^/~~~ ~\^v^/~~~ 
+  Osamu Aoki <debian@aokiconsulting.com>, GnuPG-key: 1024D/D5DE453D  +
+  My debian quick-reference, http://www.aokiconsulting.com/quick/    +

#! /usr/bin/env python
import sys, string

# (C) Osmu Aoki Sun Aug 26 16:53:55 UTC 2001 Public Domain
# Ported from awk script by KMSelf Sat Aug 25 20:47:38 PDT 2001
# This program is distributed WITHOUT ANY WARRANTY.

def usages():
		print \
"Usage:  ", sys.argv[0], " start_UID [filename]\n" \
"\tstartUID is the starting userid to add.\n" \
"\tfilename is input file name. If not specified, standard input.\n\n" \
"Input file format:\n"\
"\tfirstname lastname password\n"
		return 1

def parsefile(startuid):
	#
	# main filtering
	#
	uid = startuid
	while 1:
		line = infile.readline()
		if not line:
			break
		(first, last, passwd) = string.split(string.lower(line))
		# above crash with wrong # of parameters :-)
		user = first[0] + last
		gid = uid
		lineout = "%s:%s:%d:%d:%s %s,,/home/%s:/bin/bash\n" %  \
			(user, passwd, uid, gid, first, last, user)
		sys.stdout.write(lineout)
		++uid

if __name__ == '__main__':
	if len(sys.argv) == 1:
		usages()
	else:
		uid = int(sys.argv[1])
		#print "# UID start from: %d\n" % uid
		if len(sys.argv) > 1:
			infilename   = string.join(sys.argv[2:])
			infile = open(infilename, 'r')
			#print "# Read file from: %s\n\n" % infilename
		else:
			infile = sys.stdin
		parsefile(uid)

Reply to: