如何让逻辑不在的壳呢? [英] how to get Logical Not in shell?
本文介绍了如何让逻辑不在的壳呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我测试命令是否这样的存在:
I am testing whether command exist like this:
if hash pm2 2>/dev/null; then
echo "already exist"
else
npm install --global pm2
fi
但其实我只是想做到这一点。
but in fact I just want to do this
if not exist
install it
fi
这个我试过
if [ ! hash pm2 2>/dev/null ]; then
npm install --global pm2
fi
这是不正常
推荐答案
就否定条件在如果
:
if ! hash pm2 2>/dev/null; then
# ^
npm install --global pm2
fi
如果您要使用测试命令 [
,你必须在一个 $()
请将命令来把它评价:
If you want to use the test command [
you have to enclose the command within a $()
to get it evaluated:
if [ ! $(hash pm2 2>/dev/null) ]; then
示例
让我们创建一个空文件:
Example
Let's create an empty file:
$ touch a
和检查,如果它不包含一些文字,如5:
And check if it does not contain some text, eg, 5:
$ if ! grep -sq 5 a; then echo "no 5 here"; fi
no 5 here
$ if [ ! $(grep -sq 5 a) ]; then echo "no 5 here"; fi
no 5 here
这篇关于如何让逻辑不在的壳呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文