[英]How to combine 2 alias commands in bash?
For combining 2 alias commands.用于组合 2 个别名命令。 below are the commands to do it.
以下是执行此操作的命令。
command1
instantly generates list of id's; command1
立即生成 id 列表; 12345, 45687, 12345, 45687,
command2
takes one of the above value as input (manualy entering); command2
将上述值之一作为输入(手动输入); command2 \!=1
(command2 12345) command2 \!=1
(命令 2 12345)
how can i combine both commands together in one line (inside alias file) something like alias test command1 | exec command2 \!=1
我怎样才能将这两个命令组合在一行中(在别名文件中),比如
alias test command1 | exec command2 \!=1
alias test command1 | exec command2 \!=1
Note: Once list generated using command1, it should wait for my input.注意:使用 command1 生成列表后,它应该等待我的输入。 time taken for command1 is approximately 30 seconds.
command1 花费的时间大约为 30 秒。 Intention is to avoid writing a lengthy script and maintaining!
目的是避免编写冗长的脚本和维护!
I have tried following;我试过以下;
command1 | exec command2 \!=1
command1 | exec command2 \!=1
command1 | exec -c command2 \!=1
command1 | exec command2 \!=1
command1 | exec -c command2 \!=1
command1 | exec -c command2 \!=1
I'm not 100% clear on what exactly OP's command1
generates so the following may need some tweaking...我不是 100% 清楚 OP 的
command1
到底生成了什么,所以下面可能需要一些调整......
As others have commented, and in light of the (some level of) complexity of the requirement, functions may be preferred over aliases.正如其他人所评论的那样,并且鉴于需求的(某种程度的)复杂性,函数可能比别名更受欢迎。
One idea using functions:使用函数的一个想法:
command1 () { echo 12345 45687 98765; }
command2 () { command1 | awk -v n="$1" '{print $n}'; }
Taking for a test drive:参加试驾:
$ command2 1 # print 1st arg from command1 output
12345
$ command2 2 # print 2nd arg from command1 output
45687
$ command2 3 # print 3rd arg from command1 output
98765
Taking it a bit further and assuming OP needs the ability to dynamically define the first 'command' during the call to the second 'command':更进一步,假设 OP 需要在调用第二个“命令”期间动态定义第一个“命令”的能力:
command1 () { echo C1.12345 C1.45687 C1.98765; }
command2 () { echo C2.abcde C2.defgh C2.wxyz; }
command3 () { echo C3.Hello C3.world; }
commandX () { "$1" | awk -v n="$2" '{print $n}'; } # run command '$1' and pipe results to 'awk' to print the '$2'th field
Taking for a test drive:参加试驾:
$ commandX command1 3
C1.98765
$ commandX command2 2
C2.defgh
$ commandX command3 1
C3.Hello
NOTES:笔记:
command1
generates so it's likely the function code may need to changecommand1
生成什么,因此功能代码可能需要更改command2
(eg, just one arg? must be a positive integer? what to do if larger than the number of fields generated by command1
?, etc) command2
的输入(例如,只有一个 arg?必须是正整数?如果大于command1
生成的字段数怎么办?等)awk
for demonstration purposes;awk
进行演示; there's nothing to keep OP from replacing the awk
code with something else that performs the desired operation on the output from the 1st 'command'awk
代码替换为对第一个“命令”的输出执行所需操作的其他代码command[123]
) for demonstration purposes;command[123]
) 进行演示; there's nothing to keep OP from defining commandX
to take any command (function, script, binary) as its 1st argument... even going so far as to allow for command line args to be fed to the 1st argument... but now we're getting a bit more complicated without more details of OP's actual requirementcommandX
以将任何命令(函数、脚本、二进制)作为其第一个参数……甚至允许将命令行参数提供给第一个参数……但现在我们在没有 OP实际要求的更多细节的情况下变得有点复杂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.