Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a GNU sed script I use on Linux; it is installed at /bin/sed and it seems it contains GNUisms. I have collaborators using Mac OS X. They have installed (non-GNU) sed, located at /usr/bin/sed, and using Homebrew (http://mxcl.github.io/homebrew/) can install GNU sed as gsed with the coreutils package, located at /usr/local/bin/gsed.

Currently, the script starts with #!/bin/sed -f. How do I modify it so that it can be run on Mac OS X, when GNU sed is installed as gsed?

Another option would be to remove the GNUisms, but this may be a bit hard, as I do not have a Mac OS X install at hand and cannot ask my collaborators to test intermediate versions.

share|improve this question
1  
I have no OS X to test this on, but wouldn't #!/usr/bin/env sed work on both OS X and Linux? That would make things much more portable and it is the reason why env always should be used rather than fixed paths). –  Hennes May 14 '13 at 10:15
    
@Hennes: well, the problem is that on OS X, the sed I want would be called gsed, but your suggestion would work if the script does not contain any GNUisms. –  equaeghe May 15 '13 at 8:40
add comment

3 Answers

up vote 3 down vote accepted

There are a few options:

  • Use #!/usr/bin/gsed -f (assuming it is in /usr/bin) as the shebang everywhere, and make sure that your Linux environments symlink this properly;
  • Remove the GNUisms;
  • Symlink sed to /usr/bin/gsed from a directory that earlier than /usr/bin in the user's $PATH (possibly dangerous);
  • Make a wrapper script that looks something like this:
#!/bin/sh

script=/foo

type gsed >/dev/null 2>&1 && exec gsed -f "$script"
exec sed -f "$script"

Ultimately there either need to be changes to at least one of the environments, or changes to the script itself.

share|improve this answer
add comment

You could have your sed script start with this instead:

\:;s=sed;type gsed >/dev/null 2>&1 && s=gsed; exec "$s" -f "$0" "$@";$:s/^//

That's a no-op in sed (though would slightly degrade your script performance) and when interpreted by a shell would execute with gsed if found or sed if not (both looked up in $PATH).

share|improve this answer
    
Ah, would \:;s=sed;command -v gsed >/dev/null && s=gsed; exec "$s" -f "$0" "$@";$:s/^// then be more portable? –  equaeghe May 15 '13 at 9:01
    
Would \:;`command -v gsed || command -v sed` -f "$0" "$@";$:s/^// be a portable alternative? –  equaeghe May 15 '13 at 9:11
1  
I don't think command -v is significantly more portable than type. Both are optional in POSIX, required by Unix. Some old non-POSIX shells like variants of the Bourne shell, have type but not command -v (though some older ones don't set the exit status to non-zero on command-not-found). posh has neither. I once reported to the LSB that having neither was a problem, they acknowledged it, I though they did something about it, but now quickly looking at the latest spec, there doesn't seem to be any type or command command (!?). –  Stephane Chazelas May 15 '13 at 9:40
1  
Correction. LSB now does mandate both type and command (though I don't see that it says whether -v should be supported). So it looks like type is required by the LSB, but (maybe) not command -v. –  Stephane Chazelas May 15 '13 at 9:46
add comment

(Giving an answer to my own question, or rather, an alternative approach that will work in this situation.)

A sed script ascript.sed, even when they start with a sha-bang, can also be applied from the command line on a target file target.txt as sed -f ascript.sed target.txt. This means that when on Mac OS X, one can call the script as gsed -f ascript.sed target.txt.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.