Bash

De Admin -- TALEVAS.
(Différences entre les versions)
(Pattern Matching)
 
(10 révisions intermédiaires par un utilisateur 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 85 : Ligne 109 :
 
== Jouer avec les variables ==
 
== Jouer avec les variables ==
 
http://linuxgazette.net/issue57/eyler.html
 
http://linuxgazette.net/issue57/eyler.html
 
===Pattern Matching===
 
 
There are two kinds of pattern matching available, matching from the left and matching from the right. The operators, with their functions and an expample, are shown in the following table:
 
 
{|Operator ||Function || Example|
 
|${foo#t*is}|| deletes the shortest possible match from the left|| export $foo="this is a test"|
 
||||echo ${foo#t*is}|
 
|||||is a test|
 
|${foo##t*is}|| deletes the longest possible match from the left || export $foo="this is a test"|
 
|||||echo ${foo#t*is}|
 
|||||a test|
 
|${foo%t*st} || deletes the shortest possible match from the right|| export $foo="this is a test"|
 
|||||echo ${foo%t*st}|
 
|||||this is a|
 
|${foo%%t*st}|| deletes the longest possible match from the right || export $foo="this is a test"|
 
|||||echo ${foo#t*is}|}
 
 
 
    Note: While the # and % identifiers may not seem obvious, they have a convenient mnemonic. The # key is on the left side of the $ key and operates from the left. The % key is on the right of the $ key and operated from the right.
 
  
 
== variables var decoupage ==
 
== variables var decoupage ==
Ligne 118 : Ligne 122 :
 
  cut -d' ' -f 6,7,8,9
 
  cut -d' ' -f 6,7,8,9
  
 +
== tr ==
 +
$ echo 'hi' | tr '[:lower:]' '[:upper:]'
 +
HI
  
 
== FIND ==
 
== FIND ==
Ligne 123 : Ligne 130 :
 
  find /PATH/to/FILE/  -name [0-4]-[0-4] -exec ls -l {} \;
 
  find /PATH/to/FILE/  -name [0-4]-[0-4] -exec ls -l {} \;
  
== decouper une straing variable en array ==
+
 
 +
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"
 
  string="10.10.25.211 10.10.25.212 10.10.25.213"
 
  myarray=($string)
 
  myarray=($string)
Ligne 134 : Ligne 145 :
 
  echo ${#myarray[@]}
 
  echo ${#myarray[@]}
 
  3
 
  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

Sommaire

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

[1]

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

[2]

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