1

How to call shell script from another shell script from another user?

Can you please share the sample code?

I have two scripts:

showdate.sh:

#! /bin/bash
echo "Date is : `date`"

and mainscript.sh:

 #! /bin/bash
 su - nmsadm 
 echo " You are logged in as `whoami`"
 echo "`./showdate.sh`"

When I am executing mainscript I want to switch to other user and run showdate script.

Here is the output

#./mainscript.sh    
You have new mail.
-bash-3.2$

It switches to nmsadm user but does not execute show script.

1 Answer 1

2

To call the script as another user, use the following syntax:

sudo -u nsadm ./showdate.sh

Make sure the showdate script is executable

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.