1834255 Members
1879 Online
110066 Solutions
New Discussion

su command

 
SOLVED
Go to solution
Krishnan Viswanathan
Frequent Advisor

su command

Can the "su" command be used to call a function within the script instead of calling a script/binary outside the script?

Example :

#!/bin/sh
---
--
su - user1 -c
su - user2 -c
su - user3 -c

functionname()
{
---
)

I tried it with single/double/back quotes but "su" doesnt like it. Is it known to work or the only way is to call a separate script ?

Thanks
-Krishnan
2 REPLIES 2
Sridhar Bhaskarla
Honored Contributor
Solution

Re: su command

H Krishnan,

-c expects a command|script outside of the current script. So, a function within the same script cannot be passed onto it.

I would do the following in this scenario.

#!/usr/bin/ksh

cat << EOF >> /tmp/script$$
your commands..
EOF

chmod 755 /tmp/script$$

su - user1 -c /tmp/script$$
su - user2 -c /tmp/script$$

rm /tmp/script$$

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rajeev  Shukla
Honored Contributor

Re: su command

with su you can only call external scripts with the -c option.
and those functions you are calling why dont you put them in the script which you are calling by -c option.

Rajeev