
HELP:bash: making tilde expansion after command expansion...
On 20 Feb 1998 10:14:37 +0100, u...@rzstud1.rz.uni-karlsruhe.de (Bruno
Boettcher) posted to comp.unix.shell:
> #!/bin/sh
> TMPFILE=log
> echo "listing:"
> for i in `cat /etc/passwd | grep home | cut -d: -f1`
Aaargh, major ugliness.
1) *bing* Useless Use of Cat
2) what if a daemon's name is homer?
3) backticks are not safe, never mind that they're not necessary
here.
grep ':/home/[^:]*:[^:]*$' /etc/passwd | cut -d: -f1 | while read f
> do
> echo -n "processing "
> echo -n $i
> echo -n " in "
> echo -n ~`echo $i`
Can't you just use the explicit path to the home directory you
specifically grepped for up there? But wait, here's yet another pair
of backticks:
> echo
> echo "`du -ks ~`$i` 2>/dev/null | cut -f1` $i" >> $TMPFILE
Why are you writing to a file when all the rest of the script is
writing to standard output? Oh wait, maybe that's intentional.
The backticks inside backticks need to be backslashed for this to
work, but since you already processed that thing once, perhaps you
should cache it instead of recalculate it again?
> done
Try this instead:
grep ':/home/[^:]*:[^:]*$' /etc/passwd | cut -d: -f1,5 |
while IFS=: read user dir; do
echo "Processing $user in $dir"
du -ks $dir 2>/dev/null | cut -f1 | tr -d '\012' >>$TMPFILE
echo " $user" >>$TMPFILE
done
If your sed understands backrefs you could use that instead of the
grep | cut combo but that's a minor savings.
Hope this helps,
/* era */
If you really want an answer to your specific question, try the shell
builtin "eval". Anyway, the variants of /bin/sh I've seen don't even
understand the ~user syntax. (Bash does, and is /bin/sh on Linux, of
course, but that's beside the point. Ah, yes, the subject mentions
Bash but your script says /bin/sh.)
--
Paparazzi of the Net: No matter what you do to protect your privacy,
they'll hunt you down and spam you. <http://www.iki.fi/~era/spam/>