Rocco Rossi
Rocco Rossi

Reputation: 13

Convert a "test" correctly from POSIX sh to old plain bourne shell

On an old Solaris that only has plain bourne shell, I haven't been able to correctly translate a simple test such as:

[ -d '/export/home/mydir' -o ! -e '/export/home/mydir' -a -d $(dirname '/export/home/mydir') ]

...as is supported under modern POSIX shells such as ash, bash, ksh, &c.

Any suggestions please?

Upvotes: 0

Views: 63

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20909

With portable shell scripting, try avoiding using -a and -o for and and or. Additionally, as others have commented, $() expansion may not be available, but backticks will be.

Try this instead:

if [ -d '/export/home/mydir' ] || [ ! -e '/export/home/mydir' ] && [ -d `dirname '/export/home/mydir'` ]; then

If [ isn't available, you may need to use test instead of [ and remove the closing ]s.

For reference, here is a guide on writing portable shell scripts.

Upvotes: 2

Related Questions