Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用shell判断软件是否已安装?_Shell_Ubuntu - Fatal编程技术网

如何使用shell判断软件是否已安装?

如何使用shell判断软件是否已安装?,shell,ubuntu,Shell,Ubuntu,我的电脑的操作系统是Ubuntu14.04,我想写一个脚本来自动安装一些软件,我需要判断它以前没有安装过,举个例子,我想要 判断是否安装了google chrome,代码如下: if [ !which google-chrome #I knoe this way was wrong] do download it sudo dpkg -i google-chrome*.deb done fi 如果[]错误,我知道判断代码,您能告诉我如何使用shell判断它未安装

我的电脑的操作系统是Ubuntu14.04,我想写一个脚本来自动安装一些软件,我需要判断它以前没有安装过,举个例子,我想要 判断是否安装了google chrome,代码如下:

if [ !which google-chrome  #I knoe this  way was wrong]
do
     download it 
     sudo dpkg  -i  google-chrome*.deb
done 
fi

如果[]错误,我知道判断代码,您能告诉我如何使用shell判断它未安装吗?

您可以使用dpkg检查程序包名称的状态,它会告诉您它是否未安装或未激活,等等。

您可以使用dpkg检查包名的状态,它会告诉您包名是否未安装或未激活,等等。

请注意,
正在搜索包名。看见给定用户可能在其
$PATH
中提到了目录(如
$HOME/bin/
/usr/local/bin/
…),这些目录不应由打包系统管理

不要忘记Linux本质上是一个系统。例如,我的妻子和儿子可以使用同一台电脑。不同的用户可能有不同的
$PATH
设置,因此会使用不同的程序集(例如,通过或从其外壳调用的等效程序…)

如果用户有自己的
google chrome
程序(例如,在他的
$HOME/bin/
中,通过其他程序安装),我认为你不应该在系统范围内安装
google chrome stable
软件包,但你(甚至你的用户)应该决定在这种情况下该怎么做

相反,我会测试是否安装了
googlechrome-stable
软件包(但是,你所做的与你所要求的不同)

也许你可以试试

if dpkg -l google-chrome-stable >& /dev/null ; then
  echo google-chrome-stable is installed
fi
顺便说一句,我不认为在没有用户明确和事先同意的情况下自动安装任何软件包是一个好主意。相反,您应该建议他安装(这是一个非常不同的问题,可能是一个在shell中自动完成的问题)

在我的Debian系统上,
/usr/bin/google-chrome
是指向
/etc/altgenerations/google-chrome
的符号链接,是指向
/usr/bin/google-chrome-stable
的符号链接。所以,我也可以用

if  dpkg -S $(realpath $(which google-chrome)) >& /dev/null ; then
  echo some package gives google-chrome
fi

请注意,
哪个
正在搜索。看见给定用户可能在其
$PATH
中提到了目录(如
$HOME/bin/
/usr/local/bin/
…),这些目录不应由打包系统管理

不要忘记Linux本质上是一个系统。例如,我的妻子和儿子可以使用同一台电脑。不同的用户可能有不同的
$PATH
设置,因此会使用不同的程序集(例如,通过或从其外壳调用的等效程序…)

如果用户有自己的
google chrome
程序(例如,在他的
$HOME/bin/
中,通过其他程序安装),我认为你不应该在系统范围内安装
google chrome stable
软件包,但你(甚至你的用户)应该决定在这种情况下该怎么做

相反,我会测试是否安装了
googlechrome-stable
软件包(但是,你所做的与你所要求的不同)

也许你可以试试

if dpkg -l google-chrome-stable >& /dev/null ; then
  echo google-chrome-stable is installed
fi
顺便说一句,我不认为在没有用户明确和事先同意的情况下自动安装任何软件包是一个好主意。相反,您应该建议他安装(这是一个非常不同的问题,可能是一个在shell中自动完成的问题)

在我的Debian系统上,
/usr/bin/google-chrome
是指向
/etc/altgenerations/google-chrome
的符号链接,是指向
/usr/bin/google-chrome-stable
的符号链接。所以,我也可以用

if  dpkg -S $(realpath $(which google-chrome)) >& /dev/null ; then
  echo some package gives google-chrome
fi

我认为这不是个好主意。我会使用
aptitude search
然后
aptitude install
你能为示例演示一下你的方法吗判断google chrome使用shell@Basile starynkevitchy你应该激发更多的问题,并给出一些更具体的背景(为什么你想这样做很重要,所以编辑你的问题以改进它)。我的回答提供了一些线索。你可能是指“测试”或“检查”而不是“判断”我认为这不是一个好主意。我会使用
aptitude search
然后
aptitude install
你能为示例演示一下你的方法吗判断google chrome使用shell@Basile starynkevitchy你应该激发更多的问题,并给出一些更具体的背景(为什么你想这样做很重要,所以编辑你的问题以改进它)。我的回答提供了一些线索。你可能是指“测试”或“检查”而不是“判断”你能给我一个示例代码如何判断它是否使用[]例如google chrome吗?你能给我一个示例代码如何判断它是否使用[]例如google chrome吗?