FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 02-09-2008, 03:24 AM
phillinux
 
Default Shell script function problems

At 07:27 PM 2/8/2008, you wrote:

phillinux wrote:
I'm trying to write a bash shell script to create user accounts
that calls 2 functions. I can't call these functions from the
script or the command line. The set command seems to show the
loaded script in the shell (loaded with . FunctionName at command
line) with other environmental variables. ALSO: The type command
does not recognize the function.


The script and functions work on my laptop running Fedora (with one
small glitch). Is there something I can do to my Debian server to
get functions recognized??




Are these functions by chance fedora specific? Or are they built
into your shell script? How are you loading them into your script?


Thx,
Jeff


I'm still testing, so load the functions at the command line before I
run the script. I load it by typing:


. File_Name (dot, space File_Name)

One function is a recursive script that is called when a duplicate
user name is found on the system. It appends a number on the end and
keeps checking and appending until a unique name is found. I read
that a function is the only way to make a script recursive.


The other function creates the user and password calling useradd and
passwd and here files.
I tested it on my Fedora laptop and kit works, Each function exists
in a separate file, all files are in the same directory.



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a
subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org




--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-09-2008, 03:33 AM
"s. keeling"
 
Default Shell script function problems

phillinux <phillinux@nyc.rr.com>:
>
> I'm trying to write a bash shell script to create user accounts that
> calls 2 functions. I can't call these functions from the script or

Why?

> the command line.

Why?

> The set command seems to show the loaded script in
> the shell (loaded with . FunctionName at command line) with other
> environmental variables. ALSO: The type command does not recognize
> the function.

[Eyes roll up in head.] What!?!

> The script and functions work on my laptop running Fedora (with one
> small glitch). Is there something I can do to my Debian server to get
> functions recognized??

This is not rocket science. Once they're sourced [but you can't
source them?!?], they're callable. They're in the environment ready
to be used.

How is it you're having a problem with this? What is it, really,
you're trying to do?


--
Any technology distinguishable from magic is insufficiently advanced.
(*) http://blinkynet.net/comp/uip5.html Linux Counter #80292
- - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-09-2008, 04:52 AM
phillinux
 
Default Shell script function problems

At 11:33 PM 2/8/2008, you wrote:

phillinux <phillinux@nyc.rr.com>:
>
> I'm trying to write a bash shell script to create user accounts that
> calls 2 functions. I can't call these functions from the script or

Why?

> the command line.

Why?

> The set command seems to show the loaded script in
> the shell (loaded with . FunctionName at command line) with other
> environmental variables. ALSO: The type command does not recognize
> the function.

[Eyes roll up in head.] What!?!

> The script and functions work on my laptop running Fedora (with one
> small glitch). Is there something I can do to my Debian server to get
> functions recognized??

This is not rocket science. Once they're sourced [but you can't
source them?!?], they're callable. They're in the environment ready
to be used.

How is it you're having a problem with this? What is it, really,
you're trying to do?



Why am creati ng a script to create users???
Answer:
I'm a teacher managing the school server. Teachers give edited MS
excel files of students to be added to the system. The script reads
the class lists and loops through account creation


Why a function???
Answer:
I was told only a function could be called recursively. The function
is called recursively to append a number to the user name when
duplicate user names are found on the system.


What do you mean by "sourced"?? Is that loading them into the environment??
That's what I'm talking about. on my laptop it works. on the server
it doesn't I don't understand why. how do you "source" a function??




--
Any technology distinguishable from magic is insufficiently advanced.
(*) http://blinkynet.net/comp/uip5.html Linux Counter #80292
- - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org




--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-09-2008, 05:32 AM
Jeff D
 
Default Shell script function problems

phillinux wrote:

At 11:33 PM 2/8/2008, you wrote:

phillinux <phillinux@nyc.rr.com>:
>
> I'm trying to write a bash shell script to create user accounts that
> calls 2 functions. I can't call these functions from the script or

Why?

> the command line.

Why?

