简体   繁体   English

如何检查bash脚本中安装的composer软件包

[英]How do I check that composer package installed in bash script

I'm trying to install composer package if it not installed in bash script. 如果没有在bash脚本中安装composer软件包,我将尝试安装它。 But now it doesn't work, and no_package function always pass 但是现在不起作用了,并且no_package函数总是通过

#!/bin/bash -e

no_package() {
    composer show | grep matchish/laravel-scout-elasticsearch | test
}

if [ no_package ]; then
  composer require "matchish/laravel-scout-elasticsearch"
else
  echo 'Package installed'
fi

UPD: Here is solution UPD:这是解决方案

package_installed() {
    composer show | grep matchish/laravel-scout-elasticsearch --quiet
}
if package_installed; then
  echo 'Package installed'
else
  composer require "matchish/laravel-scout-elasticsearch"
fi

There are two misunderstandings here: 这里有两个误解:

  1. You can't pipe stuff to test . 您无法通过管道进行test
  2. if some_command is the way to do something if a command succeeds. if some_command是命令成功执行操作的方式。 [ no_package ] doesn't actually run the command, it simply checks that the string "no_package" is not empty and therefore always succeeds. [ no_package ]实际上并不运行命令,它只是检查字符串“ no_package”是否为空,因此总是成功。

In addition to this you may want to use the --quiet flag to grep to avoid printing the package name. 除此之外,您可能希望使用--quiet标志来grep以避免打印软件包名称。

I tested the test command, and I found that the test command returned the same result no matter what I piped in. 我测试了test命令,发现无论输入什么命令, test命令都返回相同的结果。

So it is better to run in this way 因此最好以这种方式运行

package_exist() {
    composer show | grep matchish/laravel-scout-elasticsearch >/dev/null
}

if package_exist; then
    echo 'installed'
else
    echo 'uninstalled'
    echo 'installing matchish/laravel-scout-elasticsearch'
    composer require "matchish/laravel-scout-elasticsearch"
fi

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

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