n.r.
n.r.

Reputation: 919

Sort is not working from inside bash script

I am using a bash script to retrieve data in CSV-style from a database, I sort it and put into an external file.

Everything seems to be correct, but sort (sort (GNU coreutils) 8.26) is not sorting the rows.

That is my bash script:

#!/bin/bash
mongoexport --type=csv ${QUERY} | /usr/bin/sort > export.csv

The content of export.csv remains the same as the original mongoexport-output - it's still unsorted.

The confusing thing is: When I run this command directly from the command line, sort does what it's supposed to do: It sorts the lines.

What I am doing wrong?

Upvotes: 0

Views: 2091

Answers (1)

n.r.
n.r.

Reputation: 919

Feels like I spend days on this problem, but I finally figured out what do to. The key to the solution is - as everytime - the debug log. Ofcourse sort provides a command line parameter called --debug.

This leads this simple and short notice of sort, before returning the unsorted data:

sort: failed to set locale; using simple byte comparison

The solution is to set an environment variable called LC_ALL (for some reasons I dont know yet, on Ubuntu I had to use the LANG env variable). So that would be the content of my export script:

#!/bin/bash
export LANG='en_GB.UTF-8'
export LC_ALL='en_US.UTF-8'
mongoexport --type=csv ${QUERY} | /usr/bin/sort > export.csv

Upvotes: 2

Related Questions