我需要从程序中确定 Linux 命令是否是内置命令。我还需要偶尔运行该内置命令。我将使用别名作为示例,它也适用于其他内置函数。
我发现的一个恰当的答案是使用:
sh -c 'type alias'
Run Code Online (Sandbox Code Playgroud)
这将返回 'alias is a shell builtin',正是我所需要的。但是...我也需要运行它。我尝试过的任何事情都不会这样做。所有这些都在终端窗口中失败(并以编程方式):
sh -c 'alias' (没有任何输出)sh -c 'command alias' (没有任何输出)sh -c 'builtin alias' (sh:1:内置:未找到)sh -c 'type builtin' (内置:未找到)sh -c 'type command' (命令是内置的shell)sh -c 'type type' (类型是内置的外壳)(几乎)不用说,所有这些命令(即没有sh -c)在终端上都可以正常工作。这让我对sh -c命令看似不合理的性质感到困惑。
我的问题是:所有 Linux 版本都是这样吗?我是否缺少开关或设置?那么,我如何从程序中执行内置函数以取回其输出?
我正在开发 Kubuntu 14.04(在 Trisquel 中也是如此);此问题发生在konsole和xterm和程序化调用中。
感谢您的评论和回答,不胜感激。
我确实需要澄清我在做什么:我正在编写一个 CLI 助手 GUI,一个存储最喜欢的 linux 命令并执行它们的程序。
我使用 freepascal/Lazarus,它有一个 TProcess 类,可以启动一个进程并提供对 stdin、stdout 和 stderror 的访问权限。
这对于基于文件的命令(例如 rsync 等)非常有效,并且我输入和输出文本没有问题。我什至可以复制管道,只需将输出从一个进程传递到另一个进程的输入。但... …
我正在尝试使用 Mac OS X 上的 aws cli 创建一个可重用的委托集,以用作我的域的白名单名称服务器。我的 AWS 凭证(我为此目的创建的具有完全管理员权限的 IAM 配置文件,位置设置为us-east-1) 在设置过程中正确输入并被系统接受。
输入命令时
$ aws route53 create-reusable-delegation-set --caller-reference [CALLER-REFERENCE] --hosted-zone-id [HOSTED_ZONE] --generate-cli-skeleton
Run Code Online (Sandbox Code Playgroud)
请求成功,我得到响应:
{
"CallerReference": "",
"HostedZoneId": ""
}
Run Code Online (Sandbox Code Playgroud)
但是当我删除 --generate-cli-skeleton 并输入时
aws route53 create-reusable-delegation-set --caller-reference [CALLER-REFERENCE] --hosted-zone-id [HOSTED_ZONE]
Run Code Online (Sandbox Code Playgroud)
我明白了:
An error occurred (InvalidClientTokenId) when calling the CreateReusableDelegationSet operation: The security token included in the request is invalid.
Run Code Online (Sandbox Code Playgroud)
我的现实是,尽管我的 IAM 凭证有效,并且尽管我使用的配置文件 (donaldjenkins) 具有完全管理员权限,但在所有 aws 服务和所有命令中都被系统地拒绝,而不仅仅是 Route53。
尽管进行了广泛的研究,但我一直无法查明造成这种情况的原因。感激地收到任何建议。
的doskey命令分隔符($T当该命令不产生输出)产生一个空行。
C:\> set "a="
C:\> set "b="
C:\> doskey test=set "a=foo" $T set "b=bar" $T echo ^%a^% ^%b^%
C:\> test
C:\>
C:\> foo bar
Run Code Online (Sandbox Code Playgroud)
注意发出后的空行test,这会影响可读性。
有一个众所周知的^?替代方法$T,它不添加空格,但新命令不继承环境变量。
C:\> set "a="
C:\> set "b="
C:\> doskey test=set "a=foo" ^& set "b=bar" ^& echo ^%a^% ^%b^%
C:\> test
%a% %b%
Run Code Online (Sandbox Code Playgroud)
这里没有空格,反正set启动新命令后无效。
如何去除空行?
更新: 删除了“创建一个单独的进程”行,因为它是错误的。
有没有办法知道Pivotal Cloud Foundry我们使用的是哪个许可版本?
我看到 Public PCF 中提供了一些免费的市场服务,但我们正在使用的许可 PCF 版本中没有这些服务?我假设版本不匹配可能是原因。
我正在尝试使用单击库向命令行应用程序添加帮助。正如官方文档中提到的,
对于命令,会生成一个简短的帮助片段。默认情况下,它是命令帮助消息的第一句话,除非它太长。这也可以被覆盖
使用简单的 @click.command 一切正常:
import click
@click.command()
def cli():
"""This is sample description of script."""
if __name__ == '__main__':
cli()
Run Code Online (Sandbox Code Playgroud)
运行它会从方法的 doscstring 显示脚本的描述:
Usage: example.py [OPTIONS]
This is sample description of script.
Options:
--help Show this message and exit.
Run Code Online (Sandbox Code Playgroud)
但是我需要使用 CommandCollection,因为我正在创建一个由多个命令组成的脚本。以下是官方帮助的示例:
import click
@click.group()
def cli1():
pass
@cli1.command()
def cmd1():
"""Command on cli1"""
@click.group()
def cli2():
pass
@cli2.command()
def cmd2():
"""Command on cli2"""
cli = click.CommandCollection(sources=[cli1, cli2])
if __name__ == '__main__':
cli()
Run Code Online (Sandbox Code Playgroud)
而且我不知道如何向整个命令集合添加描述。到目前为止我尝试过的:
有没有办法检测主机操作系统是 XP、Vista、7、8 还是 10?
runtime.GOOS只返回“windows”。
这样做的原因是因为我试图操作启动文件夹中的文件。
在 Windows Vista、7、8、10 上,它位于:
%appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
在 Windows XP 上,它位于:
Documents and Settings\%username%\Start Menu\Programs\Startup
尝试最简单的 kotlin hello world:
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ ll
total 32
drwxr-xr-x 2 thufir thufir 4096 Oct 27 07:28 ./
drwx------ 46 thufir thufir 16384 Oct 27 06:47 ../
-rw-r--r-- 1 thufir thufir 104 Oct 27 07:27 Hello.kt
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ kotlinc Hello.kt
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: …Run Code Online (Sandbox Code Playgroud) 目前我正在处理一个现有的 Java 项目,我的目标是创建一个命令行界面来向它传递参数,可以这么说来“运行”它。
我正在使用Apache commons cli 库,如下所示:
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class CommandLineParameters {
public static void main(String[] args) {
CommandLineParser parser = new DefaultParser();
Options options = new Options();
options.addOption("a", "abc", true, "First Parameter");
options.addOption("x", "xyz", true, "Second Parameter");
options.addOption("h", "help", false, "Shows this Help");
try {
CommandLine commandLine = parser.parse(options, args);
System.out.println(commandLine.getOptionValue("a"));
System.out.println(commandLine.getOptionValue("x"));
if (commandLine.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParameters", options);
}
} catch (ParseException …Run Code Online (Sandbox Code Playgroud) 我正在尝试从在 AWS 中运行的应用程序获取日志,同时排除包含特定短语的日志:
aws logs filter-log-events --log-group-name $MY_LOG_GROUP --filter-pattern -"dont want"
Run Code Online (Sandbox Code Playgroud)
以上工作。但是,如果我改为尝试:
aws logs filter-log-events --log-group-name $MY_LOG_GROUP --filter-pattern -"dontwant"
Run Code Online (Sandbox Code Playgroud)
我得到:
aws: error: argument --filter-pattern: expected one argument
Run Code Online (Sandbox Code Playgroud)
我假设我的 shell 将它解析为一个附加标志,而不是--filter-pattern. 那么,第一个问题:如何强制 aws cli 将其视为参数?
如果我改为尝试:
aws logs filter-log-events --log-group-name "$MY_LOG_GROUP" --filter-pattern -"GET /healthcheck"
Run Code Online (Sandbox Code Playgroud)
我得到:
An error occurred (InvalidParameterException) when calling the FilterLogEvents operation: Invalid filter pattern
Run Code Online (Sandbox Code Playgroud)
第二个问题:这里有什么问题,我该如何解决?
logging design-patterns filter command-line-interface amazon-web-services
aws configure在终端中编辑密钥的命令是什么?