开发者

Simple shell script works in cygwin but not on solaris

I'm trying to store the output of a bash command in a variable. I'm pretty new to shell scripting, but so far, I have the following:

#!/bin/sh
MOSTRECENTMOD="$(开发者_高级运维echo | ls -t | head -n1)"
echo "$MOSTRECENTMOD"

This works just fine in cygwin, but not on Solaris. Any ideas? I am using Unix newlines. I tried a lot of things, but it just doesn't seem to work.

On Solaris, it just outputs:

$(echo | ls -t | head -n1)

when run.

Thank you.


The standard /bin/sh on Solaris is fairly strictly a System V Bourne Shell. It is categorically not a POSIX-compatible shell, and does not understand the $(...) notation (amongst quite a number of other differences, of greater or lesser importance). That means to continue use /bin/sh, you will need to change the $(..) notation to use backticks (which are a pain to show in inline Markdown - I've tried all sorts of sequences without success:

`...`

Alternatively, if your machine has /bin/bash, specify that on the shebang line; otherwise, specify /bin/ksh which does support $(...) notation and many other useful features.


I believe the $( ... ) syntax is from Bash, not standard sh. Use backquotes (`echo | ls -t | head -n1`) or change #!/bin/sh to #!/bin/bash (assuming that exists on your system).


I think your problem is that you have too many quotes. Get rid of the quotes on the first line, and it should work.


Solaris 10 and older /bin/sh isn't POSIX compatible for legacy/upward compatibility reasons. If you want to keep your script unchanged, which I would recommend, you need to switch to a shell that support the posix $() notation, like /bin/ksh, /usr/xpg4/bin/sh or bash.

eg:

#!/bin/xpg4/bin/sh
MOSTRECENTMOD="$(echo | ls -t | head -n1)"
echo "$MOSTRECENTMOD"

Note also that Solaris 11 (Express) has a compatible /bin/sh so this won't be required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