Compte rendu de la soirée chaussette !




Suite à la dernière soirée firstjeudi (la soirée chaussette) voici le compte rendu des tips bash qui ont été présentés.

Tout à d'abord, le premier tips (qui mérite une chaussette) mais qui est toujours très utile :

[bsimard@localhost ~]$ man bash

La commande "type" permet de savoir qu'une commande est pure bash

[bsimard@localhost ~]$ type cd
cd est une primitive du shell

L'historique et les arguments des commandes

Comment rejouer la dernière commande exécutée ?

Utilisez le "!!"

[bsimard@localhost ~]$ echo toto
toto
[bsimard@localhost ~]$ !!
echo toto
toto

Comment rejouer la n-ième commande de l'historique ?

Utilisez le "!-n" où n est la position dans l'historique.

[bsimard@localhost ~]$ echo toto
toto
[bsimard@localhost ~]$ echo titi
titi
[bsimard@localhost ~]$ !-2
echo toto
toto

Comment récupérer le n-ième argument d'une commande ?

Utilisez le ":n" où n est le numéro de l'argument.

[bsimard@localhost ~]$ echo toto titi tata
toto titi tata
[bsimard@localhost ~]$ echo !!:3
echo tata
tata

Comment récupérer tous les arguments entre le ième et le jième ?

 Utilisez le ":i-j"

[bsimard@localhost ~]$ echo toto titi tata
toto titi tata
[bsimard@localhost ~]$ echo !!:1-3
echo toto titi tata
toto titi tata

Comment récupérer le dernier argument de la dernière commande ?

Utilisez "!$"

[bsimard@localhost ~]$ echo titi toto tata
titi toto tata
[bsimard@localhost ~]$ echo !$
echo tata
tata

Le remplacement de chaine de caractère

Comment remplacer la première occurrence d'un mot dans la dernière commande ?

Utilisez "^old^new^"

[bsimard@localhost ~]$ echo toto titi tata
toto titi tata
[bsimard@localhost ~]$ ^toto^tete^
echo tete titi tata
tete titi tata

Comment remplacer la première occurrence d'un mot dans la dernière commande ?

Utilisez la command substitute ":s/old/new/"

[bsimard@localhost ~]$ echo toto toto toto
toto toto toto
[bsimard@localhost ~]$ !!:s/toto/titi/
echo titi toto toto
titi toto toto

Comment remplacer toutes  les occurences ?

Utilisez ":gs/old/new/"

[bsimard@localhost ~]$ echo toto toto toto
toto toto toto
[bsimard@localhost ~]$ !!:gs/toto/titi/
echo titi titi titi
titi titi titi

Comment récupéré le path d'un fichier ?

Utilisez ":h"

[bsimard@localhost ~]$ ls /usr/share/doc/xterm/copyright
[bsimard@localhost ~]$ cd !$:h
[/usr/share/doc/xterm]

L'historique et la recherche de commande

Vous en avez marre de taper toujours la même commande, alors que celle-ci se trouve dans l'historique ?

Alors le CTRL+R est pour vous ! Lorsque vous tapez cela dans votre terminal, vous verez apparaitre un prompt. Si vous tapez le debut de la commande que vous recherchez, bash va chercher dans l'historique la dernière commande qui y ressemble.

De plus si cela n'est pas la bonne, retaper CTRL+R, et bash va aller chercher la précédente. Bref vous naviguer dans votre historique avec un filtre.

[bsimard@localhost ~]$ echo toto
toto
[bsimard@localhost ~]$ echo titi
titi
[bsimard@localhost ~]$ echo tata
tata
(reverse-i-search)`e': echo tata

Une autre solution, consite à modifier le fichier ~/.inputrc, pour y ajouter les lignes suivantes :

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

Ceci permet de filtrer l'historique obtenue avec les touches "up-arrow" et "down-arrow", avec le début de la commande que l'on tape. Ainsi plus besoin du CTRL+R. Si on tape rien , c'est l'historique habituel. Par contre si on tape "ec", seules les lignes de l'historique commençant par "ech" seront accéssibles.

Quelques tips en vrac, mais bien utile !

Comment insérer un marqueur dans une ligne de commande ?

Utilisez "CRTL+ESPACE".

Comment alterner la position du curseur, avec celle du marqueur ?

Utilisez "CTRL+X CTRL+X"

Comment basculer la ligne courante dans l'éditeur défini par la variable d'environnement EDITOR ?

Utilisez "CTRL+X CTRL+E"

Comment remplacer toutes les variables d'une commande par leur vrai valeur ?

Utilisez "CTRL+META+E"

Comment faire un menu de selection dans vos scripts ?

select choix in choix1 choix2 choix3; do ...; done

Comment faire planter une machine ?

Utilisez ":(){ :|:& };:"


auncun commentaires


Poster un commentaire





Captcha