Operating System - Linux
1753554 Members
4392 Online
108795 Solutions
New Discussion юеВ

nested 'su' via script - help

 
SOLVED
Go to solution
Maaz
Valued Contributor

nested 'su' via script - help

# useradd manager
# useradd clerk

I want to create a script that login as "manager", then create a file, do some other work, then login as clerk, do some work and exit.

I w'll execute that script from cron(root's cron)
# cat script.sh
#!/bin/bash
su - manager

ls -l && touch manager.txt && exit && su - clerk && touch clerk.txt && exit


please help, this script doesnt work, it just login as "manager" then do nothing more.

Regards

2 REPLIES 2
smatador
Honored Contributor
Solution

Re: nested 'su' via script - help

Hi,

You could try su - manager -c "command"
Or you can try sudo
Dennis Handly
Acclaimed Contributor

Re: nested 'su' via script - help

Once you su, it wants to read from stdin for more commands. I would suggest you do what smatador suggests with -c.
You could try a here document and not nest:
su - manager <ls -l && touch manager.txt
EOF
su - clerk <touch clerk.txt
EOF