简体   繁体   中英

How to combine 2 alias commands in bash?

For combining 2 alias commands. below are the commands to do it.

command1 instantly generates list of id's; 12345, 45687,

command2 takes one of the above value as input (manualy entering); command2 \!=1 (command2 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

Note: Once list generated using command1, it should wait for my input. time taken for command1 is approximately 30 seconds. 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 -c command2 \!=1

I'm not 100% clear on what exactly OP's command1 generates so the following may need some tweaking...

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':

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 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)
  • in both sets of code I've used awk for demonstration purposes; 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'
  • in the 2nd set of code I've used other functions ( command[123] ) for demonstration purposes; 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
  • if this doesn't address OP's requirement then OP should consider updating the question with a minimal, reproducible example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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