LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2022, 04:33 PM   #1
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382
Blog Entries: 1

Rep: Reputation: 71
Switching user stops script


Trying to automate LFS I came to a problem. There are a few places were you have to switch user and when it finds that line the script stops with the new user at the prompt.

Code:
su - lfs
lfs@DebianKey:~$
That is the line it reads and were the script stops.
Anything I can do to keep going (without manual input)?

There are other places where the user changes as well.
 
Old 03-08-2022, 07:40 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well you can't fault the script as it did exactly as you instructed, ie it logged in as lfs.

As you have not shown what you want the lfs user to do at this point, my guess would be you need to look at the '-c' option for su
 
Old 03-08-2022, 07:52 PM   #3
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Of course it is not the scripts fault. What I want is for the next commands to keep executing (the next series of lines). How can I do that with the -c option?
 
Old 03-08-2022, 08:03 PM   #4
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Trying this, I get
Code:
su -c lfs
bash: line 1: lfs: command not found
The next line is a cat command.
 
Old 03-08-2022, 08:08 PM   #5
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Same thing happens if I use "su -c root" at the top of the script:
bash: line 1: root: command not found

Where the next line is a mkdir line.
 
Old 03-08-2022, 08:12 PM   #6
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by Debian6to11 View Post
Trying this, I get
Code:
su -c lfs
bash: line 1: lfs: command not found
The next line is a cat command.
The -c option is used to pass a command. So in your case it would be

Code:
su lfs -c <command>
You would be prompted for a password then <command> would be executed as the lfs user.
 
Old 03-08-2022, 08:16 PM   #7
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Of course you could also do:

Code:
sudo lfs -c "<command; command; command; ...>"
to execute multiple commands as the lfs user.

Last edited by Mechanikx; 03-08-2022 at 08:25 PM. Reason: Forgot quotes
 
Old 03-08-2022, 08:22 PM   #8
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
But I get an error already. Besides, it is another 150 lines for a start. I shouldn't just stuck them all in one line.
 
Old 03-08-2022, 08:27 PM   #9
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
And I will be switching user in other parts of the script (and the next 2 parts). It's gonna be 3 scripts which if successful I will be joining them.
 
Old 03-08-2022, 09:08 PM   #10
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Well I'm not sure if this would be the best approach but you could use a heredoc.

Code:
su - lfs<<EOF
<password>
echo ""
...
EOF
For <password> you just enter the lfs password. I'm guessing this would be okay since lfs is just a temporary user for building your system so security isn't really a concern. Then put 'echo ""' to print a newline and the rest of the commands.

I tested this out with:

Code:
su - $USER<<EOF
<password>
echo ""
whoami
EOF
and it worked as expected but there might be other considerations I'm unaware of.
 
Old 03-08-2022, 09:25 PM   #11
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Thanks for your time.
after some more careful studying, it looks like there was an su root at the beginning, an su lfs (for a short time) and back to su root. I bypassed the first one by removing it and switching to root before the start and decided to commend out the lfs user lines and do all of it as root. The documentation says that the user is set up basically to avoid doing mistakes in the host system so I guess it's okay to leave him out. I went further down the script and stopped in a makefile error. So I guess this thread is over, no need to do anything about it, just by pass it. Thanks again.

Seeing your last answer, it's like you laid a trap for this. Good one. Maybe it's gonna be useful for someone coming on this thread.
 
Old 03-08-2022, 09:39 PM   #12
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by Debian6to11 View Post
Thanks for your time.
after some more careful studying, it looks like there was an su root at the beginning, an su lfs (for a short time) and back to su root. I bypassed the first one by removing it and switching to root before the start and decided to commend out the lfs user lines and do all of it as root. The documentation says that the user is set up basically to avoid doing mistakes in the host system so I guess it's okay to leave him out. I went further down the script and stopped in a makefile error. So I guess this thread is over, no need to do anything about it, just by pass it. Thanks again.

Seeing your last answer, it's like you laid a trap for this. Good one. Maybe it's gonna be useful for someone coming on this thread.
Would you mind posting the original script from the book or giving me the page number of its location?
 
Old 03-09-2022, 12:34 AM   #13
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
It is in the early stages of the book.

https://www.linuxfromscratch.org/lfs...ddinguser.html
 
Old 03-10-2022, 08:14 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
If you are root and use su - lfs you will not be prompted for a password as root can login to all accounts.

As for multiple lines / commands, my suggestion would be to place the lfs protions in there own script and then simply call that script:
Code:
su - lfs -c /path/to/script
This will return to your current script once the lfs one has completed

As a side note, you really should follow the safety recommendations of the LFS manual as it is tried and tested, so I would imagine they have a good reason
 
1 members found this post helpful.
Old 03-10-2022, 10:13 AM   #15
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
If I understood well, you are saying to split the script and issue the command adding the second part to run from it. Good to know, copied that for future reference (as I usually do with useful answers).

I usually follow guidelines, but in this case I do not mind loosing all the stuff I have in the computer which is a test computer.

Edit. This was actually posted on answer #6 but I did not understood it well. Some clarification is needed sometimes.

Last edited by Debian6to11; 03-10-2022 at 10:21 AM.
 
  


Reply



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
mencoder stops before it finishes - stops around 4% - all dvd's dthims Linux - Software 1 09-11-2008 05:25 PM
internet runs-stops, runs-stops........ arnuld Debian 4 05-29-2007 01:02 PM
"USB device stops working after a time" 'fix' stops cdrom device mount/readability adamben Slackware 6 04-18-2007 01:18 PM
VNC Works then stops! Reboot fixes but then it stops again! Leethal Linux - Software 1 02-26-2004 07:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 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
Open Source Consulting | Domain Registration