Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Cannot handle names with spaces/unusual characters in script

Occasional Advisor

Cannot handle names with spaces/unusual characters in script

Hello,

 

I am writing a small script to check filesystem usage by doing remsh to remote hosts. The script does the following:

- Connect remote host an do:

    remsh ${SYSTEM} "du -sk * | sort -k1n > /tmp/fsusage"

- List the contents of the biggest directories in remote system:

     remsh ${SYSTEM} "tail -5 /tmp/fsusage |awk '{print\$2}' |xargs -n1 -i ls -ldF $FIL/{}"

It works well except when it finds directories like these:

======> Result of "du -sk *"

595570  DIR1
658118  XXXX YYY
1906674 DIR2
5752242 Mª Rose
7672140 DIR3


======> List:

/XXXX not found
drwxr-xr-x   8 smbnull    smbnull      30720 Nov 13 02:00 /DIR1/
/Mª not found
drwxr-xr-x   2 smbnull    smbnull         96 Nov 11 10:26 /DIR2/
drwxr-xr-x   2 smbnull    smbnull       2048 Nov 14 07:41 /DIR3/

How can  I handle directory names made up of two o more words? I think it could be done by putting in the awk statement "from field two onwards", but I don't know how to write it...

Best Regards,

3 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Cannot handle names with spaces/unusual characters in script

[ Edited ]

>How can I handle directory names made up of two or more words? I think it could be done by putting in the awk statement "from field two onwards"

 

You could check NF and see if more than 2 fields:

dir = $2

if (NF > 2)

   dir = $2 " " $3

 

But this would fail if more than 1 space.

You could parse $0 and take everything after the first group of spaces.

 

But then your next ls(1) pipeline may also fail.  Better to not have evil spaces.

Honored Contributor Honored Contributor

Re: Cannot handle names with spaces/unusual characters in script

> [...] "from field two onwards" [...]

   Not the best plan, I claim.  You don't want fields, you want
directory names.  A name like "a b" is one thing, but what about
"a    b"?  There are things in "awk" like, say, substr() which might be
useful, but trying to extract a directory name from the "du" output
sounds to me like a direct path to problems.

   I haven't tried anything serious, but I'd probably start with some
kind of "find" command to get the actual names.  For directories, say:

      find . -type d

dyi # ls -l
total 0
drwxr-xr-x   2 root       sys             96 Nov 14 02:18  leading space
drwxr-xr-x   2 root       sys             96 Feb 11  2010 itrc
drwxr-xr-x   2 root       sys             96 Nov 14 02:36 middle    spaces
drwxr-xr-x   2 root       sys             96 Nov 14 02:18 trailing space

(I'd bet that there's a smarter way to do the following:)

dyi # find . -type d -exec echo {} \; | sed -e 's/^\(.*\)$/>\1</'
>.<
>./itrc<
>./ leading space<
>./trailing space <
>./middle    spaces<

   In any case, then, I might feed those names from "find" into separate
"du" commands.  For example:

dyi # find . -type d -exec du -ks {} \;
104     .
96      ./itrc
0       ./ leading space
0       ./trailing space
0       ./middle    spaces

   Depending on exactly what you want, getting everything onto one line
may be difficult.  I'd expect to write a script, and copy that script
around to all the remote systems.

Highlighted
Occasional Advisor

Re: Cannot handle names with spaces/unusual characters in script

Hi,

 

Thank you all for your help. Eventually I managed to put into double quotes the names with spaces.

 

Regards,

 

P.s. ¿How can assign points?