【问题标题】:What is going on in this script - Bash Scripting此脚本中发生了什么 - Bash 脚本
【发布时间】:2013-09-20 19:33:52
【问题描述】:

所以我recently asked about the unexpected token fi 并修复了这些错误。现在我得到了不同的错误。有人可以通读我的脚本并指出或修复您看到的所有错误 - 我是 bash 新手,但没有收到一半的错误。例如:

$ composerrun.sh
./composerrun.sh: line 2: checkForComposerJson: command not found
./composerrun.sh: line 27: syntax error near unexpected token `)'
./composerrun.sh: line 27: `           [Nn]*) '

当我跑步时:

#!/bin/bash


# We need to see if the AisisCore/ Directory exists.
# If it doesn't we will try and create it for you.
# Then to be safe - we check if it exists.
function checkForAisisCore {
    if [[-d "AisisCore/"]]
    then 
        return 0;
    else
       read -p "Would you like us to create the directory?" yn,
       case $yn in
           [[Yy]]*) 
               mkdir "AisisCore/";
               if [[-d "AisisCore/"]]
               then
                   return 0;
               else
                   echo "We could not create the directory as you requested";
                   return 1;
               end
               ;;
           [[Nn]]*) 
               return 1;
               ;;
           *) 
               echo "Please put in either yes or no";
       esac
   fi
}

# We check for the aisis-core inside of vender inside of adam.balan.
# If it exists return true.
# If Not, then alert the user and return false.
function checkForVendor(){
  if [[-d "vender/adam.balan/aisis-core/"]]
  then
      return 0;
  else
      echo "Something is wrong. We could not find the vendor/ folder. Please check that you are running this script inside of your theme or project folder.";
      return 1;
  fi
}

# We need to know if composer.json exists.
# If it does we return true.
# If not - we alert the user.
function checkForComposerJson(){
    if [[-f "composer.json"]]
    then
        return 0;
    else
        echo "You need composer.json with appropriate information about AisisCore";
        return 1;
    fi
}

# ================================ [ Core ] ================================

# Core program.
#
# Check if the composer.json and AisisCore/ exist.
# Run composer install.
# Check for vendor folder.
# If all is good - echo Found Vendor and exit. (For now).
if checkForComposerJson && checkForAisisCore
then
    composer install
    if checkForVendor
    then
        echo "Found vendor"; exit;
    fi
fi

我确定有数百个错误和问题,但我看不到它们,因为我不懂 bash。我认为我在正确的轨道上。我怎么能用另一双眼睛看这个剧本,就像“你在做什么......”



【问题讨论】:

  • 阅读Bash Pitfalls 页面并清理你的空格——上面的一些是语法错误。

标签: bash shell


【解决方案1】:

在使用之前声明你的函数。用双分号分隔 case 子句 (;;)。首选[[]] 而不是[](请参阅this SO discussion),因为它们可以减少意外。

【讨论】:

  • 好的,我更新了代码示例以更好地反映这一点,但它仍然得到相同的错误,减去 ./composerrun.sh: line 2: checkForComposerJson: command not found
  • 第 27 行的错误是因为在 Yy 条件之后,bash 期望您在启动 Nn 条件之前添加仅包含 ;; 的行。通配符之前也是如此。
  • 好的,所以我设法添加了;;,我相信他们应该去的地方,就像你说的,但现在 bash 讨厌他们......就像unexpected token ';;'
  • @LogicLooking 您已经过度替换了方括号。在 case 语句中,[Yy] 表示匹配 Yy 的模式。在 if 语句中,[[ ... ]] 是一个bash 条件表达式,它是test/[ 命令的更强大的替代方案。
  • @chepner 哦-所以[[]] 应该替换为[] ??对于 Yy 和 Nn,如果是这样,它仍然不喜欢 ;;
猜你喜欢
  • 2021-02-16
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多