LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2007, 02:47 PM   #1
elie nasrani
LQ Newbie
 
Registered: Jul 2007
Posts: 11

Rep: Reputation: 0
using su inside a bash file


hello, i'm trying to create a bash file that will do multiple tasks using multiple users, i'm stuck where i'm trying to switch from a user to another for example:

Code:
su - user1
task1
exit (here exit will exit the whole bash, is there another way to switch between users ? )
su - user2
task2
exit
 
Old 08-22-2007, 04:07 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Have you tried executing in a subshell?
Code:
( su - user1; task1; exit )
( su - user2; task2; exit )
 
Old 08-22-2007, 04:59 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You can do this:
Code:
barnabas:/home/ada/tmp # whoami
root
barnabas:/home/ada/tmp # cat test
su - ada << 'EOF'
echo "I am $(whoami)."
EOF
echo "Now I'm $(whoami)."
barnabas:/home/ada/tmp # ./test
I am ada.
Now I'm root.
barnabas:/home/ada/tmp #
 
Old 08-22-2007, 10:10 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Try the -c switch

su - user1 -c "your cmd/script here"
su - user2 -c "your cmd/script here"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 10:56 AM
bash: test for substring inside string ali_bush Linux - General 1 03-08-2007 05:34 PM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 07:48 PM
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 06:24 PM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 09:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration