Linux / Unix‎ > ‎

Bash

bash competion otpions : http://caliban.org/bash/Manipulation sur les dates

- date '+%d-%m-%Y' --date '1 days ago' -> HIER
- date '+%d-%m-%Y' --date '1 days' -> DEMAIN
Faire un traitement sur toutes les d'un fichier

#! /bin/bash
while read line
do 
echo -e "$line\n"
done < file.txt
Faire un switch/case en bash

case $ACTION in
CREATE) echo create $FILE;;
DELETE) echo delete $FILE;;
*) echo ignored ;;
esac

String manipulation

http://tldp.org/LDP/abs/html/string-manipulation.html bash competion otpions : http://caliban.org/bash/
Manipulation sur les dates

- date '+%d-%m-%Y' --date '1 days ago' -> HIER
- date '+%d-%m-%Y' --date '1 days' -> DEMAIN
Faire un traitement sur toutes les d'un fichier

#! /bin/bash
while read line
do 
echo -e "$line\n"
done < file.txt
Faire un switch/case en bash

case $ACTION in
CREATE) echo create $FILE;;
DELETE) echo delete $FILE;;
*) echo ignored ;;
esac

String manipulation

http://tldp.org/LDP/abs/html/string-manipulation.html 
Lenght

${#string}
example: 
stringZ=abcABC123ABCabc
echo ${#stringZ} # 15
Substring Replacement

${string/substring/replacement}

Replace first match of $substring with $replacement. [2] 
${string//substring/replacement}

Replace all matches of $substring with $replacement.

stringZ=abcABC123ABCabc

echo ${stringZ/abc/xyz} # xyzABC123ABCabc
# Replaces first match of 'abc' with 'xyz'.

echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
# Replaces all matches of 'abc' with # 'xyz'.

echo ---------------
echo "$stringZ" # abcABC123ABCabc
echo ---------------
# The string itself is not altered!

# Can the match and replacement strings be parameterized?
match=abc
repl=000
echo ${stringZ/$match/$repl} # 000ABC123ABCabc
# ^ ^ ^^^
echo ${stringZ//$match/$repl} # 000ABC123ABC000
# Yes! ^ ^ ^^^ ^^^

echo

# What happens if no $replacement string is supplied?
echo ${stringZ/abc} # ABC123ABCabc?
echo ${stringZ//abc} # ABC123ABC?
# A simple deletion takes place.

${string/#substring/replacement}

If $substring matches front end of $string, substitute $replacement for $substring.
${string/%substring/replacement}

If $substring matches back end of $string, substitute $replacement for $substring.

stringZ=abcABC123ABCabc

echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc?
# Replaces front-end match of 'abc' with 'XYZ'.

echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ
# Replaces back-end match of 'abc' with 'XYZ'.
Lenght

${#string}
example: 
stringZ=abcABC123ABCabc
echo ${#stringZ} # 15
Substring Replacement

${string/substring/replacement}

Replace first match of $substring with $replacement. [2] 
${string//substring/replacement}

Replace all matches of $substring with $replacement.

stringZ=abcABC123ABCabc

echo ${stringZ/abc/xyz} # xyzABC123ABCabc
# Replaces first match of 'abc' with 'xyz'.

echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
# Replaces all matches of 'abc' with # 'xyz'.

echo ---------------
echo "$stringZ" # abcABC123ABCabc
echo ---------------
# The string itself is not altered!

# Can the match and replacement strings be parameterized?
match=abc
repl=000
echo ${stringZ/$match/$repl} # 000ABC123ABCabc
# ^ ^ ^^^
echo ${stringZ//$match/$repl} # 000ABC123ABC000
# Yes! ^ ^ ^^^ ^^^

echo

# What happens if no $replacement string is supplied?
echo ${stringZ/abc} # ABC123ABCabc?
echo ${stringZ//abc} # ABC123ABC?
# A simple deletion takes place.

${string/#substring/replacement}

If $substring matches front end of $string, substitute $replacement for $substring.
${string/%substring/replacement}

If $substring matches back end of $string, substitute $replacement for $substring.

stringZ=abcABC123ABCabc

echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc?
# Replaces front-end match of 'abc' with 'XYZ'.

echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ
# Replaces back-end match of 'abc' with 'XYZ'.
Comments