views:

2062

answers:

3

I can't get --random-sort to work with the sort command on a Fedora Linux-system.

Some context information:

$ cat /etc/fedora-release
Fedora release 7 (Moonshine)
$ which sort
/bin/sort
$ man sort | grep -A 2 '\-R'
       -R, --random-sort
              sort by random hash of keys

$ man sort | grep -A 3 '\-R'
       -R, --random-sort
              sort by random hash of keys

       --random-source=FILE

And the test:

$ echo -e "2\n1\n3\n5\n4"
2
1
3
5
4
$ echo -e "2\n1\n3\n5\n4" | sort -r # Sort in reverse order
5
4
3
2
1
$ echo -e "2\n1\n3\n5\n4" | sort -R # Sort in random order
1
2
3
4
5
$ # Fail! That's not random (I've tried it multiple times.)
+1  A: 

It works on my Ubuntu 8.04 machine. Maybe the problem is the source of randoms. From the manual:

--random-source=FILE

         get random bytes from FILE (default /dev/urandom)
# this should give always the same result:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/zero

# this should be random:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/urandom
João da Silva
As default is supposed to be /dev/urandom it shouldn't matter. I tested it, and it didn't matter. It was worth a try though!
DeletedAccount
+1  A: 

I don't know if bash works this way, but in ksh there's the "whence" command which tells you exactly what will execute if you were to type the argument as a command, whereas "which" just tells you the first instance of the command in $PATH. For instance:

wembley 0 /home/jj33 > which ls
/bin/ls
wembley 0 /home/jj33 > whence ls
'/bin/ls -FC'

I doubt it's your problem, but a next troubleshooting step would be to specify the exact path (or escape a possible alias with a backslash) for "sort" when you execute it:

$ echo -e "2\n1\n3\n5\n4" | /bin/sort -R

After that, I might suspect an environment or locale setting that's making it wonky. Not necessarily important, but the LC_* variables often have unexpected side effects (the first thing I do on a new box is set LC_ALL=C to turn it all off =)).

jj33
Also keep in mind that sort might be a shell built-in
Magnus Hoff
From man which "searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash". I don't have whence. Exact path gives same result. And LC_ALL=C didn't help. Lots of nice suggestions though! But no dice. :-(
DeletedAccount
bash's version of `whence` is `type`: `$ which echo -> /bin/echo` but `$ type echo -> echo is a shell builtin`
sarnold
A: 

It must have been a faulty installation in school.

DeletedAccount