shell: reconcile language and sort behaviour


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell: reconcile language and sort behaviour
# 1  
Old 03-24-2010
shell: reconcile language and sort behaviour

Hi

Don't know if this is a dummy question, but let's give it a try.

I yesterday had a problem with undefined behaviour in the sort shell command (I'm using bash), leading to different sort orders without apparent reasons. I resolved this by typing

Code:
export LC_ALL="C"
export LC_COLLATE="C"
export LC_CTYPE="C"

and adding this to my .bash_profile as well.

The language is still set to english :

Code:
[jos@faba ~]$ echo $LANG
en_US.UTF-8

But now, the shell doesn't recognize special characters anymore, like accented ones (I've lots of them : although my system is in english, I'm in France so I've lots of accents in my filenames).
Even in english text, non-recognized characters do appear (for instance "man rpm" gives "<E2><80><99>" characters).

My question : is it "either-or", i.e. either correct sorting behaviour or correct character handling, or is there a way to have both behave correctly ?

Thanks in advance
jos
# 2  
Old 03-24-2010
Do not set LC_* in your profile. Just run the sort command in a different environment, when needed:

Code:
LC_COLLATE=C sort ...

or

Code:
LC_ALL=C sort ...

# 3  
Old 03-24-2010
Interesting possibility.
Can one set both LC_COLLATE and LC_ALL this way ?

Code:
LC_COLLATE="C" LC_ALL="C" sort ...

??

I found that only setting one of them still leeds to unpredictable sort results.

If this is possible, I could just create a sort alias, I imagine ?
# 4  
Old 03-24-2010
Quote:
Originally Posted by jossojjos
Interesting possibility.
Can one set both LC_COLLATE and LC_ALL this way ?

Code:
LC_COLLATE="C" LC_LANG="C" sort ...

??

I found that only setting one of them still leeds to unpredictable sort results.
In that case use:

Code:
LC_ALL=C sort ...

... and you'll get the expected result.

The multiple assignment should work too, though ...

Code:
% env | grep ^var
%                                       
% var1=a var2=b sh -c 'env | grep ^var' 
var1=a
var2=b

Quote:
If this is possible, I could just create a sort alias, I imagine ?
You could create a function:
Code:
clocale_sort() {
  local LC_COLLATE=C LC_LANG=C
  sort ...
  }

Notice that the local keyword is not standard (some shells use typeset, other do not have local variables (?)).

Or you can just run the command in a "isolated" environment (in a sub-shell):

Code:
(
  LC_LANG=C LC_COLLATE=C
  export LC_LANG LC_COLLATE
  sort ..
  )

# 5  
Old 03-24-2010
Quote:
Originally Posted by radoulov
In that case use:

Code:
LC_ALL=C sort ...

... and you'll get the expected result.
No, I don't, only LC_ALL is not sufficient ... struggled with that yesterday Smilie

So this seems to work :
Code:
alias csort="LC_ALL=C LC_COLLATE=C LC_CTYPE=C sort"

I'm not sure why I should create a function or isolated environment ... nor how to do this exactly, never done before.

Thanks !
# 6  
Old 03-24-2010
If the alias is sufficient, use it (the functions are just more flexible and powerful).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Sort behaviour

I see strange results when sorting with -n options and I wander if somebody can explain it. Input file and two results: $ cat aa 14 -1 11 -1 0 -1 0 $ sort -u aa -1 0 (1 Reply)
Discussion started by: migurus
1 Replies

2. Shell Programming and Scripting

Korn shell behaviour in AIX

Hi, Consider the code snippet below: fun() { while read x do echo $x done < somefile_that_does_not_exists } fun echo I am here Korn shell on HPUX prints the message "I am here", while the behaviour is different on AIX korn shell. We do not get the message on AIX. Any... (5 Replies)
Discussion started by: 116@434
5 Replies

3. What is on Your Mind?

Destroy All Software on strange language behaviour

I'll just leave this here (0 Replies)
Discussion started by: pludi
0 Replies

4. Shell Programming and Scripting

Scripting using shell language

hi i am student and i learn computer sciences i need to write a script that can will be execute automatically when a user logs on a computer, and will be automatically disconnect after a duration that i will determine can somebody help me thank (1 Reply)
Discussion started by: Thucydide
1 Replies

5. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

6. Shell Programming and Scripting

How can i run the shell script from ABAP programming language

I am in need to execute the Files transferring's shell script from ABAP programming language. it would be highly appreciated if you help me as quickly as possible. Thank you (28 Replies)
Discussion started by: Venkat1818
28 Replies

7. Shell Programming and Scripting

Spaces behaviour in shell

Hello, I am a bit puzzled by the way my shell treats spaces in filenames. An example will be way clearer than any explanation I can make: $ ls test\ file\ with\ spaces test file with spaces $ var="test\ file\ with\ spaces" $ echo $var test\ file\ with\ spaces $ ls $var ls: cannot... (4 Replies)
Discussion started by: SDelroen
4 Replies

8. UNIX for Advanced & Expert Users

Sort command - strange behaviour

Hi guys, I have the following example data: A;00:00:19 B;00:01:02 C;00:00:13 D;00:00:16 E;00:02:27 F;00:00:12 G;00:00:21 H;00:00:19 I;00:00:13 J;00:13:22 I run the following sort against it, yet the output is as follows: sort -t";" +1 -nr example_data.dat A;00:00:19 (16 Replies)
Discussion started by: miwinter
16 Replies

9. Shell Programming and Scripting

Help me to resolve uncertian behaviour of a sort command

I have got a file BeforeSort.txt having 40 fields seperated by "|" First field= RecordType (Value will be P or FP) Second field= CamCode Third field = UpdatingDate Fourth field = ProductType Fifth field = ActionCode (Value may be 01, 02 or 03) Sixth field = ProductCode and so on My... (1 Reply)
Discussion started by: pankajrai
1 Replies

10. Shell Programming and Scripting

any explanation for thsi shell script behaviour

hello whats the difference between excuting a shell script as a)sh myscript.sh b). ./myscript.sh i noticed that my shell script works fine when i run it as . ./myscript .sh but fails when i run it as sh myscript.sh could anybody explain why. the shell script is very simple ... (9 Replies)
Discussion started by: xiamin
9 Replies
Login or Register to Ask a Question