问题描述
我想在Linux Shell脚本中向用户回显Windows共享文件夹地址,该地址是这样的字符串:\\hostname\release\1.02A01。最后一个字符串(1.02A01)是版本号,每次运行脚本时都会更改。我在sh(不是bash)中尝试了类似的方法,但是它不起作用:
I want to echo a Windows shared folder address to users in a Linux shell script, the address are strings like this: \\hostname\release\1.02A01. and the last string(1.02A01) is a version number, it's changed each time when I run the script. I tried something like this in sh (not bash ), but it doesn't work:
version=$1 # version number are get from the parameter
repository="\\\hostname\release\$version"
echo $repository # I get this: \hostname\dir$version
这是两个错误:
- 双反斜杠不正确。
- 版本解析不正确。
推荐答案
1)在unix / linux sh中,反斜杠( \)具有
2)您在INSIDE引号中进行的字符串连接是LITERAL,如果您想要$ version的值,则必须将其置于OUTSIDE
1) In unix/linux sh, the backslash("\") has to be escaped/preceded with a backslash("\").
2) The string concatenation you are doing INSIDE quotes is LITERAL, if you want the value of $version, you have to put it OUTSIDE the closing quote.
我将其放在centos linux的shell(shell1)中并在 sh下执行:
I put this in a shell ( shell1 ) in centos linux and executed it under "sh":
sh-4.1# cat shell1
version=$1
repository="\\\\hostname\\release\\"$version
echo $repository
这是输出:
sh-4.1# ./shell1 1.02A01 <br>
\\hostname\release\1.02A01
这篇关于如何回声一个双反斜杠以及一个变量(版本号),例如“ \\hostname\release\1.02A01”;通过使用sh?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!