<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.yobi.be/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=~newacct</id>
	<title>YobiWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.yobi.be/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=~newacct"/>
	<link rel="alternate" type="text/html" href="https://wiki.yobi.be/index.php?title=Special:Contributions/~newacct"/>
	<updated>2026-05-24T15:46:44Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://wiki.yobi.be/index.php?title=Bash_Tips&amp;diff=6374</id>
		<title>Bash Tips</title>
		<link rel="alternate" type="text/html" href="https://wiki.yobi.be/index.php?title=Bash_Tips&amp;diff=6374"/>
		<updated>2010-08-11T05:30:45Z</updated>

		<summary type="html">&lt;p&gt;~newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Documentation==&lt;br /&gt;
* [http://ldp.unixtech.be/LDP/abs/html/index.html Advanced Bash-Scripting Guide]&lt;br /&gt;
* http://www.ss64.com/bash/index.html&lt;br /&gt;
* http://tille.xalasys.com/training/bash&lt;br /&gt;
&lt;br /&gt;
==Sous MC==&lt;br /&gt;
===Shortcuts===&lt;br /&gt;
* ctrl+o: toggler l&#039;affichage console/mc&lt;br /&gt;
* alt+o:  même chemin dans l&#039;autre fenêtre&lt;br /&gt;
* alt+enter: copie sur la ligne de commande le nom du fichier pointé&lt;br /&gt;
* * (num key!): inverse la sélection&lt;br /&gt;
* + (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&lt;br /&gt;
* - (num key!): permet de désélectionner avec un filtre&lt;br /&gt;
* ctrl+u: undo&lt;br /&gt;
* ctrl+l: rafraîchir l&#039;écran&lt;br /&gt;
* Bien plus dans le manuel: F1 -&amp;gt; appuyer sur down pour aller sur &amp;quot;contents&amp;quot;; enter -&amp;gt; les keys&lt;br /&gt;
===Tips===&lt;br /&gt;
* Pour pouvoir faire des sélections à la souris (gpm ou X) sans que mc ne les interprète, tenir la touche shift enfoncée&lt;br /&gt;
* J&#039;aime lancer mes mc root avec un alias qui me met un fond rouge plutot que bleu, l&#039;idée vient de Damn Small Linux mais ici en employant les options mc:&lt;br /&gt;
 alias smc=&#039;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&#039;&lt;br /&gt;
* mc permet de se connecter via ftp ou ssh, d&#039;ouvrir les tar.gz, tar.bz2, 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.&lt;br /&gt;
* Si il y a un problème avec la touche alt (qui ne fonctionne pas en tant que touche &amp;quot;meta&amp;quot;): F9 -&amp;gt; options -&amp;gt; display bits -&amp;gt; 3eme: uncheck full 8-bits input&amp;lt;br&amp;gt;Cela mettera use_8th_bit_as_meta=1 dans ~/.mc/ini&lt;br /&gt;
* Pour d&#039;autres problèmes de ce type, jetez un oeil à cette [http://www.ibiblio.org/mc/FAQ FAQ]&lt;br /&gt;
&lt;br /&gt;
==Séquences de couleur ISO 6429 (ANSI)==&lt;br /&gt;
 echo -e &amp;quot;Test \033[31mrouge\033[m&amp;quot;&lt;br /&gt;
 echo -e &amp;quot;Test \033[1;31mrouge vif\033[m&amp;quot;&lt;br /&gt;
 echo -e &amp;quot;Test \033[32mvert\033[m&amp;quot;&lt;br /&gt;
 echo -e &amp;quot;Test \033[1;31mvert vif\033[m&amp;quot;&lt;br /&gt;
 man dir_colors&lt;br /&gt;
&lt;br /&gt;
 ESC[rowsA          Cursor up&lt;br /&gt;
 ESC[rowsB          Cursor down&lt;br /&gt;
 ESC[colsC          Cursor right&lt;br /&gt;
 ESC[colsD          Cursor left&lt;br /&gt;
 ESC[row;colH       Set cursor position (top left is row 1, column 1)&lt;br /&gt;
 ESC[2J             Clear screen&lt;br /&gt;
 ESC[K              Clear from cursor to end of line&lt;br /&gt;
 ESC[row;colf       Set cursor position, same as &amp;quot;H&amp;quot; command&lt;br /&gt;
 ESC[=modeh         Set display mode; see table of mode values below&lt;br /&gt;
 ESC[=model         Set display mode; see table of mode values below&lt;br /&gt;
 ESC[attr;attr;..m  Set display attributes; see table of attribute values below&lt;br /&gt;
 ESC[key;string;..p Substitute &amp;quot;string&amp;quot; for the specified key; see key substitutions section below.&lt;br /&gt;
 ESC[s              Save cursor position (may not be nested)&lt;br /&gt;
 ESC[u              Restore cursor position after a save&lt;br /&gt;
===Attribute Description===&lt;br /&gt;
*0 All attributes off (normal white on black)&lt;br /&gt;
*1 High intensity (bold)&lt;br /&gt;
*2 Normal intensity&lt;br /&gt;
*4 Underline (usually effective only on monochrome displays)&lt;br /&gt;
*5 Blinking&lt;br /&gt;
*7 Reverse Video&lt;br /&gt;
*8 Invisible&lt;br /&gt;
*30-37 Set the foreground color: 30=Black 31=Red 32=Green 33=Yellow 34=Blue 35=Magenta 36=Cyan 37=White&lt;br /&gt;
*40-47 Set the background color: 40=Black 41=Red 42=Green 43=Yellow 44=Blue 45=Magenta 46=Cyan 47=White&lt;br /&gt;
Settings are cumulative, so (for example) to set bright red foreground set all attributes off, then set red, then bold: echo _[0;31;1m.&lt;br /&gt;
===Display Modes===&lt;br /&gt;
*0 Text 40x25 monochrome&lt;br /&gt;
*1 Text 40x25 color&lt;br /&gt;
*2 Text 80x25 monochrome&lt;br /&gt;
*3 Text 80x25 color&lt;br /&gt;
*4 Graphics 320x200 4-color&lt;br /&gt;
*5 Graphics 320x200 4-color&lt;br /&gt;
*6 Graphics 640x200 2-color&lt;br /&gt;
*7 (cursor wrap kludge)&lt;br /&gt;
Mode 7 is an unfortunate kludge; Setting mode 7 with an &amp;quot;h&amp;quot; command tells ANSI to wrap text to the next line when it passes the end of a line; setting mode 7 with an &amp;quot;l&amp;quot; (lower-case L) command tells ANSI not to wrap text. For all other modes the &amp;quot;h&amp;quot; and &amp;quot;l&amp;quot; commands are equivalent.&lt;br /&gt;
===Key Substitutions===&lt;br /&gt;
The key substitutions (&amp;quot;p&amp;quot;) command causes ANSI to substitute the text in &amp;quot;string&amp;quot; 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.&lt;br /&gt;
&amp;lt;br&amp;gt;For a list of numeric ASCII values, see ASCII Tables. For a list of extended key codes see Key Code and Scan Code Tables.&lt;br /&gt;
&amp;lt;br&amp;gt;To clear a key substitution, &amp;quot;substitute&amp;quot; the original key for itself.&lt;br /&gt;
&lt;br /&gt;
==TAB completion==&lt;br /&gt;
* Avoid the bell: add &amp;quot;set bell-style visible&amp;quot; to ~/.inputrc (versus audible and none)&lt;br /&gt;
* Discover the rules when completing a given command, e.g. acroread: complete|grep acroread&lt;br /&gt;
&amp;lt;br&amp;gt;Rules are in /etc/bash_completion and loaded in the environment (see set)&lt;br /&gt;
* See http://aplawrence.com/Unix/customtab.html&lt;br /&gt;
** Enabling custom completions: shopt -s progcomp&lt;br /&gt;
** Registering a custom function to complete command line of e.g. myprog: complete -F _myprog -o dirnames myprog&lt;br /&gt;
** Creating the custom function:&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
 _myprog()&lt;br /&gt;
 {&lt;br /&gt;
    local curw&lt;br /&gt;
    COMPREPLY=()&lt;br /&gt;
    curw=${COMP_WORDS[COMP_CWORD]}&lt;br /&gt;
    COMPREPLY=($(compgen -A user -- $curw))&lt;br /&gt;
    return 0&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
==Misc==&lt;br /&gt;
&lt;br /&gt;
Pay attention when using &#039;&#039;&#039;sudo&#039;&#039;&#039; that $HOME is still configured as yours and you could end up with some user config files owned by root in your own directory.&lt;br /&gt;
&amp;lt;br&amp;gt;To avoid that, use &amp;quot;sudo -H&amp;quot; or to make it definitive, use visudo to add to the sudo conf: &amp;quot;Defaults always_set_home&amp;quot;&lt;br /&gt;
&lt;br /&gt;
To convert ASCII  Mac file to Unix file:&lt;br /&gt;
 for file in *; do cat &amp;quot;$file&amp;quot; | tr \\15 \\12 &amp;gt;e;mv e &amp;quot;$file&amp;quot;; done&lt;br /&gt;
&lt;br /&gt;
To convert a filename from uppercase to lowercase&lt;br /&gt;
 for file in * ; do mv &amp;quot;$file&amp;quot; &amp;quot;$(echo -n $file|tr [[A-Z] [[a-z])&amp;quot; ; done&lt;br /&gt;
&lt;br /&gt;
Boot Disk :&lt;br /&gt;
 mkbootdisk numero version : voir version dans /lib/modules/2....&lt;br /&gt;
 ou rdev -h : aide&lt;br /&gt;
 rdev /boot/kernel - &amp;lt; nom du noyau&lt;br /&gt;
 dd if=/boot/linux of=/dev/fd0 bs=8192&lt;br /&gt;
&lt;br /&gt;
Conversion Ext2 -&amp;gt; Ext3&lt;br /&gt;
&lt;br /&gt;
 tune2fs -j /dev/hdXX&lt;br /&gt;
And don&#039;t forget to adapt fstab entry&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Devinette===&lt;br /&gt;
&lt;br /&gt;
What is the output of this command?&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
echo one&amp;gt;/tmp/t;(echo two&amp;gt;/dev/stdout)&amp;gt;&amp;gt;/tmp/t;cat /tmp/t&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, try...&lt;br /&gt;
&amp;lt;br&amp;gt;Astonishing, isn&#039;t it?!&lt;br /&gt;
&amp;lt;br&amp;gt;PS: to get what you expected, try &amp;gt;&amp;gt;/dev/stdout&lt;br /&gt;
&lt;br /&gt;
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!&lt;br /&gt;
&lt;br /&gt;
Note that under cygwin/bash, there is no real /dev/stdout and the result is one two&lt;br /&gt;
&lt;br /&gt;
echo one&amp;gt;/tmp/t;m=$(echo two&amp;gt;/dev/stdout);echo $m&amp;gt;&amp;gt;/tmp/t;cat /tmp/t&lt;br /&gt;
is working as expected, /proc/self/fd/1 of the sub-process is now a pipe to the main process.&lt;br /&gt;
&lt;br /&gt;
So a program is able to detect if its output was redirected or not!&lt;br /&gt;
===Little scripts===&lt;br /&gt;
To constitute a dictionary for Scrabble from a french dict:&lt;br /&gt;
 grep -v [-\&#039;.] /usr/share/dict/french|unaccent ISO-8859-1|sort -u&amp;gt;scrabble-french&lt;br /&gt;
Usual prefixes:&lt;br /&gt;
 cat scrabble-french |cut -c-3|uniq -c|sort -n -r|gawk &#039;$1&amp;gt;1000{print $2}&#039; &lt;br /&gt;
 cat scrabble-french |cut -c-2|uniq -c|sort -n -r|gawk &#039;$1&amp;gt;2000{print $2}&#039;&lt;br /&gt;
To find usual suffixes, invert the dictionary:&lt;br /&gt;
 cat scrabble-french|tr &amp;quot;\n&amp;quot; &amp;quot;.&amp;quot;|tac -r -s.|tr &amp;quot;.&amp;quot; &amp;quot;\n&amp;quot;|sort&amp;gt;scrabble-suffix&lt;br /&gt;
Then find the most common suffixes:&lt;br /&gt;
  cat scrabble-suffix |cut -c-4|uniq -c|sort -n|gawk &#039;$1&amp;gt;1000{print $2}&#039;|tr &amp;quot;\n&amp;quot; &amp;quot;.&amp;quot;|tac -r -s.|tr &amp;quot;.&amp;quot; &amp;quot;\n&amp;quot;&lt;br /&gt;
  cat scrabble-suffix |cut -c-3|uniq -c|sort -n|gawk &#039;$1&amp;gt;2000{print $2}&#039;|tr &amp;quot;\n&amp;quot; &amp;quot;.&amp;quot;|tac -r -s.|tr &amp;quot;.&amp;quot; &amp;quot;\n&amp;quot;&lt;br /&gt;
  cat scrabble-suffix |cut -c-2|uniq -c|sort -n|gawk &#039;$1&amp;gt;2000{print $2}&#039;|tr &amp;quot;\n&amp;quot; &amp;quot;.&amp;quot;|tac -r -s.|tr &amp;quot;.&amp;quot; &amp;quot;\n&amp;quot;&lt;br /&gt;
  cat scrabble-suffix |cut -c-1|uniq -c|sort -n|gawk &#039;$1&amp;gt;5000{print $2}&#039;|tr &amp;quot;\n&amp;quot; &amp;quot;.&amp;quot;|tac -r -s.|tr &amp;quot;.&amp;quot; &amp;quot;\n&amp;quot;&lt;br /&gt;
===History===&lt;br /&gt;
To enable timestamps in the history, set the env variable (see &#039;man strftime&#039;)&lt;br /&gt;
 HISTTIMEFORMAT=&#039;%F %T &#039;&lt;br /&gt;
To convert timestamps from the history file&lt;br /&gt;
 perl -e &#039;print scalar localtime(1141863326), &amp;quot;\n&amp;quot;&#039;&lt;br /&gt;
To keep the history &amp;quot;forever&amp;quot;, here is a nice [http://www.onerussian.com/Linux/.files/.bashrc_history .bashrc_history] script to install&lt;br /&gt;
&amp;lt;br&amp;gt;I adapted some stuff:&lt;br /&gt;
* Enable timestamps on individual commands&lt;br /&gt;
* Dates expressed as YY-MM-DD hh:mm:ss&lt;br /&gt;
* Converted copy of the individual timestamps (requires gawk so enable it only if you have gawk, this is purely cosmetic as timestamps can be converted later if needed)&lt;br /&gt;
* Trap HUP also as invoked bash from mc is apparently interrupted this way&lt;br /&gt;
&amp;lt;source lang=diff&amp;gt;&lt;br /&gt;
 if [ &amp;quot;$PS1&amp;quot; ] ; then # interactive shell&lt;br /&gt;
-    export STARTTIME=`date` \&lt;br /&gt;
-           HISTORYDUMP=0&lt;br /&gt;
+    export STARTTIME=`date &#039;+%Y-%m-%d %H:%M:%S&#039;` \&lt;br /&gt;
+           HISTORYDUMP=0 \&lt;br /&gt;
+          HISTTIMEFORMAT=&#039;%F %T &#039;&lt;br /&gt;
     shopt -s cmdhist histappend&lt;br /&gt;
 &lt;br /&gt;
     # Next function really stores the history logs. Besides that if you want to store whatever you have so far&lt;br /&gt;
     # you can call this function and it will save increment from the last call to it.&lt;br /&gt;
     archive_history()&lt;br /&gt;
     {&lt;br /&gt;
         TFILE=${HISTFILE}.$$.temp&lt;br /&gt;
-       CURTIME=`date`&lt;br /&gt;
+       CURTIME=`date &#039;+%Y-%m-%d %H:%M:%S&#039;`&lt;br /&gt;
        CURTTY=`tty`&lt;br /&gt;
         #STAMP&lt;br /&gt;
         HISTORYDUMP=$(($HISTORYDUMP+1))&lt;br /&gt;
         # Title&lt;br /&gt;
         echo &amp;quot;#$USER@${HOSTNAME} [ ${STARTTIME} - ${CURTIME} ]:$HISTORYDUMP ($CURTTY) ----&amp;quot; &amp;gt;| $TFILE&lt;br /&gt;
         # Fresh tasty history&lt;br /&gt;
        history -a ${TFILE}&lt;br /&gt;
         # Append it to the archive&lt;br /&gt;
-        cat ${TFILE} &amp;gt;&amp;gt; ${HISTFILE}.archive&lt;br /&gt;
+        #cat ${TFILE} &amp;gt;&amp;gt; ${HISTFILE}.archive&lt;br /&gt;
+       # But we add a converted version of the timestamp&lt;br /&gt;
+       gawk &#039;/^#[0-9]+$/{print strftime(&amp;quot;#%F %T &amp;quot;, gensub(/#/,&amp;quot;&amp;quot;,&amp;quot;g&amp;quot;)) }//&#039; ${TFILE} &amp;gt;&amp;gt; ${HISTFILE}.archive&lt;br /&gt;
         # Adjust history file itself&lt;br /&gt;
         # of cause we need to dump to some temp file and then rename... he heh&lt;br /&gt;
         cat ${HISTFILE} ${TFILE} | tail -${HISTSIZE} &amp;gt;| ${HISTFILE}.$$&lt;br /&gt;
         mv -f ${HISTFILE}.$$ ${HISTFILE}&lt;br /&gt;
         # clean up after yourself - to be absessive remove the other tmp file as well -&lt;br /&gt;
         # - might happen if there were no permission to move it at above point&lt;br /&gt;
         rm -rf ${TFILE} ${HISTFILE}.$$&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
-    trap &#039;archive_history&#039; EXIT&lt;br /&gt;
+    trap &#039;archive_history&#039; EXIT HUP&lt;br /&gt;
     unset IGNOREEOF&lt;br /&gt;
 &lt;br /&gt;
 fi&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
And to install the script it is better to insert the following in ~/.bashrc&lt;br /&gt;
 source ~/.bashrc_history&lt;br /&gt;
because e.g. with &amp;quot;vserver xxx enter&amp;quot; the shell is not started in the homedir&lt;br /&gt;
&amp;lt;br&amp;gt;[&#039;&#039;&#039;UPDATE&#039;&#039;&#039;] now this &amp;quot;patch&amp;quot; is included in the upstream version, thanks Yaroslav!&lt;br /&gt;
&amp;lt;br&amp;gt;Another nice tip from Yaroslav is to define another function in .bashrc_history:&lt;br /&gt;
 gbha () { grep &amp;quot;$@&amp;quot; ~/.bash_history.archive; }&lt;br /&gt;
so you just type &amp;quot;gbha PATTERN&amp;quot; (gbha = grep bash history archive) to find what you need!&lt;br /&gt;
====Examples====&lt;br /&gt;
What did I install last?&lt;br /&gt;
 gbha &amp;quot;^apt-get install&amp;quot;|tail&lt;br /&gt;
When did I modified syslog.conf?&lt;br /&gt;
 gbha -B2 &amp;quot;syslog.conf&amp;quot;&lt;br /&gt;
What did I do that day between 10:00 and 11:59?&lt;br /&gt;
 gbha -E -A2 &amp;quot;#2006-12-09 1[01]:&amp;quot;&lt;br /&gt;
== Conversion d&#039;une string hex en texte ==&lt;br /&gt;
&lt;br /&gt;
Input: 48656c6c6f20776f726c640a &amp;lt;br /&amp;gt;Output:Hello world&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;48656c6c6f20776f726c640a&amp;quot; |\&lt;br /&gt;
     gawk  &#039;BEGIN{FS=&amp;quot;&amp;quot;};{for(i=1;i&amp;amp;lt;=NF;i+=2)printf(&amp;quot;%c&amp;quot;,strtonum(&amp;quot;0x&amp;quot;$i$(i+1)))}&#039;&lt;br /&gt;
&lt;br /&gt;
In Perl:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 my $str = &amp;quot;02000501524550454154434f44453a323530&amp;quot;;&lt;br /&gt;
 print pack(&#039;C*&#039;, map { hex } unpack(&#039;(A2)*&#039;, $str));&lt;br /&gt;
==Compter en hex==&lt;br /&gt;
 for ((i=0;i&amp;lt;65536;i++)) ; do f=$(printf &amp;quot;%0*X\n&amp;quot; 4 $i); echo $f;done&lt;br /&gt;
==DON&#039;T TRY AT HOME==&lt;br /&gt;
 :(){ :|:&amp;amp; };:&lt;/div&gt;</summary>
		<author><name>~newacct</name></author>
	</entry>
</feed>