Call function as different user within the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call function as different user within the script
# 1  
Old 05-01-2015
Call function as different user within the script

Hello

For HP-UX, ksh shell, is it possible to define functions and call them by another user ? For example
Code:
<function_name> ( ) {
command1
command2
}
su - <user> -c <function_name>

Or the only option is defining the user in the function itself as follows -

Code:
<function_name> ( ) {
su - <user> -c command1
}

Please help.
# 2  
Old 05-01-2015
I don't think that's possible. su is going to create another shell. That shell won't have your functions. You can send su a group of commands. I've wrapped several lines of things inside a single su call in startup scripts..

Code:
su - user -c '
    thing1
    thing2 "$@"
' _ "$arg1" "$arg2" # su

be mindful that it's already in single quotes. You can pass arguments too though to help with keeping the quoting manageable.

Last edited by neutronscott; 05-01-2015 at 09:35 AM.. Reason: mention args/quotes
# 3  
Old 05-01-2015
functions must be defined at the start of every new shell. This can be achievd in ksh by ENV variable pointing to an include file that contains the function.
The perhaps better alternative is
Code:
<function_name> ( ) {
su - <user> -c '
command1
command2
'
}


Last edited by MadeInGermany; 05-01-2015 at 09:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0. fun1() { t=$1 printf "%02d\n", $t % 60; } echo "Hi There 23" | awk '{print $0; system(fun1 $3)}' Any suggestions what to be modified in above code to achieve requirement.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

2. Shell Programming and Scripting

Call function from another script

Hey, i got this 2 file. When i try to pick option 1, which is test1, it says ./test: test1: not found. Any idea on how i can fix it? #!/bin/sh QUIT=0 `dirname $0`/testfile while ; do testmenu read option case $option in 1) test1 ;; 2) test2 ;; 3) echo... (2 Replies)
Discussion started by: Nick1097
2 Replies

3. Shell Programming and Scripting

Script – Function Call

Hello, I have Individual function in my shell script , Function1 { Master activities } Function2 { Sub activities 1 } Function3 { Sub activities 2 } … (2 Replies)
Discussion started by: Shanks
2 Replies

4. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

5. Shell Programming and Scripting

Shell Script to call another function

Here is the following code : 1. # gcc -c test firstprog.c the above command will generate a executable file called "test " in which ever directory it is run. Assuming It will also return a value. 2. In the below SCRIPT . test is a file generated by compiling a c program... (3 Replies)
Discussion started by: Vabiosis
3 Replies

6. Shell Programming and Scripting

error when call function in bash script

Dear all, Could you please advice as I when call function i found the following error " refills: command not found" note that refills is function name. following also the function and how i call it function refills { echo "formatting refills and telepin" >> $log awk -F,... (20 Replies)
Discussion started by: ahmed.gad
20 Replies

7. Shell Programming and Scripting

Call Function in .pm as another user

Hello, I need to call a function which reside in some package moudle (.pm) as another user by using su -. Are anyone know how can I do it? Thanks (1 Reply)
Discussion started by: Alalush
1 Replies

8. Shell Programming and Scripting

how can i call a function in shell script

i have a function written in one shell script and i want to call that function in another shell script and use the value returned by that script. can any one suggest me how can i do that? regards, Rajesh.P (4 Replies)
Discussion started by: rajesh.P
4 Replies

9. Shell Programming and Scripting

i want to call a oracle function in my shell script

i want to call a oracle function in my shell script (4 Replies)
Discussion started by: dineshr85
4 Replies
Login or Register to Ask a Question