2

I am trying launch adduser command from perl.

use strict;
use warnings;

my @test=('/usr/sbin/useradd',
"-c 'Fred'",
'-d /vol2/home/DMZ/f.kals',
'-g 3335','-u 11002',
"-k '/dev/null'",
'-m',
'-p "$1$kKNKMa8O$g03oj6YeeZbO2i3NMSoyT1"',
'fred');
system (@test);

When I executed the above, I got the following output:

[ay@pandora /vol2]$ sudo ./test.pl
useradd: invalid home directory ' /vol2/home/DMZ/fred'

Why?

If I do not use array

my $command="/foor/bar/useradd -m -g 1234 -u 6789 -param2 -param3 username"
system ($command);

that works OK.. why not array?

0

1 Answer 1

6
my @test=('/usr/sbin/useradd',
"-c 'Fred'",
  1. -c and Fred are different arguments, so are -d and /vol2/home/DMZ/fred ...

  2. When you use array form, you don't have to escape/quote. (this is why array form is safer -- you don't have to handle the shell escapes!)

So, it should be:

my @test=('/usr/sbin/useradd',
'-c', 'Fred',
'-d', '/vol2/home/DMZ/f.kals',
'-g', '3335',
'-u', '11002',
'-k', '/dev/null',
'-m',
'-p', '$1$kKNKMa8O$g03oj6YeeZbO2i3NMSoyT1',
'fred');
Sign up to request clarification or add additional context in comments.

1 Comment

@AntonShevtsov If this answered your question, you should click the checkmark to the left to mark it as "accepted".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.