descriptionBSD 2-Clause licensed reimplementation of GNU shuf
owneralessio.chiapperini@nullbuffer.com
last changeThu, 31 Mar 2022 07:15:49 +0000 (31 09:15 +0200)
content tags
add:
README.md

shuf

The shuf utility shuffles its input by outputting a random permutation of its input lines. It is not a one-to-one reimplementation of the shuf(1) utility from GNU coreutils since it lacks some of its command-line options.

Features

Limitations

Build dependencies

The only dependency is a toolchain supporting the following flags:

CFLAGS = -std=c99 -O2 -Wall -Wextra -Wpedantic \
        -Walloca -Wcast-qual -Wconversion -Wformat=2 -Wformat-security \
        -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds \
        -Wbad-function-cast -Wconversion -Wshadow -Wstrict-overflow=4 -Wundef \
        -Wstrict-prototypes -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough \
        -Wpointer-arith -Wswitch-enum \
        -D_FORTIFY_SOURCE=2 \
        -fstack-protector-strong -fPIE -fstack-clash-protection

LDFLAGS = -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code

Otherwise you can just remove the security flags and compile it with

CFLAGS = -std=c99 -O2 -Wall -Wextra -Wpedantic
LDFLAGS =

or pass your own flags to make

make CC=gcc CFLAGS=... LDFLAGS=...

Installation

Clone this repository then

$ make PREFIX=/usr install

This will install the compiled binary under PREFIX (/usr/bin) in this case, if not specified PREFIX will default to /usr/local. For staged installs, DESTDIR is also supported. As the binary does not have any dependency it does not have to be installed before use.

Usage

shuf [-h] [-n count] [-o outfile] [-r] [file]
shuf [-h] -e [-n count] [-o outfile] [-r] [args ...]
shuf [-h] -i lo-hi [-n count] [-o outfile] [-r]

shuf has three modes of operation that control how the input is obtained. By default it takes input from standard input. The input file can be omitted, in this case the program takes the input from the standard input until EOF or ^D is reached. If a file is a single dash (‘-’), shuf reads from standard input. The other two modes can be switched by means of the -e and -i flags:

The three modes cannot be mixed together.

The other options are as follows:

Examples

Output 5 random numbers in the range 0-2:

$ shuf -n 5 -r -i 0-2

Flip a coin ten times:

$ shuf -e -n 10 -r Heads Tails

Select five numbers from 10 to 20:

$ shuf -i 10-20 -n 5

Shuffle what is provided on standard input and write the output to out

$ shuf -o out

Static analysis

Static analysis on the code base is done by using clang's static analyzer run through scan-build.sh which wraps the scan-build utility. The checkers used are part of the Experimental Checkers (aka alpha checkers):

Contributing

Send patches on the mailing list, report bugs using git-bug.

License

BSD 2-Clause FreeBSD License, see LICENSE.

shortlog
2022-03-31 Alessio ChiapperiniRemove extra linemaster
2022-03-31 Alessio ChiapperiniUpdate mailing list and bug tracker
2022-01-21 Alessio ChiapperiniUpdate usage function
2022-01-21 Alessio ChiapperiniUpdate README.md
2022-01-21 Alessio Chiapperinigetopt(3) requires POSIX.1-2001
2022-01-21 Alessio ChiapperiniAdd allowed range information for -i flag
2022-01-21 Alessio ChiapperiniRemove bold with monospace formatting
2022-01-21 Alessio ChiapperiniAdd manpage
2022-01-21 Alessio ChiapperiniAdd README.md
2022-01-21 Alessio ChiapperiniRemove -z flag and add -o flag
2022-01-21 Alessio ChiapperiniShuffle files
2022-01-21 Alessio ChiapperiniAdd scan-build.sh wrapper script
2022-01-21 Alessio ChiapperiniFix possible out-of-bound access in shuffle_inrange
2022-01-20 Alessio ChiapperiniFix wrong swap order in Fisher-Yates shuffle
2022-01-20 Alessio ChiapperiniReplace intmax_t with (signed) long
2022-01-20 Alessio ChiapperiniFix out-of-bounds array access when count > range
...
heads
2 years ago master