[lang_en]If you, like me, strive to keep a well organized and modular configuration setup, sooner or later will split your bash configuration into multiple files. And if you want it to be valid for multiple machines you’ll appreciate the following function:[/lang_en][lang_pt]Se, como eu, sempre tenta organizar e ter uma configuração modular, mais cedo ou mais tarde vai separar a configuração bash em vários ficheiros. Além disso, se quiser usar a mesma em várias máquinas, esta função dá jeito:[/lang_pt]


1
2
3
source_if_exists () {
    for i_; do test -f "$i_" && source "$i_"; done
}

[lang_en]This way, you can try to process other configuration fragments only when they exist:[/lang_en][lang_pt]Desta forma pode tentar incluir os fragmentos apenas onde e quando existem:[/lang_pt]


1
source_if_exists ${HOME}/etc/bash_completion

[lang_en]Another common need for bash configuration is to either append or prepend paths to an environment variable. When doing a configuration for multiple machines it is always a pain to make sure those paths exist. These functions take care of that making adding directories to the path a breeze:[/lang_en][lang_pt]Algo que também é comum fazer é acrescentar um directório a uma variável de ambiente. Quando se faz uma configuração para várias máquina é sempre um inconveniente assegurar que os directórios existem. Estas funções tratam do problema:[/lang_pt]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
safe_prepend_path() {
    VAR=$1
    shift
    VAL=$(eval echo \$${VAR})
    for i_; do
        test -d "$i_" && VAL=$i_${VAL:+":$VAL"}
    done
    eval export ${VAR}=$(echo ${VAL})
}

safe_append_path() {
    VAR=$1
    shift
    VAL=$(eval echo \$${VAR})
    for i_; do
        test -d "$i_" && VAL=${VAL:+"$VAL:"}$i_
    done
    eval export ${VAR}=$(echo ${VAL})
}

[lang_en]Example:[/lang_en][lang_pt]Exemplos:[/lang_pt]


1
2
safe_prepend_path PATH /opt/local/bin ${HOME}/bin
safe_append_path MANPATH /opt/local/man ${HOME}/man

[lang_en]Also, it is not unusual to have some path variables with duplicate entries, which is not very efficient. This function makes sure that a path only occurs once, and retains only the first occurrence, thus keeping the search order intact.[/lang_en][lang_pt]É frequente que as variáveis de ambiente com directórios apresentem entradas duplicadas o que torna a pesquisa menos eficiente. Esta função assegura que cada directório ocorre apenas uma vez e retem apenas a primeira entrada, mantendo por isso a ordem.[/lang_pt]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
unique_path() {
    eval export $1=$(echo $(eval echo \$${1}) | \
        awk -F:                   \
        '{ a[$1];                 \
           printf "%s",$1;        \
           for(i=2;i<=NF;i++) {   \
             if(!($i in a)) {     \
               printf ":%s",$i;   \
             };                   \
             a[$i];               \
           };                     \
           printf "\n";           \
         }'
)
}

[lang_en]Example:[/lang_en][lang_pt]Examples:[/lang_pt]


1
2
unique_path PATH
unique_path MANPATH

Leave a Reply

You must be logged in to post a comment.

-->