Question : Help debugging perl script

Hi experts,

I've written the following Perl script, but it isn't working and I can't see why:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
# define variables
$user_remote_file='[email protected]:/usr/local/test/user.txt';
$local_path="/usr/local/test/";
$local_file="user.txt";
$user_list=$local_path . $local_file;
$dont_create=0;

# scp file from live node to local test directory
system('scp', $user_remote_file, $local_path);

# open user list for reading
open(user_list) or die("Could not open file $user_list!");

# loop for each user in list
while ($user = <user_list>) {
	#print "$user";
	# open passwd file to check whether user exists on this system
	open FILE, "</etc/passwd";
	
	while ($passwd_line = <FILE>) {# loop for each line in passwd file
		#print "Into while loop for user $user";
		#print $passwd_line;
		# if the user exists
		if ($passwd_line =~ m/$user/) {
			$dont_create=1;
			print "$user already exists on system!\n";
		}
	}
	# if this user doesn't already have an account on the server
	if ($dont_create==0) {
		# create an account
		system("useradd $user");
		$dont_create=0;
	}


The script is supposed to search /etc/passwd for the users in the file userlist file - this is a text file with a list of users, one on each line.

When I run the script, everything works, except it never goes into the if ($dont_create) section so tries to recreate users that are already on the system. I thought that my regex syntax was correct for searching a string, but apparently its not.....

Any help welcomed!

George

Answer : Help debugging perl script

$user might have a \n or \r\n at its tail.
Add before line 16:

chomp ($user);
Random Solutions  
 
programming4us programming4us