0
votes

I have searched online and use the following bash script (test.sh) to sort files from the directory with assigned random seeds:

#!/bin/bash
get_seeded_random()
{
  seed="$1"
  openssl enc -aes-256-ctr -pass pass:"$seed" -nosalt \
    </dev/zero 2>/dev/null
}

ls |sort -R --random-source=<(get_seeded_random $1) 

When I run:

./test.sh 405

I got:

sort: /dev/fd/63: end of file

The bash --version i am using is GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

Could some one help me here? it seems to work fine for me in another container where the bash --version is GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

3

3 Answers

0
votes

As far as I understood, your function is looking for a seed that you are supposed to pass as an argument to your test.sh script ($1)

./test.sh <seed>

When your script is doing <(get_seeded_random $1), the file descriptor /dev/fd/63 is used to provide the output of your get_seeded_random() function to sort. Because the function does not have this seed, dev/fd/63 is empty.

0
votes

There are at least 2 problems with your script.

  • Giving /dev/zero as input file for openssl is not a good idea since it has an infinite length
  • $seed is empty which result in the openssl password to be blank

You could solve both problem by something like

sort -R --random-source=<(dd if=/dev/zero count=1 | openssl enc -aes-256-ctr -pass pass:"$seed" -nosalt)

and make you call your script with ./test "mysecretpassword"

But please don't! It's a very ugly way to get random folder entries.

If your only goal is to have the file of your folder in a random order, please use shuf command:

shuf -e *
0
votes

Referring to Shuffle output of find with fixed seed

The --random-source needs to have more bytes than 42 or such small string.


Suggestion: point --random-source to the input file itself, so same input is shuffled same way, if you care for reproducing the experiments (as in my case)