Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've a not really permissive program to work with. Unfortunately, this program doesn't just allow the following

"command -a -b -c"

or

"command -abc"

So I've to always type the following

command -a && command -b && command -c

I am pretty sure there is more efficient way to type that, but can't figure it out.

share|improve this question
5  
This will totally depend on the command. Please include it in your question. – joepd Jan 12 at 10:42
1  
You have many different answers, but I bet you will stick to cmd -a; cmd -b; cmd -c in long run, as all human beings do. – jimmij Jan 12 at 11:39
    
This question is too broad without details on command. In general case, command -a -b -c, command -abc and command -a && command -b && command -c have different meaning.. – Dmitry Grigoryev Jan 13 at 7:50
up vote 10 down vote accepted

You could do:

echo -a -b -c | xargs -n 1 command

Or:

xargs -n1 command <<< '-a -b -c'

with some shells.

But beware that it affects the stdin of command.

With zsh:

autoload zargs # best in ~/.zshrc
zargs -n 1 -- -a -b -c -- command

Or simply:

for o (-a -b -c) command $o

None of those would abort if any of the command invocations failed (except if it fails with the 255 exit status).

For that, you'd want:

for o (-a -b -c) command $o || break

That still fails to the $? to the exit status of the failed command invocation). You could change that to:

(){for o (-a -b -c) command $o || return}

But by that time, it's already longer than:

command -a && command -b && command -c
share|improve this answer
    
Thank you Stéphane for this option. The combinaison of !!:0 and xargs did the trick. – Douda Jan 12 at 11:17

You can refer to the first word of the previous command line (command in your case) by history expansion !!:0 and then you can just add necessary arguments to it.

command -a && !!:0 -b && !!:0 -c

For example:

% echo foobar && !!:0 spamegg && !!:0 baz    
echo foobar && echo spamegg && echo baz
foobar
spamegg
baz

Note that, as Stéphane Chazelas has pointed out, this can result in unexpected expansions too.

share|improve this answer
1  
Exactly what I needed ! Thank you vey much heemayl – Douda Jan 12 at 10:49
2  
!!:0 (in shells that support csh history expansion) will expand to the first word of the previous command-line, not previous command. – Stéphane Chazelas Jan 12 at 10:59
    
@StéphaneChazelas clarified.. – heemayl Jan 12 at 11:00
    
We don't know what the first word of the previous command-line was. Try to enter uname and then echo foobar && !!:0 spamegg && !!:0 baz, that will expand to echo foobar && uname spamegg && uname baz. – Stéphane Chazelas Jan 12 at 11:05
2  
One ! is enough: !:0 but with @StéphaneChazelas points better is use !#:0 where the !# mean The entire command line typed so far. – Costas Jan 12 at 12:17

How about a shell function wrapping a loop?

yourcmd() {
    local arg
    for arg; do
        thing "$arg" || return
    done
}

Where "thing" is the actual command you want to invoke. Then simply

yourcmd -a -b -c

You can even generalize this to any command:

splitargs() {
    local cmd arg 
    cmd="$1"
    shift
    for arg; do
        "$cmd" "$arg" || return
    done
}

Then

splitargs somecommand -a -b -c
share|improve this answer

Amidst the creative and insightful answers, here's something simple in the name of typing fewer characters:

# under the assumption that you have a frequently-used set of flags: -a -b -c
alias c='command -a && command -b && command -c'

# slightly more generally, name some aliases according to the set of flags
alias cabc='command -a && command -b && command -c'
alias cdef='command -d && command -e && command -f'

# more generally, just shorten the command name and then pass whatever flags you want
alias c=command
c -e && c -f && c -f
share|improve this answer

Make a shell script MultiRun, which takes first Parameter as Command and runs this with remaining Parameters one at a time.

./MultiRun curl A B C D

Your Script should take curl as the command and run "curl A" , "curl B" , "curl C" , "curl D". Now, because your script is in control, you can decide whether a failure in any one command will terminate the script or continue to next Parameter or wait for user input or whatever. You can even decide on the exit status of the whole script, based on the execution of the individual commands.

MultiRun Script can be something like this:

#! /bin/bash
COMMAND=$1
shift #### Remove COMMAND from existing Parameters
for PARAMETER in "$@" ; #### Process all remaining Parameters
do
  # echo Executing "$COMMAND $PARAMETER" now #### Uncomment this line to see what command is getting executed.
  $COMMAND $PARAMETER
  # Check exit status and take action or wait for user input , if required.
  # read -p "finished executing command. press enter to continue. waiting..." #### Uncomment this line to pause between commands.
done
# Depending on the individual exit statuses , choose your exit status N with "exit $N".

Now execute this:
./MultiRun echo 1 2 3 4
to get output of "echo 1" , "echo 2" , "echo 3" , "echo 4" like this :
1
2
3
4

It is a very flexible & reusable solution.

share|improve this answer

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.