Bash Tips

From YobiWiki
Jump to navigation Jump to search

Documentation

Sous MC

Shortcuts

  • ctrl+o: toggler l'affichage console/mc
  • alt+o: même chemin dans l'autre fenêtre
  • alt+enter: copie sur la ligne de commande le nom du fichier pointé
  • * (num key!): inverse la sélection
  • + (num key!): permet de sélectionner selon un filtre, par défaut prend directement tous les fichiers (pas les réps) dans le répertoire courant
  • - (num key!): permet de désélectionner avec un filtre
  • ctrl+u: undo
  • ctrl+l: rafraîchir l'écran
  • Bien plus dans le manuel: F1 -> appuyer sur down pour aller sur "contents"; enter -> les keys

Tips

  • Pour pouvoir faire des sélections à la souris (gpm ou X) sans que mc ne les interprète, tenir la touche shift enfoncée
  • J'aime lancer mes mc root avec un alias qui me met un fond rouge plutot que bleu, l'idée vient de Damn Small Linux mais ici en employant les options mc:
alias smc='sudo -H mc --colors normal=,red:selected=,brightmagenta:marked=,red:markselect=,brightmagenta:directory=,red:executable=,red:link=,red:stalelink=brightcyan,red:device=,red:special=,red:core=,red:input=,green /root'
  • mc permet d'ouvrir directement un ftp, d'ouvrir les targz, les rpm, les deb, les iso, ... en fait quasi tout ce qui peut être représenté comme un système de fichiers, réel ou virtuel.
  • Si il y a un problème avec la touche alt (qui ne fonctionne pas en tant que touche "meta"): F9 -> options -> display bits -> 3eme: uncheck full 8-bits input
  • Pour d'autres problèmes de ce type, jetez un oeil à cette FAQ

Séquences de couleur ISO 6429 (ANSI)

echo -e "Test \033[31mrouge\033[m"
echo -e "Test \033[1;31mrouge vif\033[m"
echo -e "Test \033[32mvert\033[m"
echo -e "Test \033[1;31mvert vif\033[m"
man dir_colors
ESC[rowsA          Cursor up
ESC[rowsB          Cursor down
ESC[colsC          Cursor right
ESC[colsD          Cursor left
ESC[row;colH       Set cursor position (top left is row 1, column 1)
ESC[2J             Clear screen
ESC[K              Clear from cursor to end of line
ESC[row;colf       Set cursor position, same as "H" command
ESC[=modeh         Set display mode; see table of mode values below
ESC[=model         Set display mode; see table of mode values below
ESC[attr;attr;..m  Set display attributes; see table of attribute values below
ESC[key;string;..p Substitute "string" for the specified key; see key substitutions section below.
ESC[s              Save cursor position (may not be nested)
ESC[u              Restore cursor position after a save

Attribute Description

  • 0 All attributes off (normal white on black)
  • 1 High intensity (bold)
  • 2 Normal intensity
  • 4 Underline (usually effective only on monochrome displays)
  • 5 Blinking
  • 7 Reverse Video
  • 8 Invisible
  • 30-37 Set the foreground color: 30=Black 31=Red 32=Green 33=Yellow 34=Blue 35=Magenta 36=Cyan 37=White
  • 40-47 Set the background color: 40=Black 41=Red 42=Green 43=Yellow 44=Blue 45=Magenta 46=Cyan 47=White

Settings are cumulative, so (for example) to set bright red foreground set all attributes off, then set red, then bold: echo _[0;31;1m.

Display Modes

  • 0 Text 40x25 monochrome
  • 1 Text 40x25 color
  • 2 Text 80x25 monochrome
  • 3 Text 80x25 color
  • 4 Graphics 320x200 4-color
  • 5 Graphics 320x200 4-color
  • 6 Graphics 640x200 2-color
  • 7 (cursor wrap kludge)

Mode 7 is an unfortunate kludge; Setting mode 7 with an "h" command tells ANSI to wrap text to the next line when it passes the end of a line; setting mode 7 with an "l" (lower-case L) command tells ANSI not to wrap text. For all other modes the "h" and "l" commands are equivalent.

Key Substitutions

The key substitutions ("p") command causes ANSI to substitute the text in "string" when the specified key is pressed. The key code can be a single character in quotes, a numeric ASCII value, or an extended code for a non ASCII key (e.g. function or cursor keys) in the form 0;n, where n is the scan code for the key. The string to be substituted can be a single character or character string in quotes, a numeric ASCII value, or an extended key code.
For a list of numeric ASCII values, see ASCII Tables. For a list of extended key codes see Key Code and Scan Code Tables.
To clear a key substitution, "substitute" the original key for itself.

TAB completion

  • Avoid the bell: add "set bell-style visible" to ~/.inputrc (versus audible and none)
  • Discover the rules when completing a given command, e.g. acroread: complete|grep acroread


Rules are in /etc/bash_completion and loaded in the environment (see set)

  • See http://aplawrence.com/Unix/customtab.html
    • Enabling custom completions: shopt -s progcomp
    • Registering a custom function to complete command line of e.g. myprog: complete -F _myprog -o dirnames myprog
    • Creating the custom function:
_myprog()
{
   local curw
   COMPREPLY=()
   curw=${COMP_WORDS[COMP_CWORD]}
   COMPREPLY=($(compgen -A user -- $curw))
   return 0
}

Misc

Pay attention when using sudo that $HOME is still configured as yours and you could end up with some user config files owned by root in your own directory.
To avoid that, use "sudo -H" or to make it definitive, use visudo to add to the sudo conf: "Defaults always_set_home"

To convert ASCII Mac file to Unix file:

for file in *; do cat "$file" | tr \\15 \\12 >e;mv e "$file"; done

To convert a filename from uppercase to lowercase

for file in * ; do mv "$file" "$(echo -n $file|tr [[A-Z] [[a-z])" ; done

Boot Disk :

mkbootdisk numero version : voir version dans /lib/modules/2....
ou rdev -h : aide
rdev /boot/kernel - < nom du noyau
dd if=/boot/linux of=/dev/fd0 bs=8192

Conversion Ext2 -> Ext3

tune2fs -j /dev/hdXX

And don't forget to adapt fstab entry


Devinette

What is the output of this command? <verbatim> echo one>/tmp/t;(echo two>/dev/stdout)>>/tmp/t;cat /tmp/t </verbatim>

Now, try...
Astonishing, isn't it?!
PS: to get what you expected, try >>/dev/stdout

Apparently when the command is executed, the sub-process is launched with /dev/stdout pointing to its /proc/self/fd/1 but this one is now set to /tmp/t so the sub-shell re-opens the file and overwrites it!

Note that under cygwin/bash, there is no real /dev/stdout and the result is one two

echo one>/tmp/t;m=$(echo two>/dev/stdout);echo $m>>/tmp/t;cat /tmp/t is working as expected, /proc/self/fd/1 of the sub-process is now a pipe to the main process.

So a program is able to detect if its output was redirected or not!