Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Altered behavior of 'cut' command
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
leifbk
Guru
Guru


Joined: 05 Jan 2004
Posts: 415
Location: Bærum, Norway

PostPosted: Tue Apr 15, 2008 8:24 pm    Post subject: Altered behavior of 'cut' command Reply with quote

I just noticed that the 'cut' command in the new coreutils versions has a subtly altered behavior. I've got a couple of scripts that used to read the file size like this:

Code:
$SIZE=`ls -l $ARCHIVE | cut -f5 -d' '`


Now I find that this command, instead of the file size, shows the group, in this case 'users'. An ls -l of the file shows that there are two spaces between the file attributes and the next field:

Code:
-rw-r--r--  1 leif users 2272635 apr 15 18:08 archive.sql.gz


From a little googling on the phenomenon, it seems like this was the way cut was intended to work. If you're using ' ' as delimiter, then it is a space, not just any whitespace. I've now changed my byte-extracting snippet to use awk instead of cut, as awk uses actual whitespace for field separators:

Code:
SIZE=`ls -l $ARCHIVE | awk '{print $5}'`


Hope that this may help someone.
_________________
Grumpy old man
Back to top
View user's profile Send private message
Earthwings
Bodhisattva
Bodhisattva


Joined: 14 Apr 2003
Posts: 7753
Location: Germany

PostPosted: Tue Apr 15, 2008 9:12 pm    Post subject: Reply with quote

While you're at fixing, replace $ARCHIVE with "$ARCHIVE" to ensure that it works with filenames containing e.g. spaces. Even better:
Code:
SIZE="$(test -e "${ARCHIVE}" && stat -c %s "${ARCHIVE}" || echo 0)"

This one uses stat instead of parsing ls and returns 0 if ARCHIVE isn't set properly.

Moved from Portage & Programming to Documentation, Tips & Tricks.
_________________
KDE
Back to top
View user's profile Send private message
leifbk
Guru
Guru


Joined: 05 Jan 2004
Posts: 415
Location: Bærum, Norway

PostPosted: Wed Apr 16, 2008 12:22 pm    Post subject: Reply with quote

Thanks for the tip, Earthwings. It's maybe a bit over the top for my humble personal database backup and restore scripts, but there are a couple of interesting points that I'll add to my toolbox. I've been working with *nix since 1992, and I'm still learning new things. Funny thing is that whatever weird and twisted command line snippet you'll post online, someone will come up with an even weirder -- and more elegant -- one. Where in the world did you pick up that "stat" thingy? 8O

I just discovered that it's actually the output of ls -l that has changed. On a Gentoo computer that still hasn't been updated with the new coreutils, I notice that there's only one space after the file attributes.
_________________
Grumpy old man
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Thu Apr 17, 2008 12:14 pm    Post subject: Reply with quote

"Since the previous stable release, 6.9, there have been .. almost 500 change sets in coreutils proper.
That doesn't include the 1100+ change sets in gnulib." http://lists.gnu.org/archive/html/coreutils-announce/2008-01/msg00001.html

Ouch! This has hit other production users so it'd be good to quantify the changes that are messing people up, not that I have any light to shed right now.

btw Earthwings, I'd recommend using [[ -e $ARCHIVE ]] as [[ is easier to deal with same as $(..) and significantly more powerful. I appreciate it might have been to enforce the quoting thing, but ime it's better to get people to use [[ from the beginning.
http://wooledge.org:8000/BashFAQ/031 is useful to show people a hint of what they can do with [[ as opposed to [ or test.

Regards,
steveL.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum