It is currently Tue, 17 May 2022 02:58:37 GMT



 
Author Message
 HELP:bash: making tilde expansion after command expansion...

Hello,

i have made the following script:

#!/bin/sh
TMPFILE=log
echo "listing:"
for i in  `cat /etc/passwd | grep home | cut -d: -f1`
do
  echo -n "processing "
  echo -n $i
  echo -n " in "
  echo -n ~`echo $i`
  echo
  echo "`du -ks ~`$i` 2>/dev/null | cut -f1`  $i" >> $TMPFILE
done

what i intended to do was to make a log of the diskusage of all users, this
script has to be ported to a system with a more complicated directory i
structure....

but unfortunately i find no way to make the variable/command expansion be
performed before the tilde expansion....

is there any way?

--
==============================================================
bbo...@erm1.u-strasbg.fr
http://www.**-**.com/ ~bboett



 Tue, 08 Aug 2000 03:00:00 GMT   
 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/>



 Tue, 08 Aug 2000 03:00:00 GMT   
 HELP:bash: making tilde expansion after command expansion...

What about:

#!/bin/sh

TMPFILE=log

for i in `ls /home `
do
du -ks /home/$i
done | sort -nr | sed 50q >> $TMPFILE

Hope this will help you.

Karl Jogvan



 Wed, 09 Aug 2000 03:00:00 GMT   
 HELP:bash: making tilde expansion after command expansion...

On 20 Feb 1998 10:14:37 +0100, u...@rzstud1.rz.uni-karlsruhe.de (Bruno

I would just as soon forget about whether you can tilde expand within
backticks, and try to tackle this in another way.

My suggestion is that because you are reading the user names from
/etc/passwd, then why not go the whole way and extract the home
directory from each record too.

Something like:

IFS=:
while read username passwd uid gid gecos homedir loginshell
do
        echo "Processing" $homedir "of" $username
        du -ks $homedir >> $TMPFILE
done < /etc/passwd

Have fun,
--
Saico



 Thu, 10 Aug 2000 03:00:00 GMT   
 HELP:bash: making tilde expansion after command expansion...

The command you're looking for is eval, but you don't really
need it here. You are already going through /etc/passwd
to get the user names, so you could as well get the home
directories at the same time. So, try something like this:

while IFS=: read id pw uid gid gcos home shell
do
  echo "processing $id in $home"
  du -ks $home | sed "s/$home/$id/" >>$TMPFILE
done </etc/passwd

You might wish to add tests to exclude system users like root
(at least if root's home directory is /), &c.
The while/read structure is worth remembering in any case.

--
Tapani Tarvainen



 Fri, 11 Aug 2000 03:00:00 GMT   
 HELP:bash: making tilde expansion after command expansion...

This week's useless use of cat award goes to...

Bruno> for i in  `cat /etc/passwd | grep home | cut -d: -f1`
[...rest of shell script deleted...]

And of course, if you've been following along for a week or two, you know
that this (BING!) is a Useless Use of Cat!

Rememeber, nearly all cases where you have:

        cat file | some_command and its args ...

you can rewrite it as:

        <file some_command and its args ...

and in some cases, such as this one, you can move the filename
to the arglist as in:

        some_command and its args ... file

Just another Useless Use of Usenet,

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <mer...@stonehenge.com> Snail: (Call) PGP-Key: (finger mer...@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



 Sun, 20 Aug 2000 03:00:00 GMT   
 
   [ 6 post ] 

Similar Threads

1. tilde + variable expansion in bash

2. Problem with tilde expansion in newer bash shells

3. tilde expansion with variable in bash script

4. Configuring tilde expansion in bash

5. Help: NCSA httpd no tilde expansion

6. Help: NCSA httpd tilde expansion

7. variable expansion on the bash command line

8. tcsh tilde (~) expansion problems hanging (slowing) the shell

9. tcsh tilde (~) expansion problems hanging (slowing) the shell

10. tilde expansion for filename


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software