> The set command seems to show the loaded script in
> the shell (loaded with . FunctionName at command line) with other
> environmental variables. ALSO: The type command does not recognize
> the function.

[Eyes roll up in head.] What!?!

> The script and functions work on my laptop running Fedora (with one
> small glitch). Is there something I can do to my Debian server to get
> functions recognized??

This is not rocket science. Once they're sourced [but you can't
source them?!?], they're callable. They're in the environment ready
to be used.

How is it you're having a problem with this? What is it, really,
you're trying to do?



Why am creati ng a script to create users???
Answer:
I'm a teacher managing the school server. Teachers give edited MS excel
files of students to be added to the system. The script reads the class
lists and loops through account creation


Why a function???
Answer:
I was told only a function could be called recursively. The function is
called recursively to append a number to the user name when duplicate
user names are found on the system.


What do you mean by "sourced"?? Is that loading them into the environment??
That's what I'm talking about. on my laptop it works. on the server it
doesn't I don't understand why. how do you "source" a function??




You can source files either by:

. /file/to/be/sourced
or
source /file/to/be/sourced

either method should work just fine.

If this script is the only one that will be using these functions, why
not just include them into the script itself?


But whats failing in your script? There are some differences in how
debian and fedora add users. Have you ran your script with the -x to
debug it? That may help finding out where your script is failing.


Jeff




--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-09-2008, 11:36 AM
William Pursell
 
Default Shell script function problems

phillinux wrote:


I'm trying to write a bash shell script to create user accounts that
calls 2 functions. I can't call these functions from the script or the
command line. The set command seems to show the loaded script in the
shell (loaded with . FunctionName at command line) with other
environmental variables. ALSO: The type command does not recognize the
function.



The script and functions work on my laptop running Fedora (with one
small glitch). Is there something I can do to my Debian server to get
functions recognized??




From some other comments in the thread, it sounds like you are trying
to do something like the following:



$ cat foo

g()
{
printf 'in g
'
}
$ cat bar
#!/bin/sh

g
$ . foo
$ ./bar # won't work since bar doesn't source foo
./bar: line 3: g: command not found
$ cat baz
#!/bin/sh

. foo
g
$ ./baz
in g



If you source the file with the function definitions, it makes
the functions available in the current shell, but not in subshells
that you may invoke.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-09-2008, 05:42 PM
Ken Irving
 
Default Shell script function problems

On Sat, Feb 09, 2008 at 12:36:53PM +0000, William Pursell wrote:
> phillinux wrote:
>>
>> I'm trying to write a bash shell script to create user accounts that
>> calls 2 functions. I can't call these functions from the script or the
>> command line. The set command seems to show the loaded script in the
>> shell (loaded with . FunctionName at command line) with other
>> environmental variables. ALSO: The type command does not recognize the
>> function.
>>
>> The script and functions work on my laptop running Fedora (with one
>> small glitch). Is there something I can do to my Debian server to get
>> functions recognized??
>
> From some other comments in the thread, it sounds like you are trying
> to do something like the following:
>
>> $ cat foo
>>
>> g()
>> {
>> printf 'in g
'
>> }
>> $ cat bar
>> #!/bin/sh
>>
>> g
>> $ . foo
>> $ ./bar # won't work since bar doesn't source foo
>> ./bar: line 3: g: command not found
> ...
> If you source the file with the function definitions, it makes
> the functions available in the current shell, but not in subshells
> that you may invoke.

A qick perusal of bash(1) shows that functions can be exported to
subshells, e.g.:

$ . foo
$ ./bar
./bar: line 2: g: command not found
$ export -f g
$ ./bar
in g

I suppose that could be (mis)used to implement some form of polymorphism
in shell scripts; the script executes g, but has no control over what g
actually is. Could be useful/confusing/dangerous...

I guess the same applies to sourcing function definitions from within
a script, since the function is still external to the script, though
it's at least defined in a file.

Ken

--
Ken Irving, fnkci+debianuser@uaf.edu


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 02:56 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org