0

I wish to invoke an alias as follows:

alias_name arg0 arg1 arg2
  • arg0 = filename
  • arg1 = General string
  • arg2 = String representation of last command

The aim is that by calling the alias and passing it arg0, arg1 and arg2 it will call a function which will append the output of the last command to a file, if the file already exists OR if the file does not exist it should create it and insert the output of the last command.

Note: My intention is for the file to be formatted as follows:

String 1  

Command 1 

Output of command 1


String i

command i

Output of command i


String N

Command N

Output of command N

I have attempted to achieve this in my .bashrc as follows:

alias alias_name='function_name(){ printf "\n\nString_i: $1" >> $0 ; printf "\nCommand_i: $2" >> $0; printf "\nOutput_of_command_i: $(!!)" >> $0 };_log_error'

After I added this command to my .bashrc file and did a source ~/.bashrc I then executed the following command:

ls -l

I then invoked my alias:

alias_name arg0 arg1 arg2

But all that I saw in the termainal was:

>

No file was created.

I'm wondering is this the correct method for achieving such results or should I have an alias which calls a script located somewhere else on the system.

NOTE: One constraint I have is portability, I often ssh into other machines and will need to run this command there. This is why I thought alias's might be the correct way to achieve this as I could simply ssh into the machine in question change the name of the ~./bashrc to ~/.bashrc_disabled then scp my bashrc (containing this new alias) into position, run my command and then scp the result file back to my machine, delete the .bashrc and rename the original .bashrc from .bashrc_disabled to .bashrc

  • 2
    Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though. – Kusalananda Aug 3 '18 at 11:27
  • @ Kusalananda thanks, i'm very new to this, wasn't sure what the correct procedure is. I will define the function and either call directly or use an alias. Thank you – MarkMark Aug 7 '18 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.