Descobrir o comando que eu executei pela última vez que inicia de uma determinada maneira?

8

No ambiente de linha de comando bash, você pode fazer !! para executar novamente seu último comando. E você pode pressionar a seta para cima para ver o último comando que você executou. Mas e se você quiser apenas ver mas não executar o último comando que você executou que começou de uma determinada maneira?

    
por bernie2436 11.03.2014 / 18:49

7 respostas

18

Se você pesquisar o histórico usando Ctrl + r e digitando algumas letras do comando e não pressionando Enter mas pressionando , o comando aparecerá e não será executado. Outra alternativa é:

history | grep 'mycommand'

Essa boa ficha de histórico pode ajudar.

    
por 11.03.2014 / 19:15
9

Você pode mostrar o último comando executado, adicionando :p a !! :

$ echo 123
123
$ !!:p
echo 123

Se você quiser mostrar o último comando executado com determinado nome, use uma exclamação:

$ echo 123
123
$ pwd
/home/cuonglm
$ !echo:p
echo 123
    
por 11.03.2014 / 19:23
4

Toda resposta aqui, até onde eu sei, não é portátil. Para uma opção portátil, considere o fc garantido por POSIX:

    $ man fc

−e editor Use the editor named by editor to edit the commands. The editor string is a utility name, subject to search via the PATH variable ... The value in the FCEDIT variable shall be used as a default when −e is not specified. If FCEDIT is null or unset, ed shall be used as the editor.

−l (The letter ell.) List the commands rather than invoking an editor on them. The commands shall be written in the sequence indicated by the first and last operands, as affected by −r, with each command preceded by the command number.

−n Suppress command numbers when listing with −l.

−r Reverse the order of the commands listed (with −l) or edited (with neither −l nor −s).

−s Re-execute the command without invoking an editor.

OPERANDS

first, last Select the commands to list or edit. The number of previous commands that can be accessed shall be determined by the value of the HISTSIZE variable. The value of first or last or both shall be one of the following:

[+or-]number A positive (or negative) number representing a command number; command numbers can be displayed with the −l option... For example, −1 is the immediately previous command...

When the −l option is used to list commands, the format of each command in the list shall be as follows:

    "%d\t%s\n", <line number>, <command>

If both the −l and −n options are specified, the format of each command shall be:

    "\t%s\n", <command>

If the consists of more than one line, the lines after the first shall be displayed as:

    "\t%s\n", <continued-command>

Mas não se esqueça de usar -l ou -e se você SOMENTE deseja ver / editar seus comandos. Por padrão fc abrirá a lista de comandos solicitada em FCEDIT (note que é diferente da variável de ambiente EDITOR ) e, quando FCEDIT é fechado, fc executará os comandos editados.

De qualquer forma, especificamente, a resposta a esta pergunta poderia ser:

    % fc -l -1

Ou sem números de linha:

    % fc -ln -1

Ou os últimos cinco comandos na ordem inversa:

    % fc -lrn -1 -5

No seu pager:

    % fc -lrn -1 -5 |$PAGER

Para sua última chamada para fc :

    % fc -l fc
    
por 12.03.2014 / 06:34
2

pressione Ctrl r e comece a digitar o comando que deseja ver. O bash será incrementalmente completo. Quando estiver satisfeito, pressione Enter para executar novamente ou Ctrl g para abortar.

    
por 11.03.2014 / 19:24
2

Se você quiser apenas o último comando, use:

 history | cut -c 8- | tail -n 2 | head -n 1 

Se você quiser fazer isso repetidamente, coloque um espaço antes de history ou use:

 history | cut -c 8- | grep -Ev '^history' | tail -n 1

Se você quiser apenas ver o último comando começando com o padrão xyz :

 history | cut -c 8- | grep -E '^xyz'  | tail -n 1 

Mas isso não funciona se o padrão for os caracteres iniciais de 'history' e você precisaria de algo como:

history | cut -c 8- | grep -Ev 'history \|' | grep -E '^hist'  | tail -n 1 
    
por 11.03.2014 / 19:45
1
bind '"\ep": history-search-backward'
bind '"\en": history-search-forward'

E então você pode inserir algum texto, e depois Alt-p para procurar os últimos comandos que iniciam o mesmo (e Alt-p , Alt -n para alternar entre eles.

(Esses são os atalhos de chaves padrão para widgets semelhantes em zsh )

    
por 11.03.2014 / 21:23
0

Adicione essas linhas em um arquivo ~/.inputrc :

"\e[A": history-search-backward
"\e[B": history-search-forward

Então source ~/.inputrc no seu shell (ou apenas reinicie o shell).

Agora você pode digitar algumas letras do comando e usar as setas para cima / para baixo para percorrer os comandos que começam com essas poucas letras.

    
por 12.03.2014 / 00:39