How to run a command multiple times with different parameters?
Asked Answered
N

5

5

Is it possible to run a command multiple times with different parameters?

Something like:

sudo apt-get install asd && install qwe && remove ert && autoremove && autoclean
Necker answered 22/4, 2015 at 7:42 Comment(0)
P
7

If you are working from the command line, you can probably use the following: once you ran command parameter1, repeat command with parameter2 instead typing:

^parameter1^parameter2

Example

I have two files: a1 and a2. Let's ls -l the first:

$ ls -l a1
-rw-r--r-- 1 me me 21 Apr 21 16:43 a1

Now let's do the same for a2:

$ ^a1^a2
ls -l a2                # bash indicates what is the command being executed
-rw-r--r-- 1 me me 13 Apr 21 16:43 a2

You can find more tricks like this in What is your single most favorite command-line trick using Bash?.

Prism answered 22/4, 2015 at 8:30 Comment(0)
I
12

Use either a for loop:

for cmd in "install asd" \
        "install qwe" "remove ert" \
        "autoremove" "autoclean"
do
    sudo apt-get $cmd
done

or xargs:

printf '%s\n'  "install asd" \
    "install qwe"
    "remove ert"
    "autoremove"
    "autoclean" |
xargs -I "#" sudo apt-get "#"
Inglorious answered 22/4, 2015 at 8:24 Comment(0)
P
7

If you are working from the command line, you can probably use the following: once you ran command parameter1, repeat command with parameter2 instead typing:

^parameter1^parameter2

Example

I have two files: a1 and a2. Let's ls -l the first:

$ ls -l a1
-rw-r--r-- 1 me me 21 Apr 21 16:43 a1

Now let's do the same for a2:

$ ^a1^a2
ls -l a2                # bash indicates what is the command being executed
-rw-r--r-- 1 me me 13 Apr 21 16:43 a2

You can find more tricks like this in What is your single most favorite command-line trick using Bash?.

Prism answered 22/4, 2015 at 8:30 Comment(0)
D
3

This loops a set of parameters and applies them to the same command. There is no error checking, unlike in your example which will fail if one of the earlier commands fails

for param in asd qwe ert; do
    install $param
done
Duque answered 22/4, 2015 at 7:52 Comment(0)
F
0

No. Unfortunately, the shell cannot read your mind.

You could do something like this:

alias sag="sudo apt-get"
sag install asd qwe && sag remove ert && sag autoremove && sag autoclean

Although I'm not convinced that you really want && there; you might be just as happy with ;

Forked answered 22/4, 2015 at 7:52 Comment(1)
never thought about setting alias but great idea :) thank you !Necker
B
0

In the most general case, if you have a set or parameters you want to loop over, perhaps try

for first in one "two, with cinnamon" three; do
  for second in red yellow "odd color between brown and gray"; do
    for third in 0.1 0.5 1.0 2.0; do
        frobnicate --number "$first" --color "$second" --limit "$third"
    done
  done
done

or perhaps

while read -r first second third; do
  frobnicate --number "$first" --color "$second" --limit "$third"
done <<____EOF
  sixty-five   mauve    0.1
  sixty-five   crimson  0.1
  fifty-eleven lilac    0.2
  fifty-eleven lilac    0.5
  17           black    1.0
  42           black    1.0
____EOF

Just to spell this out, this will read three arguments from each line and run frobnicate with those as parameters. If this is for some sort of experiment, you would perhaps also like to redirect the results to a file named after the parameters, or perhaps add a fourth field to indicate the name of the file to write to?

Boleslaw answered 7/11, 2022 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.