Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering GNU split with a custom shell function

Tags:

bash

split

Is there a way to use GNU split --filter with a custom shell function, like

my_func () {
  echo $1
}
split -d 10 INPUT_FILE chunk_ --filter="my_func $FILE$"

which I would expect to output

chunk_00
chunk_01
...

Of course the echo in the custom func is just for expressing my question here, in my concrete case the custom function creates a script that uses the chunks from split as input.

It seems that GNU shell only accepts standard shell commands within --filter.

Any smart way around this?

like image 903
Johann cohen-tanugi Avatar asked Sep 09 '26 16:09

Johann cohen-tanugi


1 Answers

You can do this by exporting the function to the environment, which is available to the sub-shell run by split. For example with bash:

ex.sh

#!/bin/bash

my_func() {
  echo $1
}

export -f my_func

seq inf | split -d --filter='my_func $FILE' /dev/stdin chunk_

If you run it like this:

bash ex.sh | head

The output is:

chunk_00
chunk_01
chunk_02
chunk_03
chunk_04
chunk_05
chunk_06
chunk_07
chunk_08
chunk_09

More details in this answer on UL.

Note that split uses whatever the SHELL variable is set to as the sub-shell to run the --filter command. If you are running a different shell, you may need to add export SHELL=/bin/bash before running split.

like image 68
Thor Avatar answered Sep 13 '26 19:09

Thor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!