Alternativas para [variável = substituição de string] no bash

4

Estou aprendendo bash e tentando entender as diferentes maneiras de fazer uma substituição e atribuí-la a uma variável .

Especificamente, estou alterando as barras Unix ('/') para o Windows ('\').

Referências:

Com agradecimentos a Seth e Terdon.

Versão atualizada. Original na parte inferior.

#!/bin/bash
origPath="/home/django_auth_lifecycle/urls.py"

path=$origPath
path=${path//\//\}                      #Works
echo "Via bash (2-slash, no-g): $path"

path=$origPath
path=${path//\//\}                      #Works
echo "Via bash (no dollar): $path"

path=$origPath
echo "${path}" | sed -e 's/\//\/g'     #Works, but don't know how to assign to var
#echo "Via sed (slash, quotes): $path"

path=$origPath
path=$(echo "$path" | sed s/'\/'/'\'/g)   #Works
echo "Via sed (slash, quotes): $path"

path=$origPath
path=$(echo $path | sed s:'\/':'\':g)   #Works
echo "Via sed (colon, quotes): $path"

path=$origPath                           #This and all following are
path=sed 's:/:\:g' <<<$path             #s:/:\:g: No such file or directory
echo "Via sed/<<< (colon): $path"

path=$origPath
path=sed 's/\//\:g' <<<$path
echo "Via sed/<<< (slash): $path"

path=$origPath
path=sed 's/"\/"/"\":g' <<<$path
echo "Via sed/<<< (slash, quotes): $path"

Saída:

$ bash ./initial_script3.sh
Via bash (2-slash, no-g): \home\django_auth_lifecycle\urls.py
Via bash (no dollar): \home\django_auth_lifecycle\urls.py
./initial_script3.sh: line 13: ${echo "${path}" | sed -e 's/\//\/g'}: bad substitution
Via sed (slash, quotes): /home/django_auth_lifecycle/urls.py
Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py
Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py
./initial_script3.sh: line 25: s:/:\:g: No such file or directory
Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py
./initial_script3.sh: line 29: s/\//\:g: No such file or directory
Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py
./initial_script3.sh: line 33: s/"\/"/"\":g: No such file or directory
Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py

Saída com depuração:

$ bash -x ./initial_script3.sh
+ origPath=/home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via bash (2-slash, no-g): \home\django_auth_lifecycle\urls.py'
Via bash (2-slash, no-g): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via bash (no dollar): \home\django_auth_lifecycle\urls.py'
Via bash (no dollar): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ sed -e 's/\//\/g'
+ echo /home/django_auth_lifecycle/urls.py
\home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
++ sed 's/\//\/g'
++ echo /home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py'
Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
++ sed 's:\/:\:g'
++ echo /home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py'
Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's:/:\:g'
./initial_script3.sh: line 25: s:/:\:g: No such file or directory
+ echo 'Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's/\//\:g'
./initial_script3.sh: line 29: s/\//\:g: No such file or directory
+ echo 'Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's/"\/"/"\":g'
./initial_script3.sh: line 33: s/"\/"/"\":g: No such file or directory
+ echo 'Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py

Original

Abaixo está o que eu tentei. Infelizmente, só consigo um método funcionar (sed com um pipe e citando cada elemento). O primeiro deles estava funcionando, eu juro, e agora não é.

O que estou fazendo de errado?

#!/bin/bash
origPath="/home/django_auth_lifecycle/urls.py"

path=$origPath
path=${$path/\//\/g}                    #WAS working, now not
echo "Via bash: $path"                   # ${$path/\//\/g}: bad substitution

path=$origPath
path=${path/\//\/g}                     #Bad output:
echo "Via bash (no dollar): $path"       #'\/ghome/django_auth_lifecycle/urls.py'

path=$origPath
path=$(echo $path | sed s/'\/'/'\'/g)   #Works
echo "Via sed (slash, quotes): $path"

path=$origPath
#path=$(echo $path | sed s/\//\/g)      #No error, but output=input
echo "Via sed (slash): $path"

path=$origPath
path=$(echo $path | sed s:'\/':'\':g)   #Works
echo "Via sed (colon, quotes): $path"

path=$origPath
path=$(echo $path | sed s:\/:\:g)       #sed: -e expression #1, char 7:
echo "Via sed (colon): $path"            #     unterminated 's' command

path=$origPath                           #This and all following are
path=sed 's:/:\:g' <<<$path             #s:/:\:g: No such file or directory
echo "Via sed/<<< (colon): $path"

path=$origPath
path=sed 's/\//\:g' <<<$path
echo "Via sed/<<< (slash): $path"

path=$origPath
path=sed 's/"\/"/"\":g' <<<$path
echo "Via sed/<<< (slash, quotes): $path"

Saída:

$ bash ./initial_script3.sh
./initial_script3.sh: line 5: ${$path/\//\/g}: bad substitution
Via bash: /home/django_auth_lifecycle/urls.py
Direct echo: \/ghome/django_auth_lifecycle/urls.py
Via bash (no dollar): \/ghome/django_auth_lifecycle/urls.py
./initial_script3.sh: line 14: \/ghome/django_auth_lifecycle/urls.py: No such file or directory
Via echo bash (no dollar): /home/django_auth_lifecycle/urls.py
Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py
Via sed (slash): /home/django_auth_lifecycle/urls.py
Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py
sed: -e expression #1, char 7: unterminated 's' command
Via sed (colon):
./initial_script3.sh: line 34: s:/:\:g: No such file or directory
Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py
./initial_script3.sh: line 38: s/\//\:g: No such file or directory
Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py
./initial_script3.sh: line 42: s/"\/"/"\":g: No such file or directory
Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py

Saída com depuração:

$ bash -x ./initial_script3.sh
+ origPath=/home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
./initial_script3.sh: line 5: ${$path/\//\/g}: bad substitution
+ echo 'Via bash: /home/django_auth_lifecycle/urls.py'
Via bash: /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ echo 'Direct echo: \/ghome/django_auth_lifecycle/urls.py'
Direct echo: \/ghome/django_auth_lifecycle/urls.py
+ path='\/ghome/django_auth_lifecycle/urls.py'
+ echo 'Via bash (no dollar): \/ghome/django_auth_lifecycle/urls.py'
Via bash (no dollar): \/ghome/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=echo
+ '\/ghome/django_auth_lifecycle/urls.py'
./initial_script3.sh: line 14: \/ghome/django_auth_lifecycle/urls.py: No such file or directory
+ echo 'Via echo bash (no dollar): /home/django_auth_lifecycle/urls.py'
Via echo bash (no dollar): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
++ sed 's/\//\/g'
++ echo /home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py'
Via sed (slash, quotes): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ echo 'Via sed (slash): /home/django_auth_lifecycle/urls.py'
Via sed (slash): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
++ sed 's:\/:\:g'
++ echo /home/django_auth_lifecycle/urls.py
+ path='\home\django_auth_lifecycle\urls.py'
+ echo 'Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py'
Via sed (colon, quotes): \home\django_auth_lifecycle\urls.py
+ path=/home/django_auth_lifecycle/urls.py
++ sed 's:/:\:g'
sed: -e expression #1, char 7: unterminated 's' command
++ echo /home/django_auth_lifecycle/urls.py
+ path=
+ echo 'Via sed (colon): '
Via sed (colon):
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's:/:\:g'
./initial_script3.sh: line 34: s:/:\:g: No such file or directory
+ echo 'Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (colon): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's/\//\:g'
./initial_script3.sh: line 38: s/\//\:g: No such file or directory
+ echo 'Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (slash): /home/django_auth_lifecycle/urls.py
+ path=/home/django_auth_lifecycle/urls.py
+ path=sed
+ 's/"\/"/"\":g'
./initial_script3.sh: line 42: s/"\/"/"\":g: No such file or directory
+ echo 'Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py'
Via sed/<<< (slash, quotes): /home/django_auth_lifecycle/urls.py
    
por aliteralmind 09.01.2015 / 21:34

1 resposta

7

Com "Expansão de Parâmetro":

origPath="/home/django_auth_lifecycle/urls.py"
path="${origPath//\//\}"
echo $path

Saída:

\home\django_auth_lifecycle\urls.py

Com um cano, sed e "Substituição de Comando":
origPath="/home/django_auth_lifecycle/urls.py"
path="$(echo "${origPath}" | sed -e 's/\//\/g')"
echo $path

Saída:

\home\django_auth_lifecycle\urls.py

Com sed, "Here String" e "Substituição de Comando":
origPath="/home/django_auth_lifecycle/urls.py"
path="$(sed -e 's/\//\/g' <<< "${origPath}")"
echo $path

Saída:

\home\django_auth_lifecycle\urls.py
    
por Cyrus 09.01.2015 / 23:32