LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2008, 11:34 PM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
shell script to perl script


I have done a script, but it is based on shell script, and i want to convert it to perl. I am very new to perl.

This is my shell script

#!/bin/bash

echo -n "Enter your name : "
read username
echo -n "Enter your password : "
read password

pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username

And i want to change it to perl script

This is how i change it, but it does not work

#!/usr/bin/perl

print "Enter your name : \n";
$username = <STDIN>;
print "Enter your password : \n";
$password = <STDIN>;

#I think my mistake is somewhere here.
$pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
system("useradd -m -p $pass $username");

PLease help me.

Last edited by john83reuben; 02-21-2008 at 11:48 PM.
 
Old 02-22-2008, 12:23 AM   #2
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
I have found the solution.Thanks

#!/usr/bin/perl

print "Enter your name : \n";
$username = <STDIN>;
print "Enter your password : \n";
chomp(my $password = <STDIN>);

$pass = crypt($password,"password");

system("useradd -m -p $pass $username");
 
Old 02-22-2008, 01:02 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just FYI, start the top of all your Perl progs with the following 2 lines, they'll save you a lot of grief:

#!/usr/bin/perl -w
use strict;

If you haven't already, bookmark this:
http://perldoc.perl.org/
 
Old 02-22-2008, 02:57 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
If requesting a password from the command line, so that it doesn't appear when you type it, use stty -echo before the request and stty echo after:

Code:
#!/usr/bin/perl

print "Enter your name : \n";
$username = <STDIN>;
print "Enter your password : \n";
system "stty -echo";
chomp(my $password = <STDIN>);
system "stty echo";

$pass = crypt($password,"password");

system("useradd -m -p $pass $username");
 
Old 02-22-2008, 07:06 AM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Don't you want to chomp the $username variable as well? I don't think that you want that newline in your "useradd" command on the last line. Run this once, and you'll see that you have a newline you don't want:
Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Enter your name:\n";
my $username = <STDIN>; ## should be: chomp(my $username = <STDIN>);

print "Enter your password:\n";
system "stty -echo";
chomp(my $password = <STDIN>);
system "stty echo";

print "Your username is $username, and your pass is $password.\n";
 
Old 02-22-2008, 12:41 PM   #6
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by chrism01 View Post
Just FYI, start the top of all your Perl progs with the following 2 lines, they'll save you a lot of grief:

#!/usr/bin/perl -w
use strict;
Definitely. Perl isn't installed in the same place on all computers. To be more robust, I use the following, for the same effect as the above.
Code:
#! /usr/bin/env perl
use warnings;
use strict;
/usr/bin/env is pretty much guaranteed to be there, and it will find Perl anywhere in your PATH.
 
Old 02-24-2008, 06:17 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah, but actually I was indicating the '-w' , rather than the path.
Also, in some of places there is more than one version of Perl avail, so it's usually a local rule about how you specify the path eg some admins put a symlink at /usr/bin/perl which points to whichever version they want you to use.
use warnings;
is certainly one way to to do it (TMTOWTDI)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use perl.exe in shell script littletransformer Programming 7 12-03-2007 03:14 AM
Using a perl script as a User's Shell john_b Linux - Security 4 07-28-2007 04:22 AM
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
Shell Script, C, C++, or Perl ? mrpc_cambodia Programming 5 08-12-2004 10:18 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration