Hello,
I've been trying to put together a script for the classic Linux build
procedure,
# Unprivileged
config
make
make test
# As root
make install
The script will be adapted to my particular preferences:
1. Each command (shown below for 'make install') to capture a log like
make install 21 | tee install.log
2. Each command to be followed by an exit test, a la ; echo $PIPESTATUS
to be then used for continuation in case of success or for exiting the
script
in case of failure.
3. On 'make install', to use 'su' exclusively for my _temporary_ venture
into
privileged territory. No 'sudo'.
PROBLEM
To my surprise, in all my attempts, the only way to accomplish a working
script has been to use a stripped-down version of the as_root() for 'su',
_as opposed to_ a direct invocation of 'su'.
The main beauty of the BLFS book's as_root() is it offers the user a
three way _choice_.
However, in my case there's only a Hobson choice, the 'su', thus the
reasonable expectation of being able to avoid using the function and
go to 'su' directly.
[As an aside, such a script (with or without as_root()) could be used for
non-X purposes, like building Perl modules in bulk, for example.]
In my travails, I experimented with two #!/bin/bash scripts.
One OK (_with_ as_root()) and one BAD (calling 'su' directly).
Gory details below.
QUESTION:
Why one works correctly, giving the unprivileged caller the opportunity of
handling the true result of the privileged call, and one doesn't?
Suspects
1. Bash, version 4.3.30(1)-release (i686-pc-linux-gnu)
2. The tee construct. It seems without the 'tee' even a BAD script would
work correctly:
su -c cat /temp/test.txt /temp/test.log 21
Examine the ever _correct_ $? (in the comfortable unprivileged mode now)
3. My clumsiness and lack of familiarity with Bash's finer points.
Note: I did try all those easy tricks like starting another Bash shell,
enclosing the su command in (), in {} etc. Did I miss a \ or two?
Note: In case it is not obvious, I've been using the cat file as a
simulation/replacement of a more expensive make install command.
-
GORY DETAILS
temp-OK script:
as_root()
{
su -c \\$*\\
}
as_root cat /temp/test.txt 21 | tee /temp/test.log ; \
rc=$PIPESTATUS ; if [ $rc -eq 0 ] ; then echo OK ; \
else echo NOK ; exit $rc ; fi
echo $?
exit 111# Check
-
temp-BAD script:
su -c cat /temp/test.txt 21 | tee /temp/test.log ; \
rc=$PIPESTATUS ; exit $rc
echo $?
exit 111# Check
-
RESULTS (still GORY)
[/temp]% echo ALEX test.txt
[/temp]% ./temp-OK ; echo $?
Password:
ALEX
OK
0 # Fine ('test.txt' exists)
0 # Fine ('test.txt' exists)
111
[/temp]% cat test.log # Just a check
Password:
ALEX
--
[/temp]% rm -f test.txt
[/temp]% ./temp-OK ; echo $?
Password:
cat: /temp/test.txt: No such file or directory
NOK
1 # OK
[/temp]% cat test.log # Just a check
Password:
cat: /temp/test.txt: No such file or directory
--
[/temp]% echo ALEX test.txt
[/temp]% ./temp-BAD ; echo $?
Password:
ALEX
0 # Fine? Who knows.
111
[/temp]% cat test.log # Just a check
ALEX
--
[/temp]% rm -f test.txt
[/temp]% ./temp-BAD ; echo $?
Password:
cat: /temp/test.txt: No such file or directory
0 # WRONG!!!
111
[/temp]% cat test.log # Just a check
cat: /temp/test.txt: No such file or directory
Thanks,
-- Alex
--
http://lists.linuxfromscratch.org/listinfo/blfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page