简体   繁体   English

如何在 bash 中组合 2 个别名命令?

[英]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:笔记:

  • again, it's not clear (to me) what OP's command1 generates so it's likely the function code may need to change再次,(对我而言)不清楚 OP 的command1生成什么,因此功能代码可能需要更改
  • OP will want to add some logic to validate the input to command2 (eg, just one arg? must be a positive integer? what to do if larger than the number of fields generated by command1 ?, etc) OP 将要添加一些逻辑来验证对command2的输入(例如,只有一个 arg?必须是正整数?如果大于command1生成的字段数怎么办?等)
  • in both sets of code I've used 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'没有什么可以阻止 OP 将awk代码替换为对第一个“命令”的输出执行所需操作的其他代码
  • in the 2nd set of code I've used other functions ( 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 requirement没有什么可以阻止 OP 定义commandX以将任何命令(函数、脚本、二进制)作为其第一个参数……甚至允许将命令行参数提供给第一个参数……但现在我们在没有 OP实际要求的更多细节的情况下变得有点复杂
  • if this doesn't address OP's requirement then OP should consider updating the question with a minimal, reproducible example如果这不能解决 OP 的要求,那么 OP 应该考虑用一个最小的、可重现的例子来更新问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM