I'm working on a series of command lines to create files, set variables and such on Linux systems. I cannot tell which shell will actually be used during execution but I do know that it will be one of csh, tcsh, sh, ksh, or bash.
csh and tcsh not being bourne compatible is my main issue at this point since they use different commands/syntax for both setting variables and conditional expressions.
I could simply create a shell script with a shebang to switch to /bin/sh if it is safe to assume it being present.
Therefore I'm wondering: Is it safe to assume sh being installed and available or is there another more elegant way to handle the different shell styles without creating seperate scripts?
CodePudding user response:
It is safe to assume that there is a sh in POSIX compatible systems, but its path may vary. The Open Group specification says to determine the location of the standard sh utility with:
command -v sh
So you could do something like:
"`command -v sh`" <<EOS
echo my super sh script
EOS
But there's a problem: csh doesn't support quoted heredocs, which will make the process of writing the script a real pain.
As you're using Linux, you may directly write a sh script and use the shebang #!/bin/sh or #!/usr/bin/env sh.
Take notice that the variables set in the script won't be exported to the parent shell. For that you need to source the script, in which case the syntax will matter and you'll have to write at least both sh and csh versions of it.
