Page 1 of 2

Strange behaviour with tail command

Posted: 10. May 2023, 07:17
by percoco2000
Hi to everyone... First post on the board.
Developing a bash script, i faced this issue:
The command
echo Mylongstring | tail -c +2 gives me this error:

tail: impossibile aprire '+2' per la lettura: File o directory non esistente
(Transalted : can't open '+2' for reading: File or Directory not exist)

The similar command :]echo Mylongstring | tail -c -2 works.

On my other distro (linux Mint ) both commands works as expected
but on mint i've tail version 8.26, on salix tail version 9.0

Anyt advice? Thanks

Re: Strange behaviour with tail command

Posted: 10. May 2023, 12:58
by percoco2000
Found a workaround...
Downloaded coreutils 9.3 + slackbuild from slackware current source tree, compiled but the issue remain, but if compile outside the slackbuild, from a vanilla source, the resulting tail works as expected. So hase to be something related to the patch the slackbuild introduce.

Re: Strange behaviour with tail command

Posted: 10. May 2023, 17:42
by gapan
Hah, nice find. Yes, this looks like a bug in the coreutils package that comes with slackware. I'll investigate further...

Re: Strange behaviour with tail command

Posted: 10. May 2023, 22:12
by laprjns
The long option, --bytes works

Code: Select all

rich[~]$ tail --h
...
Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=[+]NUM       output the last NUM bytes; or use -c +NUM to
                             output starting with byte NUM of each file

Code: Select all

rich[~]$ echo Mylongstring | tail -c +2
tail: cannot open '+2' for reading: No such file or directory

Code: Select all

rich[~]$ echo Mylongstring | tail --bytes=+2
ylongstring

Re: Strange behaviour with tail command

Posted: 11. May 2023, 06:59
by percoco2000
So in the slackware/salix version the use of -c +XX is invalid? If so, why compiling without the slackbuild generate a working binary?

Re: Strange behaviour with tail command

Posted: 11. May 2023, 09:13
by gapan
It's probably one of the patches that slackware applies. Probably an unwanted and unnoticed side effect. According to the man page, it should be working. I don't think it's intentional, rather a bug.

Re: Strange behaviour with tail command

Posted: 12. May 2023, 23:36
by gapan
Actually, it's not the patches and it's not a bug. It's how the default behavior is for slackware. You can override it with:

Code: Select all

export _POSIX2_VERSION=200809
The default in Slackware is "199209", with this note in the SlackBuild:

Code: Select all

# Compilation with glibc version later than 2.3.2 needs the environment
# variable DEFAULT_POSIX2_VERSION set to 199209.
# Without that line, the coreutils will start complaining about 'obsolete'
# command switches, like "tail -20" will be considered obsolete.
# This behaviour breaks many other packages... the 'obsolete' parameters are
# too commonly used to disregard them.  Better to stick with the older more
# widely accepted standards until things begin to demand the new way.
You can read more here: https://www.gnu.org/software/coreutils/ ... mance.html

Re: Strange behaviour with tail command

Posted: 13. May 2023, 07:07
by percoco2000
So it's not a bug, it's a feature :lol: :lol: :lol:

Thankyou..

Re: Strange behaviour with tail command

Posted: 17. May 2023, 07:44
by percoco2000
Was looking this morning at the online help ...

Code: Select all

 -c, --bytes=[+]NUM       output the last NUM bytes; or use -c +NUM to
                             output starting with byte NUM of each file
And according to this i understand that :
set NN=3

-c +NN output starting from NN bytes, so mylongstring --> longstring (starting from 3rd bytes)
--bytes=+NN output the last NN bytes, so mylongstring --> ing (the last 3 bytes)

Obviously it does'nt work this way ..... ?????

Re: Strange behaviour with tail command

Posted: 17. May 2023, 09:59
by laprjns
You get different behaviors with and without the "+"
-c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to
output starting with byte NUM
of each file
rich[~]$ echo Mylongstring | tail --bytes 3
ng

rich[~]$ echo Mylongstring | tail --bytes +3
longstring

rich[~]$ echo Mylongstring | tail -c 3
ng

rich[~]$ echo Mylongstring | tail -c +3
longstring