脚本不能在Solaris上运行

时间:2016-04-20 16:35:23

标签: bash solaris

我正在尝试在SunOS 5.10 Generic_120011-14 sun4v sparc SUNW上运行以下脚本,但我在调整它时遇到了困难。

#!/bin/bash
DIRECTORY=$1
if [ $# -eq 1 ]; then
 if [ -d "$DIRECTORY" ]; then
   find "$DIRECTORY" -mindepth 1 -printf '%y %p\n' | awk '$1=="d"{sub(/.*\//,"&DIR: ")} {gsub(/[^\/]*\//," ")} 1'
 else
   echo "That directory doesn't exist."
   exit 1
 fi
else
 find . -mindepth 1 -printf '%y %p\n' | awk '$1=="d"{sub(/.*\//,"&DIR: ")} {gsub(/[^\/]*\//,"  ")} 1'
fi

命令find没有-printf,也没有-mindepth。任何建议我应该使用什么?

1 个答案:

答案 0 :(得分:3)

问题不是特定于Solaris,问题是您使用的是GNU扩展,因此您的脚本不可移植,即不是POSIX。

有两种方法可以解决这个问题,您需要的GNU实用程序已经安装在Solaris 10计算机上,您只需要告诉您的脚本使用它们,或者它们没有安装,您需要修改脚本使用POSIX或至少Solaris标准选项和语法。

    1. GNU工具
#!/bin/bash
PATH=$PATH:/usr/sfw/bin:/usr/local/bin:/opt/csw/bin
DIRECTORY=$1
if [ $# -eq 1 ]; then
 if [ -d "$DIRECTORY" ]; then
   gfind "$DIRECTORY" -mindepth 1 -printf '%y %p\n' | gawk '$1=="d"{sub(/.*\//,"&DIR: ")} {gsub(/[^\/]*\//," ")} 1'
 else
   echo "That directory doesn't exist."
   exit 1
 fi
else
 gfind . -mindepth 1 -printf '%y %p\n' | gawk '$1=="d"{sub(/.*\//,"&DIR: ")} {gsub(/[^\/]*\//,"  ")} 1'
fi
    1. Solaris工具
#!/bin/ksh
DIRECTORY=${1:-.}
if [ ! -d "$DIRECTORY" ]; then
  echo "That directory doesn't exist."
  exit 1
fi
find "$DIRECTORY" ! -name "$DIRECTORY" -exec \
   ksh -c 'printf "%c %s\n" $(ls -dl "$1" | cut -c1-1) "$1"' sh {} \; | \
   nawk '$1=="d"{sub(/.*\//,"&DIR: ")} {gsub(/[^\/]*\//," ")} 1'