Comment manipuler une chaîne, variable en shell

Hei tout le monde!

J’ai cette variable en shell contenant des chemins séparés par un espace:

LINE="/path/to/manipulate1 /path/to/manipulate2" 

Je veux append une chaîne de chemin supplémentaire au début de la chaîne et aussi bien juste après l’espace pour que la variable ait le résultat suivant:

 LINE="/additional/path1/to/path/to/manipulate1 /additional/path2/to/path/to/manipulate2" 

J’ai essayé celui-ci mais seulement les anciennes voies

 #!/bin/bash LINE="/path/to/one /path/to/two" NEW_PATH=`echo $LINE | sed "s/^\([^ ]\+\) \([^ ]\+\)/\/add1\1 \/add2\2/"` echo "$NEW_PATH" 

Toute aide appréciée Merci d’avance

Ceci des cours perturbe tous les arguments précédents que vous pourriez avoir besoin de garder.

 set $LINE LINE="/additional/path1$1 /additional/path2$2" 

Testé en bash / dash / ksh.

Edit: Si vous souhaitez conserver les arguments originaux, cela peut être utile:

 orig=$@  set $orig 
 firstPath=$(echo $LINE | cut -d' ' -f1) secondPath=$(echo $LINE | cut -d' ' -f2) firstPath="/additional/path1/to$firstPath" secondPath="/additional/path2/to$secondPath" 
 $ test="/sbin /usr/sbin /bin /usr/bin /usr/local/bin /usr/X11R6/bin" $ test2=$(for i in $test; do echo "/newroot${i}"; done) $ echo $test2 /newroot/sbin /newroot/usr/sbin /newroot/bin /newroot/usr/bin /newroot/usr/local/bin /newroot/usr/X11R6/bin 

Puisque vous utilisez Bash:

Si les ajouts sont les mêmes pour chaque partie:

 LINE="/path/to/manipulate1 /path/to/manipulate2" array=($LINE) LINE=${array[@]/#//additional/path/to/} 

S’ils sont différents:

 LINE="/path/to/manipulate1 /path/to/manipulate2" array=($LINE) array[0]=/additional/path1/to${array[0]} array[1]=/additional/path2/to${array[1]} LINE=${array[@]} 

Ou, de manière plus souple:

 LINE="/path/to/manipulate1 /path/to/manipulate2" array=($LINE) parts=(/additional/path1/to /additional/path2/to) if (( ${#array[@]} == ${#parts[@]} )) then for ((i=0; i<${#array[@]}; i++)) do array[i]=${parts[i]}${array[i]} done fi LINE=${array[@]} 
 NEW_PATH=`echo $LINE | sed "s/^\([^ ]\+\) \([^ ]\+\)/\/add1\1 \/add2\2/"` 

se traduit par NEW_PATH =

/ add1 / path / to / manipulate1 / add2 / path / to / manipulate2