简体   繁体   English

将脚本从Unix移动到linux bash

[英]moving script from unix to linux bash

I have a script that I'm moving from a Solaris 11.3 environment to Oracle Linux 6.9 (used for exporting databases). 我有一个脚本,该脚本正在从Solaris 11.3环境迁移到Oracle Linux 6.9(用于导出数据库)。 After updating various variables and paths the script runs correctly except for one section. 更新各种变量和路径后,脚本会正常运行(只有一节除外)。

What it's supposed to do is check the target directory for other exports and delete those in excess of the indicated keep setting (in this case it's 1). 应该做的是检查目标目录中是否有其他导出,并删除超出指定的保留设置(在本例中为1)的那些目录。 When the 'cleanup' section of the script runs it's properly identifying the files, but not removing them, indicating 'binary operator expected'. 脚本的“清理”部分运行时,可以正确识别文件,但不能删除文件,表明“预期二进制运算符”。

The following is the section of the script that isn't working correctly: 以下是脚本中无法正常运行的部分:

FILE_MASK="${ORACLE_SID}_*.dmp.gz" 
counter=0
if [[ -f ${EXP_DIR}/${FILE_MASK} ]]; then
  #
  # count the files (don't use ls command because it has a limit)
  #
  for CNT in $EXP_DIR/$FILE_MASK
  do
    counter=`expr $counter + 1`
  done
  count=`expr $counter - $KEEP_FILES + 1`
  #
  counter=0
  for I in $EXP_DIR/$FILE_MASK
  do
    counter=`expr $counter + 1`
    FILE_NAME=`basename $I`
    echo "${JOBNAME_SHORT}:  File = ${EXP_DIR}/${FILE_NAME}"
    if [[ ${counter} -lt ${count} ]] ; then
      echo "${JOBNAME_SHORT}:    Removing file."
      rm ${EXP_DIR}/${FILE_NAME}
    fi
  done

The error returned is: 返回的错误是:

/home/oracle/scripts/oraexpdp.sh: line 241: [: /u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz: binary operator expected /home/oracle/scripts/oraexpdp.sh:第241行:[:/u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz:预期为二进制运算符

Line 241 of the script is the line starting with 'if' above. 脚本的第241行是上面以“ if”开头的行。

In searching for a fix there was a reference to using double-bracketing ([[ ]]) with bash, and that still returns the same error. 在搜索修复程序时,曾提到将bash与双括号([[]])一起使用,但仍返回相同的错误。 What do I need to tweak to get this working correctly? 我需要调整什么才能使其正常工作?

Many Thanks! 非常感谢!

Harvey 哈维

[[ -f ${EXP_DIR}/${FILE_MASK} ]] is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists. 由于通配符bash替换, [[ -f ${EXP_DIR}/${FILE_MASK} ]]将给出一组文件,因此不会评估文件是否存在。

In the case where there are no matching files ${EXP_DIR}/${FILE_MASK} will be wildcard string still. 在没有匹配文件的情况下, ${EXP_DIR}/${FILE_MASK}将仍然是通配符字符串。

Assuming that KEEP_FILES will always be 1 or more, you could probably just remove the if [[ -f ${EXP_DIR}/${FILE_MASK} ]] as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name, 假设KEEP_FILES始终为1或更大,您可能只需删除if [[ -f ${EXP_DIR}/${FILE_MASK} ]]就像没有文件的情况一样,它不会删除任何内容,因为那样会只是通配符名称,

in the case of multiple files, it will behave as normal. 如果是多个文件,它将表现正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM