Bash
(→Jouer avec les variables) |
(→Pattern Matching) |
||
| Ligne 89 : | Ligne 89 : | ||
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: | 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| | |Operator |Function | Example| | ||
|${foo#t*is}| deletes the shortest possible match from the left| export $foo="this is a test"| | |${foo#t*is}| deletes the shortest possible match from the left| export $foo="this is a test"| | ||
Version du 16 août 2012 à 12:35
Sommaire |
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
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
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
FIND
find /PATH/to/FILE/ -name [0-4]-[0-4] -exec ls -l {} \;
decouper une straing 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