Re: md5sum and recursive traversal of dirs

2019-10-12 Thread Assaf Gordon

( adding bug-time@ )

Hello,

On 2019-10-10 11:29 a.m., Сергей Кузнецов wrote:
[...]

By the way, I wrote two new small programs: xchg (or swap, which name is
better?) And exst (exit status).
[...] The second program launches the
program indicated at startup and, after its completion, prints the output
status or the caught signal.


Somewhat related:

the GNU Time program can report both exit code and signal
in the following way:

  $ env time -f "Exit code: %x\n" [SOME PROGRAM that dies with segfault]
  Command terminated by signal 11
  Exit code: 0

However, for a long time I wanted to add a new output format
specifier to GNU time that will indicate whether
a program existed cleaning or with a signal
(and which exit code or signal).

Your message reminded me of that, and I hope to add something
like that in the near future.

It could be something like:

   %T   1 if program terminated by a signal, empty otherwise
   %S   signal number of program terminal by a signal, empty otherwise
   %X   exit code if program terminated normal,
or empty if terminated by a singal

And could be used like so:

  time -f "Signaled: %T (signal number: %S)\nExit code: %X\n" [PROGRAM]


Please send comments and suggestions to bug-t...@gnu.org .

regards,
 - assaf

P.S.
Note that your built-in shell like has its own 'time' function.
To use GNU time run "env time" or "\time" .



Re: md5sum and recursive traversal of dirs

2019-10-10 Thread Kaz Kylheku (Coreutils)

On 2019-10-10 10:29, Сергей Кузнецов wrote:

Hello, I find it strange that md5sum does not yet support recursive
directory traversal. I moved part of the code from ls and added this
functionality. How about adding this? I also added sha3 and md6 
algorithms,

they are in "gl/lib/".


If we have any utility whatsoever that operates on files, sometimes
we want to apply it to every file in a tree.

It does not follow that every utility whatsoever that operates on files
should integrate the code for traversing a tree.

We have ways in the shell, and in other programming languages, to
map any operation over a tree of files.

The mapping mechanism maps, the MD5 mechanism calculates MD5 sums;
each has a single responsibility.

One noteworthy tree traversal mechanism appears as an extension in the
GNU Bourne-Again Shell (Bash). In Bash, if you set the "globstar" option
like this:

   shopt -s globstar'

If this is enabled, then the ** operator becomes active in file globbing
patterns. The ** operator spans across multiple path components. For
instance:

  # calculate the md5sums of all .so files anywhere in /usr/lib
  md5sum /usr/lib/**/*.s

By the way, I wrote two new small programs: xchg (or swap, which name 
is

better?) And exst (exit status).


Exchanging two files can be implemented as a shell function, which can 
be

extremely simple if we don't worry about exchanging files in different
filesystem volumes. Here is a sketch:

  swap()
  {
local tmpname=$(mktemp swap-XX)
# ... check arguments here for count and sanity ...
mv -- "$1" $tmpname
mv -- "$2" "$1"
mv -- $tmpname "$2"
  }


The first program simply changes files,
the number of which can be more than two.


That should proably be called "rotate", like the rotatef operator in
Common Lisp. The logic becomes something like (untested):

   # if we have at least two arguments:
   if [ $# -gt 1 ] ; then
 mv -- "$1" $tmpname

 # while we have two or more arguments
 while [ $# -gt 1 ] ; do
   mv -- "$2" "$1"
   shift
 done

 # last argument gets $tmpname
 mv -- $tmpname "$1"
   fi

Example: rotate some logs:

   rotate deleteme log.2 log.1 log.o log
   rm deleteme

Undoubtedly elegant and useful; but should it be a C program in GNU 
Coreutils? Hardly.



The second program launches the
program indicated at startup and, after its completion, prints the 
output

status or the caught signal.


Doable in shell scripting, again. The status of the last command is 
available

in the $? variable. This can be tested:

   stat=$?

   if [ $(( stat & 0x80 )) != 0 ] ; then
  printf "terminated due to signal %d\n" $((stat & 0x7F))
   else
  printf "exited with status %d\n" $stat
   fi

Bash has "massaged" the value already. That is to say, if the program
terminates normally with an unsuccessful status 19 we don't have to do
any shifting to recover the value from the upper bits of an exit status
word; $? simply holds the value 19.




Re: md5sum and recursive traversal of dirs

2019-10-10 Thread Pádraig Brady

On 10/10/2019 18:29, Сергей Кузнецов wrote:
> Hello, I find it strange that md5sum does not yet support recursive
> directory traversal. I moved part of the code from ls and added this
> functionality. How about adding this?

The same could be said for any filter.
It's better to leave the traversal to dedicated tools like find(1).

> I also added sha3 and md6 algorithms,
> they are in "gl/lib/".

Yes we're considering these, and they've been discussed on the list previously.

> By the way, I wrote two new small programs: xchg (or swap, which name is
> better?) And exst (exit status). The first program simply changes files,
> the number of which can be more than two.

Do you mean to replace file contents with a specified filter?
We were discussing such a `replace` program quite a while back now on the list.

> The second program launches the
> program indicated at startup and, after its completion, prints the output
> status or the caught signal.

Could a shell wrapper achieve the same?
Related to this, I find the following shell prompt very useful:

export 
PS1="[\[\033[1;31m\]\${PIPESTATUS[@]/#0/\[\033[0m\]\[\033[1;32m\]0\[\033[1;31m\]}\[\033[0m\]]
 \w$ "

This shows the exit status in the prompt like:

[0] ~$ true | sleep inf
^\Quit (core dumped)
[0 131] ~$

> I don't know how to commit my code in yours repo (is it even possible?), so
> I write this letter.

Generally patches are posted to this list.
Your first step of soliciting feedback first is the correct one.

cheers,
Pádraig