Sunday, May 5, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 18347  / 2 Years ago, tue, july 5, 2022, 12:02:04

I made one small alias and got a question as , how to execute a single line group of commands as command by command with out breaking it ?



echo " alias get='sudo apt-get install'" >> .bashrc & bash


here we have commands as echo & bash. even placing them all in a single line i want to execute them command by command.



Update: for example if there is a group command line as



command1 ; command 2; command 3; command 4; commmand5



Then I want to execute this group command not in a single step. 1st 3 commands at a time and then next 2 commands . But condition is I have to write all those commands in a single line.


More From » command-line

 Answers
0

You should be able to do what you have in mind by using command grouping, which allows you to execute commands together, in this case, whether they succeed or not, i.e. in the form { cmd; cmd; }. More information on command grouping and the use of && and || logical operators is available at the Bash wiki and in the Bash reference manual.



I thought initially the form your command-line could take would be:



{ cmd1; cmd2; cmd3; }; sleep 1; { cmd4; cmd5;}


The sleep command just gives a pause and can be removed.



However, you say you want an extra return, or confirmation for the last two commands, and so to achieve that you need to interrupt the command flow in the example above- sleep merely pauses, but regardless the last two commands always execute.



To really halt the command chain, you could use read -p..... which requires user intervention to continue. Here is what you could do:



1) Execute the first three commands, then give the user the opportunity or not to execute the last ones. You could see if the oneliner I have written below is suitable:



Note: Please substitute the id command for your own commands.



id; id; id; read -p "Execute final two commands? (Y/N) " ans; if [[ $ans =~ Y|y ]]; then id; id; else echo "Finished"; fi


Now the first three commands execute and then you need to press y and return for the last two to execute, but all the commands are on the same line. This is about as much as I can contrive really, as I am not quite sure what you want.


[#30122] Tuesday, July 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skipu

Total Points: 366
Total Questions: 114
Total Answers: 112

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;