LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Switching user stops script (https://www.linuxquestions.org/questions/programming-9/switching-user-stops-script-4175709138/)

Debian6to11 03-08-2022 04:33 PM

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.

grail 03-08-2022 07:40 PM

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

Debian6to11 03-08-2022 07:52 PM

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?

Debian6to11 03-08-2022 08:03 PM

Trying this, I get
Code:

su -c lfs
bash: line 1: lfs: command not found

The next line is a cat command.

Debian6to11 03-08-2022 08:08 PM

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.

Mechanikx 03-08-2022 08:12 PM

Quote:

Originally Posted by Debian6to11 (Post 6336382)
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.

Mechanikx 03-08-2022 08:16 PM

Of course you could also do:

Code:

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

Debian6to11 03-08-2022 08:22 PM

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.

Debian6to11 03-08-2022 08:27 PM

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.

Mechanikx 03-08-2022 09:08 PM

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.

Debian6to11 03-08-2022 09:25 PM

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.

Mechanikx 03-08-2022 09:39 PM

Quote:

Originally Posted by Debian6to11 (Post 6336401)
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?

Debian6to11 03-09-2022 12:34 AM

It is in the early stages of the book.

https://www.linuxfromscratch.org/lfs...ddinguser.html

grail 03-10-2022 08:14 AM

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 :)

Debian6to11 03-10-2022 10:13 AM

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.


All times are GMT -5. The time now is 11:19 AM.