Init script behaving differently when run on machines in the UK vs US
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Init script behaving differently when run on machines in the UK vs US
I have an initscript that behaves slightly differently in the UK than it does in the US. Not a huge issue, but I'd like to know why and how to fix it. I imagine it has something to do with localization, but I'm at a loss as to what it could be, as it's just bash.
If you used #!/bin/sh then it is possible you use different shells (as /bin/sh is usually just a symlink to some other shell). I know for a fact that for example dash does not support the rather quirky quoting ($"string") you used for the echo command; why you did not use standard double quotes (just dropping the first $) is a mystery to me. Perhaps you want the script to only work if you use bash or its variants?
(dash version 0.5.5.1-7ubuntu1 does not fail in any spectacular fashion, it interprets the initial $ just as if it was \$. Earlier versions might handle this differently.)
I cannot see how localisation could affect this. The only possible cause for the output I can think of -- assuming your description of the situation is exact -- is that the other shell does not support that quoting mechanism, and eats everything up to the final | as part of the bit it does not understand, only echoing the rest of the string.
Last edited by Nominal Animal; 07-19-2011 at 07:25 PM.
Reason: dash 0.5.5 note
The $' ... ' quoted string-expansion construct is a mechanism that uses escaped octal or hex values to assign ASCII characters to variables, e.g., quote=$'\045'. You'll find that many init scripts do this:
The $' ... ' quoted string-expansion construct is a mechanism that uses escaped octal or hex values to assign ASCII characters to variables, e.g., quote=$'\045'.
Correct but bash ANSI-C quoting is not what Nominal Animal wrote about or what is used in the script. The script uses Locale Translation ($" ... ") which could behave differently in different locales such as US and UK.
I did not know Bash used that for locale translation either.
It was originally added for Bash 2.0, according to the CHANGES file. ksh93 seems to support the same.
The $"..." implementation is also a problem for users of certain character sets (BIG5, BIG5-HKSCS, GBK, GB18030, SHIFT_JIS, JOHAB); some of their glyphs cannot be used in localised strings at all, or Bash will execute parts of the strings as commands. The reason is the order of evaluation. First, Bash looks up the string in a message catalog. Then, the resulting string is evaluated just as if it was in normal double quotes, including commands ($(..) and `..`).
This means that an unescaped exclamation point (!) in the translated string will produce rather surprising results, if history substitution happens to be enabled (set -H or interactive shell).
None of this will work in a generic POSIX shell (#!/bin/sh) at all, so don't be fooled by the examples you might see on the web. A lot of us have /bin/sh symlinked to something other than /bin/bash. In fact, I kind of assumed a service script to use /bin/sh and not /bin/bash, since service scripts are supposed to be portable between systems; that's why I didn't check the Bash documentation before replying.
While the $"..." localisation method is in my opinion quite useful (certainly simpler than using the gettext shell command), it should be used with care; noting at least the portability issues and the quoting rules for the translated messages, especially '!'. It is unfortunate that the Bash manual does not bother to explain any of the caveats.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.