Ho una funzione bash per impostare in $PATHquesto modo -
assign-path()
{
str=$1
# if the $PATH is empty, assign it directly.
if [ -z $PATH ]; then
PATH=$str;
# if the $PATH does not contain the substring, append it with ':'.
elif [[ $PATH != *$str* ]]; then
PATH=$PATH:$str;
fi
}
Ma il problema è che devo scrivere funzioni diverse per variabili diverse (ad esempio un'altra funzione $CLASSPATHcome like, assign-classpath()ecc.). Non sono riuscito a trovare un modo per passare l'argomento alla funzione bash in modo che possa accedervi come riferimento.
Sarebbe meglio se avessi qualcosa di simile -
assign( bigstr, substr )
{
if [ -z bigstr ]; then
bigstr=substr;
elif [[ bigstr != *str* ]]; then
bigstr=bigstr:substr;
fi
}
Qualche idea, come ottenere qualcosa come sopra in bash?
assign-path /abcnon accodare /abca PATH se $ PATH contiene già /abc/def, /abcd, /def/abcecc Soprattutto non è possibile aggiungere /binse PATH contiene già /usr/bin.
$PATHe la prova negate contro i tuoi argomenti come: add=/bin dir=/usr/bin ; [ -z "${dir%"$add"}" ] || dir="${dir}:${add}". Nella mia risposta lo faccio in questo modo con tutti gli argomenti che vuoi solo usare IFS=:.
$PATH? e Aggiungi directory a $PATHse non è già presente (su Super User ).