无法执行 bash 脚本(意外元素 '(' )

无法执行 bash 脚本(意外元素 '(' )

我创建了一个脚本来检查是否安装了 Node、Npm、Bower 和 Susy,但是当我执行它时,我收到一个无法解决的错误。

这是脚本:

    isInstalled(){
  command -v $1 >/dev/null 2>&1 || command -v $2 >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed.  Aborting."; return false;}  
}

installNode() {
  if [[ !isInstalled('node', 'nodejs') ]]; then
    echo "Node is not installed. Installing..."
    curl https://www.npmjs.org/install.sh | sh
  fi
}

installBower()
{

   if [[ !isInstalled('npm') ]]; then
     echo "Npm is not installed. Installing..."
     curl -L https://npmjs.org/install.sh | sh
   else
     echo "Npm is installed. Checcking Bower..."
   if [[ !isInstalled('bower') ]]; then
     echo "Bower is not installed. Installing..."
     npm install -g bower
   fi

}

installSusy()
{

  if [[ !isInstalled('npm') ]]; then
     echo "Npm is not installed. Installing..."
     curl -L https://npmjs.org/install.sh | sh
   else
     echo "Npm is installed. Checcking Bower..."
   if [[ !isInstalled('bower') ]]; then
     echo "Susy is not installed. Installing..."
     npm install susy
   fi


}

这是错误消息:

begin.sh: 6: begin.sh: Syntax error: "(" unexpected (expecting "then")

答案1

中的函数bash就像命令一样被调用,而不是像其他语言中的函数一样。而不是isInstalled('node', 'nodejs'),执行:

isInstalled 'node' 'nodejs'

条件if如下:

if ! isInstalled 'node' 'nodejs';
then
    ...

相关内容