What is the correct way to assign a function containing chained commands to an alias in .bashrc?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
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 "nnString_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
linux bash shell-script alias function
add a comment |Â
up vote
0
down vote
favorite
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 "nnString_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
linux bash shell-script alias function
1
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 at 11:27
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 "nnString_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
linux bash shell-script alias function
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 "nnString_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
linux bash shell-script alias function
asked Aug 3 at 11:07
MarkMark
7717
7717
1
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 at 11:27
add a comment |Â
1
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 at 11:27
1
1
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 at 11:27
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 at 11:27
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460301%2fwhat-is-the-correct-way-to-assign-a-function-containing-chained-commands-to-an-a%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
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 at 11:27