相关疑难解决方法(0)

对一个参数运行两个命令(无脚本)

如何在一个输入上执行两个命令,而无需两次输入该输入?

例如, stat 命令告诉了很多关于文件的信息,但没有指明它的文件类型:

stat fileName
Run Code Online (Sandbox Code Playgroud)

文件命令,告诉什么类型的文件是:

file fileName
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式在一行中执行此操作:

stat fileName ; file fileName
Run Code Online (Sandbox Code Playgroud)

但是,您必须键入文件名两次。

如何在同一个输入上执行两个命令(无需两次输入输入或输入变量)?

在 Linux 中,我知道如何通过管道传输输出,但是如何通过管道传输输入呢?

command-line

28
推荐指数
4
解决办法
9663
查看次数

您如何回忆上一个与 bash 一起使用的命令的最后(第 n 个?)传递参数?

很多时候我在同一个文件上发出不同的命令。例如:

$ youtube-dl aB54c4p0eo #I made this video id up on the spot
$ mv aB54c4p0eo.flv kittens.flv
$ vlc kittens.flv
$ rm kittens.flv
Run Code Online (Sandbox Code Playgroud)

有没有办法重用当前命令中的参数,这样我就不必重写它?

bash

10
推荐指数
3
解决办法
2562
查看次数

对多个命令重复使用相似的标志

假设我想执行多个具有类似标志的命令,例如:

du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4
tar cvf archive / --exclude=dir1 --exclude=dir2 --exclude=dir3/dir4
Run Code Online (Sandbox Code Playgroud)

我想重用标志序列--exclude并使代码更干净,例如将这些标志保存到变量中。

所以之后上面的命令看起来像

du -sh / "$EFLAGS"
tar cvf archive / "$EFLAGS"
Run Code Online (Sandbox Code Playgroud)

请注意,tar排除相对于给定目录的文件和目录。

最好、最干净的解决方案是什么?

背景

我想使用 tar 备份我的系统并排除一些我不需要的目录。

shell bash variable

5
推荐指数
1
解决办法
928
查看次数

为两个命令提供相同的文件名作为参数

在 git 冲突中,我目前可以执行以下操作:

git diff path/to/some/file.txt
Run Code Online (Sandbox Code Playgroud)

...查看差异后,通常我想做:

git checkout --theirs path/to/some/file.txt && git add path/to/some/file.txt
Run Code Online (Sandbox Code Playgroud)

每次编辑两条路径都很费力,所以我希望能够做到以下几点:

git checkout --theirs <ref> && git add path/to/some/file.txt
Run Code Online (Sandbox Code Playgroud)

其中<ref>指的是 file.txt。

有什么办法可以在 bash 中做到这一点?

shell bash

3
推荐指数
1
解决办法
258
查看次数

标签 统计

bash ×3

shell ×2

command-line ×1

variable ×1