1

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)

Min Chen
  • 41
  • 3

3 Answers3

0

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.

gui.co
  • 51
  • 3
  • Hi gui.co, I forgot to put the seed in the question. Actually I did use a seed, like ./test.sh 405 and I got the same error. And the same script works in another environment with bash version 4.1.2(1)-release (x86_64-redhat-linux-gnu) – Min Chen Apr 12 '18 at 12:54
  • From what you said in your two comments, it seems that `openssl` is not installed on your machine. That is why you get this `/dev/fd/63: end of file` error. Try to install it and redo either your command or oliv's. – gui.co Apr 13 '18 at 12:00
0

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 *
oliv
  • 12,690
  • 25
  • 45
  • Hi oliv, I want a random sort but keep the seed fixed so that the result does not change. I have modified this in the original question. Also, I tried your code, got the following error openssl: command not found sort: /dev/fd/63: end of file – Min Chen Apr 12 '18 at 12:38
0

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)

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57