LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 01: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, 03:07 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Have you tried executing in a subshell?
Code:
( su - user1; task1; exit )
( su - user2; task2; exit )
 
Old 08-22-2007, 03: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, 09:10 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 09:56 AM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 05:24 PM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM

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

All times are GMT -5. The time now is 09:51 AM.

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
Open Source Consulting | Domain Registration