bash 中 [变量 = 字符串替换] 的替代方案

bash 中 [变量 = 字符串替换] 的替代方案

我正在学习 bash 并尝试了解进行替换的不同方法并将其分配给变量

具体来说,我将 Unix 斜杠 ('/') 更改为 Windows ('\')。

参考:

感谢 Seth 和 terdon。


更新版本。原始版本在底部。

#!/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"

输出:

$ 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

带调试的输出:

$ 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

原来的

以下是我尝试过的方法。不幸的是,我只能使用一种方法(使用管道引用每个元素)。我发誓,第一个是可以工作的,但现在不行了。

我究竟做错了什么?


#!/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"

输出:

$ 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

带调试的输出:

$ 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

答案1

使用“参数扩展”:

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

输出:

\主页\django_auth_lifecycle\urls.py


使用管道、sed 和“命令替换”:

origPath="/home/django_auth_lifecycle/urls.py"
path="$(echo "${origPath}" | sed -e 's/\//\\/g')"
echo $path

输出:

\主页\django_auth_lifecycle\urls.py


使用 sed、“此处字符串”和“命令替换”:

origPath="/home/django_auth_lifecycle/urls.py"
path="$(sed -e 's/\//\\/g' <<< "${origPath}")"
echo $path

输出:

\主页\django_auth_lifecycle\urls.py

相关内容