Bruce Dubbs wrote:
> DJ,
> I'm having trouble understanding the script in alsa-utils:
>
> #!/bin/sh -e
> DEV_BASENAME="${DEVNAME##*/}"
> N="${DEV_BASENAME#controlC}"
> case "$DEV_BASENAME" in
> controlC[0-7])
> x=0
> while [ $x -lt 20 ]
> do
> sleep 1
> if [ -f /usr/sbin/alsactl ]; then
> /usr/sbin/alsactl restore $N
> exit 0
> else
> if [ $x -eq 20 ]; then
> exit 1
> fi
> fi
> done &
> ;;
> esac
> exit 0
>
> 1. What is $DEVNAME and who sets it? This needs to be explained.
>
> 2. As noted earlier, x is never incremented. I suppose we need
> x=`expr $x + 1` just before the done to ensure it works will all shells.
>
> 3. The indentation in the book is wrong. The commands between the
> if../fi constructs should be indented. Also the indentation is too
> much. Use no more than four spaces per level.
>
> 4. Shouldn't the test for alsactl be moved outside the loop? I don't
> see any way the loop will ever succeed beyond one iteration. Either
> /usr/sbin/alsactl exists or it doesn't. If it exists, the loop will
> always succeed the first time. If not, it will loop (forever without
> the x increment; 20 times with it and then always fail.) What did you
> really intend to do here?
As Randy pointed out to me privately, the test for alsactl is really
waiting for /usr to be mounted, if necessary. Given that, I would
recommend the following script from Alexander and modified by me:
#!/bin/sh -e
# This script is called by udevd when a change in a device is
# detected, including initial detection upon bootup.
# udevd sets the environment variables ACTION and DEVNAME.
[ "$ACTION" = "add" ] || exit 0
DEV_BASENAME="${DEVNAME##*/}"
N="${DEV_BASENAME#controlC}"
case "$DEV_BASENAME" in
controlC[0-7])
x=0
while [ $x -lt 20 ]; do
# Wait up to 20 seconds for /usr to be mounted if necessary
if [ -f /usr/sbin/alsactl ]; then
/usr/sbin/alsactl restore $N
exit 0
fi
sleep 1
x = `expr $x + 1`
done &
;;
esac
exit 0
BTW, I haven't seen the ampersand construct "done &" before. I am
assuming it takes the while...done command and places it in the
background (does x need to exported?) and then returns success
immediately to udevd. If so, that line needs a comment:
done & # Put the while command in the background and continue
-- Bruce
--
http://linuxfromscratch.org/mailman/listinfo/blfs-dev
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page