查找Linux上是否有某个应用程序/命令可用?

时间:2021-06-06 23:06:46

I need to know if xclip (the command is installed) because if it is available I want to send a command to it via the system() function, otherwise I want to display a message. Is there a way to know if the command exists?

我需要知道是否安装了xclip(命令),因为如果它可用,我想通过system()函数向它发送命令,否则我想显示一条消息。是否有办法知道该命令是否存在?

Thanks

谢谢

I mean programably

我的意思是可编程的

2 个解决方案

#1


3  

For something like xclip, you can just do:

对于xclip这样的东西,你只需要:

if (system("xclip")==-1) // Check for command execution failed
  ...

and check the return value. A -1 indicates that xclip was not found. Or, you can execute something like:

检查返回值。A -1表示没有找到xclip。或者,您可以执行以下操作:

if (system("which xclip")==0) // Check if command can be found
  ...

and check for a 0 return, indicating no failed arguments.

检查是否返回0,表示没有失败的参数。

#2


0  

The which -s command can take in the name of a command and set $? to 0 if it could find it, 1 otherwise

哪个-s命令可以接受命令的名称并设置$?如果它能找到它,则为0。

#1


3  

For something like xclip, you can just do:

对于xclip这样的东西,你只需要:

if (system("xclip")==-1) // Check for command execution failed
  ...

and check the return value. A -1 indicates that xclip was not found. Or, you can execute something like:

检查返回值。A -1表示没有找到xclip。或者,您可以执行以下操作:

if (system("which xclip")==0) // Check if command can be found
  ...

and check for a 0 return, indicating no failed arguments.

检查是否返回0,表示没有失败的参数。

#2


0  

The which -s command can take in the name of a command and set $? to 0 if it could find it, 1 otherwise

哪个-s命令可以接受命令的名称并设置$?如果它能找到它,则为0。