Bash

De Admin -- TALEVAS.
(Différences entre les versions)
(cut)
(decouper une string variable en array)
Ligne 130 : Ligne 130 :
 
  echo ${#myarray[@]}
 
  echo ${#myarray[@]}
 
  3
 
  3
 +
 +
== Déterminer le path du script en cours d’exécution ==
 +
 +
`dirname \"$0\"`
  
 
== ls ==
 
== ls ==

Version du 31 octobre 2012 à 13:30

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


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

dams@ubuntu:~/Bureau$ echo 'hi' | tr '[:lower:]' '[:upper:]'
HI

FIND

find /PATH/to/FILE/  -name [0-4]-[0-4] -exec ls -l {} \;

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.