Bash
| (26 révisions intermédiaires par 3 utilisateurs sont masquées) | |||
| Ligne 1 : | Ligne 1 : | ||
| + | == PageUp and PageDown history search auto completion on the BASH shell == | ||
| + | |||
| + | http://www.electrictoolbox.com/pageup-history-auto-completion-bash-shell/ | ||
| + | |||
| + | /etc/inputrc | ||
| + | |||
| + | The PageUp and PageDown keys are set with the "\e[5~": & "\e[6~": lines, so you'd need to comment them out and add new commands as follows: | ||
| + | #"\e[5~": beginning-of-history | ||
| + | #"\e[6~": end-of-history | ||
| + | "\e[5~": history-search-backward | ||
| + | "\e[6~": history-search-forward | ||
| + | |||
| + | == reptyr == | ||
| + | |||
| + | retrieve a pid | ||
| + | |||
| + | récupérer le pid dans un autre shell | ||
| + | |||
| + | == hard reboot, magic commandes, /proc/ == | ||
| + | !!! C'est comme appuyer sur RESET !!! | ||
| + | |||
| + | echo 1 > /proc/sys/kernel/sysrq | ||
| + | echo b > /proc/sysrq-trigger | ||
| + | |||
== les commandes de test == | == les commandes de test == | ||
| Ligne 23 : | Ligne 47 : | ||
if [ -z "$TEST" ] ; then echo ok; else echo plouf; fi | if [ -z "$TEST" ] ; then echo ok; else echo plouf; fi | ||
Retourne OK si le user n'existe pas ou PLOUFF s'il existe | Retourne OK si le user n'existe pas ou PLOUFF s'il existe | ||
| + | |||
| + | `ARG1 -eq ARG2' | ||
| + | `ARG1 -ne ARG2' | ||
| + | `ARG1 -lt ARG2' | ||
| + | `ARG1 -le ARG2' | ||
| + | `ARG1 -gt ARG2' | ||
| + | `ARG1 -ge ARG2' | ||
| + | |||
| + | These arithmetic binary operators return true if ARG1 is equal, | ||
| + | not-equal, less-than, less-than-or-equal, greater-than, or | ||
| + | greater-than-or-equal than ARG2, respectively. | ||
| + | |||
tiré du manuel => | tiré du manuel => | ||
| Ligne 36 : | Ligne 72 : | ||
---- | ---- | ||
| + | == SED / sed == | ||
| − | + | [http://www.gentoo.org/doc/fr/articles/l-sed1.xml] | |
| − | + | <nowiki>sed -i s/avant/après/g file.txt | |
| − | sed -i s/avant/après/g file.txt | + | -i[SUFFIX], --in-place[=SUFFIX]</nowiki> |
| − | -i | + | |
edit files in place (makes backup if extension supplied) | edit files in place (makes backup if extension supplied) | ||
| − | sed -i 3{s/\[\[:alnum:\]\]/t/g} test.fr.vhost | + | <nowiki>sed -i 3{s/\[\[:alnum:\]\]/t/g} test.fr.vhost</nowiki> |
3 => 3eme ligne | 3 => 3eme ligne | ||
{ ce qu'on y applique } | { ce qu'on y applique } | ||
| + | |||
| + | <nowiki>sed -i 3{s/[[:alnum:]]*^/$date/g} test.fr.vhost</nowiki> | ||
| + | <nowiki>[[:alnum:]]*^</nowiki> => tout les alphanumériques et n'importe quoi ensuite. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | == EXPR == | ||
| + | |||
| + | [http://tldp.org/LDP/abs/html/string-manipulation.html] | ||
| + | |||
| + | expr substr $VALUE 9 2 | ||
| + | expr length $VALUE | ||
| + | if [ `expr length $VALUE` -lt "2" ] | ||
| + | |||
| + | ----- | ||
| + | |||
| + | == Addition while== | ||
| + | |||
| + | while [ $i -le 250 ]; do echo toto; i=$[i+1]; done; | ||
| + | |||
| + | |||
| + | |||
| + | == Jouer avec les variables == | ||
| + | http://linuxgazette.net/issue57/eyler.html | ||
| + | |||
| + | == variables var decoupage == | ||
| + | |||
| + | cat ${PATHFROM}/frontaux.access.${DATE}.log | while read ligne ; do echo ${ligne#*|} >> ${PATHTO}/${ligne%|*}.file; done | ||
| + | |||
| + | prendre les 3 dernier char | ||
| + | ${PathToFile:$((${#PathToFile} - 3)):3 } | ||
| + | |||
| + | == cut == | ||
| + | |||
| + | change le délimiteur par defaut <tab> pour l'espace et prends les champs de 6 à 9 | ||
| + | cut -d' ' -f 6,7,8,9 | ||
| + | |||
| + | == tr == | ||
| + | $ echo 'hi' | tr '[:lower:]' '[:upper:]' | ||
| + | HI | ||
| + | |||
| + | == FIND == | ||
| + | |||
| + | find /PATH/to/FILE/ -name [0-4]-[0-4] -exec ls -l {} \; | ||
| + | |||
| + | |||
| + | trouver les fichiers modifié dans un interval de temps : | ||
| + | find . -mmin $((($(date --date='2019-04-16 11:00:00' +%s) - $(date --date='now' +%s)) /60)) | ||
| + | |||
| + | == decouper une string variable en array == | ||
| + | string="10.10.25.211 10.10.25.212 10.10.25.213" | ||
| + | myarray=($string) | ||
| + | echo ${myarray[0]} | ||
| + | 10.10.25.211 | ||
| + | echo ${myarray[1]} | ||
| + | 10.10.25.212 | ||
| + | .... | ||
| + | trouver le nombre d'objets dans le array | ||
| + | echo ${#myarray[@]} | ||
| + | 3 | ||
| + | |||
| + | == Déterminer le path du script en cours d’exécution == | ||
| + | |||
| + | `dirname \"$0\"` | ||
| + | |||
| + | == ls == | ||
| + | # ls -s | awk '{ SUM+=$1 } END{print SUM}' | ||
| + | 1116 | ||
| + | # ls -l | grep total | ||
| + | total 1116 | ||
| + | That is the total number of file system blocks, including indirect blocks, used by the listed files. | ||
| + | |||
| + | |||
| + | == exit == | ||
| + | |||
| + | exit 0 == OK | ||
| + | exit 1 == false | ||
| + | |||
| + | true # The "true" builtin. | ||
| + | echo "exit status of \"true\" = $?" # 0 | ||
| + | |||
| + | ! true | ||
| + | echo "exit status of \"! true\" = $?" # 1 | ||
Version actuelle en date du 6 août 2020 à 15:15
PageUp and PageDown history search auto completion on the BASH shell
http://www.electrictoolbox.com/pageup-history-auto-completion-bash-shell/
/etc/inputrc
The PageUp and PageDown keys are set with the "\e[5~": & "\e[6~": lines, so you'd need to comment them out and add new commands as follows:
#"\e[5~": beginning-of-history #"\e[6~": end-of-history "\e[5~": history-search-backward "\e[6~": history-search-forward
reptyr
retrieve a pid
récupérer le pid dans un autre shell
hard reboot, magic commandes, /proc/
!!! C'est comme appuyer sur RESET !!!
echo 1 > /proc/sys/kernel/sysrq echo b > /proc/sysrq-trigger
les commandes de test
Tester un fichier (de type répertoire) dans l'exemple suivant =>
if [ -d /home/talevas ]; then echo test; fi
if test -d /home/talevas ; then echo ok; else echo plouf; fi if [ -d /home/talevas ]; then echo ok; else echo plouf; fi
Tester une chaine de caractère =>
Ici on test un user existe dans /etc/pasword.
grep ${USER} /etc/passwd
cette commande va nous retourne un STRING ou pas selon que le suer existe ou non
TEST=grep ${USER} /etc/passwd
if test -n "$TEST" ; then echo ok; else echo plouf; fi
if [ -n "$TEST" ] ; then echo ok; else echo plouf; fi
Retourne OK si le user existe ou PLOUFF s'il n'existe pas
TEST=grep ${USER} /etc/passwd
if test -z "$TEST" ; then echo ok; else echo plouf; fi
if [ -z "$TEST" ] ; then echo ok; else echo plouf; fi
Retourne OK si le user n'existe pas ou PLOUFF s'il existe
`ARG1 -eq ARG2' `ARG1 -ne ARG2' `ARG1 -lt ARG2' `ARG1 -le ARG2' `ARG1 -gt ARG2' `ARG1 -ge ARG2'
These arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2, respectively.
tiré du manuel =>
`-z STRING'
True if the length of STRING is zero.
`-n STRING'
`STRING'
True if the length of STRING is nonzero.
SED / sed
sed -i s/avant/après/g file.txt -i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
sed -i 3{s/\[\[:alnum:\]\]/t/g} test.fr.vhost
3 => 3eme ligne
{ ce qu'on y applique }
sed -i 3{s/[[:alnum:]]*^/$date/g} test.fr.vhost
[[:alnum:]]*^ => tout les alphanumériques et n'importe quoi ensuite.
EXPR
expr substr $VALUE 9 2 expr length $VALUE if [ `expr length $VALUE` -lt "2" ]
Addition while
while [ $i -le 250 ]; do echo toto; i=$[i+1]; done;
Jouer avec les variables
http://linuxgazette.net/issue57/eyler.html
variables var decoupage
cat ${PATHFROM}/frontaux.access.${DATE}.log | while read ligne ; do echo ${ligne#*|} >> ${PATHTO}/${ligne%|*}.file; done
prendre les 3 dernier char
${PathToFile:$((${#PathToFile} - 3)):3 }
cut
change le délimiteur par defaut <tab> pour l'espace et prends les champs de 6 à 9
cut -d' ' -f 6,7,8,9
tr
$ echo 'hi' | tr '[:lower:]' '[:upper:]' HI
FIND
find /PATH/to/FILE/ -name [0-4]-[0-4] -exec ls -l {} \;
trouver les fichiers modifié dans un interval de temps :
find . -mmin $((($(date --date='2019-04-16 11:00:00' +%s) - $(date --date='now' +%s)) /60))
decouper une string variable en array
string="10.10.25.211 10.10.25.212 10.10.25.213"
myarray=($string)
echo ${myarray[0]}
10.10.25.211
echo ${myarray[1]}
10.10.25.212
....
trouver le nombre d'objets dans le array
echo ${#myarray[@]}
3
Déterminer le path du script en cours d’exécution
`dirname \"$0\"`
ls
# ls -s | awk '{ SUM+=$1 } END{print SUM}'
1116
# ls -l | grep total
total 1116
That is the total number of file system blocks, including indirect blocks, used by the listed files.
exit
exit 0 == OK exit 1 == false
true # The "true" builtin. echo "exit status of \"true\" = $?" # 0 ! true echo "exit status of \"! true\" = $?" # 1