Aujourd'hui, j'ai été pris en flagrant délit en tentant de coder au golf un générateur de mot de passe pour Twitter.
import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))
90 caractères. Comme il s'agit de beaucoup d'espace libre, j'ai décidé de relever la barre et de le rendre exécutable aussi.
echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
139 caractères. Sympa, sauf visiblement bash étouffe sur le point d'exclamation.
badp@delta:~$ echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
bash: !/usr/bin/python\nimport: event not found
Point d'exclamation pesky. "Fuyons-y," pensai-je! J'ai un personnage de rechange après tout.
echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
Évidemment...
badp@delta:~$ echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
./pg: line 2: syntax error near unexpected token `('
./pg: line 2: `import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))'
badp@delta:~$ cat pg
#\!/usr/bin/python
import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))
Laissant mon code asinine jouer au golf - je ne peux pas l'expliquer.
Avec \!
, le point d'exclamation a été échappé, sauf qu'il ne l'était vraiment pas, car il a \!
été laissé tel quel echo
pour reprendre.
Une solution aurait pu être utilisée à la \x21
place, mais je ne suis pas convaincu que ce soit la bonne façon d'échapper à un point d'exclamation dans une commande bash.
tl; dr: Comment échapper correctement un point d'exclamation dans une commande bash?
!:0
!$
et !^
économise tellement de temps et de frappe.
!event
syntaxe en premier lieu? Cela ne m'a toujours causé que des ennuis.