Перейти к содержимому

Повторите терминальную команду

1

Предположим, у меня есть терминальная команда foo(на самом деле это было бы что-то вроде cdили lsили что-то более сложное. Есть ли способ, которым я могу выполнить эту команду много раз БЕЗ использования скрипта.

Например, я хочу сделать эквивалент следующего:

foo && foo && foo && foo && foo 

Есть ли способ, которым я могу сделать что-то вроде

foo * 5 

или, может быть

5:foo 

Я использую машину Linux.

534 просмотра
Nosrettap спросил 12 лет назад
N 371

5 ответов

1

Add some semi-comma into blogger's answer to make it as one-liner:

for variables in ; do ls; done 
Ben Lin ответил 12 лет назад
B 266
1

blogger is right. however, it's important to note the syntax exactly: if you're using an interactive shell (e.g. the terminal)

echo -n; for i in ; do if [[ $? ]]; then foo; fi; done 

the "$?" returns the status of the previous function call; because we're calling "echo" first, the first iteration of the loop will be "true" (0 in bash.) the "fi" closes off the "if" statement, and the semicolons are necessary.

here's the same thing if you were writing a bash script:

#!/bin/bash
echo -n
for i in ; do
if [[ $? ]]; then
foo
fi
done

you'll notice that foo doesn't have a semicolon after it; when writing a full-blown bash script in an environment where line-breaks are available (e.g. your favorite text editor,) line-breaks do the same thing as semicolons :)

also, note that you could instead use

[[ $? ]] && foo 

instead of the if statement; realize that the && means that foo will only be executed if the previous foo didn't have an error. i.e. in the function call

thing1 && thing2 

thing2 will only be executed if thing1 runs fine (technically, if it returns a 0 value) to ignore whether foo is working or not, use semicolons instead of &&

Nick Chandoke ответил 12 лет назад
N 15
1

You can try something like:

for i in ; do YourCommand; done

Example:

for i in ; do ls -l; done

stderr ответил 12 лет назад
S 8 849
0

Have you tried using "for" loop?

for VARIABLE in 1 2 3 4 5 .. N do command1 command2 commandN done 

Когда я пытаюсь сделать это, он просто вызывает новую строку, которая говорит for>

Nosrettap · 12 лет назад · 0
blogger ответил 12 лет назад
B 570
0

I found a great method here.

I know it's still "kind of a script" but i think it's the closest you can get without typing a whole command line with commands.

You can create the command repeat with which you can do this:

 repeat 5 ./foo 

Add the following lines to the end of your ~/.bashrc (if you're using bash):

repeat() { n=$1 shift while [ $(( n -= 1 )) -ge 0 ] do "$@" done } 

and the next time you login (or start bash) you can use repeat 5 ./foo.

Btw. repeat is already a keyword (which does exactly the same) in csh, tcsh and zsh, so with this addition you will add it to bash and it will work exactly the same.

Rik ответил 12 лет назад
R 10 889