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

0 shell scripting npm

我创建了一个脚本来检查我是否安装了 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


}
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

begin.sh: 6: begin.sh: Syntax error: "(" unexpected (expecting "then")
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 5

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

isInstalled 'node' 'nodejs'
Run Code Online (Sandbox Code Playgroud)

if条件将如下所示:

if ! isInstalled 'node' 'nodejs';
then
    ...
Run Code Online (Sandbox Code Playgroud)