# login script help



## emh_alpha1 (Jun 24, 2002)

can someone please help?  i've got a perl script which connects into the login interface via -login hook - a user authenticates to the network volume via netinfo and the script captures the user logging in and their information from the netinfo system.  i then want to check whether a local folder has been created for the user on the workstation and if not create one with all the default settings etc. 

i have tried putting regular commands in the perl script such as mkdir and cp etc... but they just cause the system to hang - can anyone help please?  it's for a large multiuser environment.

thanks in advance.


----------



## RHITMacMan (Jun 24, 2002)

It sounds like you would be better off creating a shell script where you can use cp and mkdir right inside the script.  If you have to use the Perl script though, I'd suggest finding a Perl book for reference and make sure you are calling the Perl functions properly.  You might also try and writing a shell script and have the Perl script execute it if you can't do everything you want in the shell script.

Since the mkdir function in Perl is different than the commands you initiate from the terminal.  Below is an example of how it would look in Perl.  I'm not sure that Perl has a cp function, there is a File::Copy function, but it might be for files only, not sure if it will copy directories or not.  Perhaps someone else might know.

mkdir userfolder, 0770;  # 0770 -> Octal File Permissions Code


----------



## emh_alpha1 (Jun 24, 2002)

Okay so how do I get perl to run a shell script then?  i could probably manage with that.  I've tried getting the perl script to build a shell script on the fly to configure the user account, and the shell script works okay, but I haven't been able to get the perl module to execute it so far.

is there some kind of variation on exec in perl that you know of?


----------



## RHITMacMan (Jun 25, 2002)

Well, you have several options you could try, depending on exactly what you want to do.  First, you can use either the exec() or the system() perl functions.  They are very simlar, but with one essential difference.  If you use the exec() function, then the perl script terminates once it reaches the exec() function in the perl script.  It will never return to the script, and any other lines in the script occurring after the exec() will not be executed.  On the other hand, the system() function will return control to the perl script after executing the passed shell commands.  If the command(s) were a success, the system() function returns 0, otherwise it returns a value of 1 or greater for a failure.  So use exec() if it will be the last or only line to be executed in your perl script, otherwise use the system() function if it is in the middle of your script.  Below, I will give a few examples of things you might try.

First, let's try creating a folder and then duplicating it in a Perl script.

```
#!/usr/bin/perl

system("mkdir ./newdir; cp -R ./newdir ./newdir2");
```
I could have also used the exec() function since I only have one function in the script, but I'm sure your script is much more complex.

Now lets create a shell script to create the new folder and duplicate it.  We'll name it "test.sh."

```
#!/bin/sh

mkdir ./newdir
cp -R ./newdir ./newdir2
```
Now let's execute "test.sh" from our Perl script.

```
#!/usr/bin/perl

system("sh ./test.sh");
```
Once again, I could have also used the exec() function here, if it was the last or only line to be executed in my perl script.  Otherwise, use system().

I just used the "mkdir" and "cp" in my examples since you had asked about these specifically.  You could use any shell commands you like in the manner I did above with "mkdir" and "cp."  Also, if you are having the Perl script generate the shell script you want to execute and saving it, you'll want to use "chmod" in your script after you generate the script and make it executable.  Hope this helps


----------



## RHITMacMan (Jun 25, 2002)

I want to mention one other thing I didn't mention above that is often useful.  If you want to pass parmeters to the shell script you can do this as well.  For example, you might define the name of the folder to be created in the Perl script at some point and want to pass this to your shell script.  Here's a simple example of how you might do this.

First, let's create a Perl script defining some variables and then executing our "test.sh" shell script which we will define next.

```
#!/usr/bin/perl

$user = "harry";
$user2 = "mary";
system("sh ./test.sh $user $user2");
```
Now let's create the "test.sh" script that we are going to execute above in our Perl script.

```
#!/bin/sh

mkdir ./$1
cp -R ./$1  ./$2
```
The $1 and $2 represent the passed varibles to the "test.sh" script.  The first varible passed is cast to the $1 variable for use in the shell script and so on.  Therefore...

$1 = $user = "harry"
$2 = $user2 = "mary"


----------



## emh_alpha1 (Jul 4, 2002)

Thanks - i'm sorta making progress now - is there any way in Perl to copy an entire directory - files and folders.  I can Use FILE::Copy to  copy individual files, but i want to copy the entire Library folder to a user account from a hidden default setup.  Copying it from a shell script works but then I have problems assigning privileges, so I thought I might try doing it from within the perl script which is running at login...


----------

