Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
You will execute them "command by command" unless you run them in parallel by sending one to the background with &. Otherwise, the shell will execute the commands sequentially. –  terdon Aug 1 '13 at 14:48
    
It's really unclear what you're trying to ask here, @Jai - could you try and explain more thoroughly exactly what you would like to do, please? –  Jez W Aug 1 '13 at 15:47
    
Dinner time- Delay ,sorry. I am trying to execute only a part of the total command and not the total group of commands at a time. my choosing part may include a single command or more. I need to select specific part of the total command for execution. –  AgentCool Aug 1 '13 at 16:26
    
Do you mean by using command grouping in Bash?, like cmd || { cmd; cmd; } –  user76204 Aug 6 '13 at 16:12
    
@Mik Thanks for responding. I will try to explain more about my Question. –  AgentCool Aug 6 '13 at 16:17
show 3 more comments

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer
    
you gave something I really needed. Thank you. I will modify it as I want. Thank you. –  AgentCool Aug 7 '13 at 12:09
add comment

If you want to run them in sequence, the best way would be to use the double ampersand &&. This is often used, for example, when running sudo apt-get update && sudo apt-get upgrade.

The && requires that the first command runs successfully before starting the second. So in the example I gave, it updates the package list, and then upgrades packages but only if the updating was successful.

The single ampersand is useful if you want to orphan a program from its terminal, so that the program stays open even if the active terminal is closed, by using e.g. nautilus & disown.

share|improve this answer
    
I mean as Not the total line . only a part of the line. only a few commands. not all the commands existed in that line. –  AgentCool Aug 1 '13 at 13:58
    
@Jai Not sure I understand. Do you want it to run one, then wait for you to confirm before it runs the second? –  Jez W Aug 1 '13 at 14:04
add comment

& runs the left hand side command in the background. If you want to run the second command only if the previous one ran successfully, use

 command1 && command2

If you want to run the second command even if the previous one is not successful, use

command1 ; command2
share|improve this answer
    
I mean as Not the total line . only a part of the line. only a few commands. not all the commands existed in that line. –  AgentCool Aug 1 '13 at 13:57
    
@Jai: Please give more details. I do not understand. –  choroba Aug 1 '13 at 14:26
add comment

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

Just put 2 ampersands and it works.

share|improve this answer
    
I mean as Not the total line . only a part of the line. only a few commands. not all the commands existed in that line. –  AgentCool Aug 1 '13 at 13:57
1  
are you just trying to execute the echo command in the bash shell because you don't currently use bash? –  Phil Lawlor Aug 1 '13 at 14:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.