将shell脚本中的./替换为$PWD。

huangapple go评论93阅读模式
英文:

Replace ./ with $PWD in shell script

问题

I have string that may or may not start from ./, or may be just a ./
In that case I want to replace it with $PWD

Any help?

  1. APP_FOLDER=./service
  2. MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")

Causes "Wrong substitution".

英文:

I have string that may or may not start from ./, or may be just a ./
In that case I want to replace it with $PWD

Any help?

  1. APP_FOLDER=./service
  2. MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")

Causes "Wrong substitution"

答案1

得分: 2

不需要在Bash中使用子shell扩展:

  1. #!/usr/bin/env bash
  2. printf '%-10s %s\n' 'app_folder' 'modified_app_folder'
  3. for app_folder in ./service foobar ./ /tmp/test; do
  4. modified_app_folder=${app_folder/#.\//$PWD/}
  5. printf '%-10s %s\n' "$app_folder" "$modified_app_folder"
  6. done

示例输出:

  1. app_folder modified_app_folder
  2. ./service /home/lea/StackOverflow/service
  3. foobar foobar
  4. ./ /home/lea/StackOverflow/
  5. /tmp/test /tmp/test
英文:

It is not needed to use sub-shell expansion here with Bash:

  1. #!/usr/bin/env bash
  2. printf '%-10s %s\n' 'app_folder' 'modified_app_folder'
  3. for app_folder in ./service foobar ./ /tmp/test; do
  4. modified_app_folder=${app_folder/#.\//$PWD/}
  5. printf '%-10s %s\n' "$app_folder" "$modified_app_folder"
  6. done

Sample output:

  1. app_folder modified_app_folder
  2. ./service /home/lea/StackOverflow/service
  3. foobar foobar
  4. ./ /home/lea/StackOverflow/
  5. /tmp/test /tmp/test

答案2

得分: 1

这样:

始终在寻求人类帮助之前,通过 https://shelcheck.net 检查你的脚本。或者使用命令行:

  1. $ shellcheck file
  2. 在文件的第2行:
  3. MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")
  4. ^-- SC1007 (警告): 如果尝试分配一个值,请删除等号后的空格(对于空字符串,请使用 var='' ... )。
  5. ^-- SC1009 (信息): 上述语法错误在此命令扩展中。
  6. ^-- SC1073 (错误): 无法解析此测试表达式。修复以允许更多检查。
  7. ^-- SC1108 (错误): 等号前后需要有空格。
  8. ^-- SC1072 (错误): 期望比较运算符(不要在[]/ [[]]中包装命令)。修复任何提到的问题,然后重试。
  9. 有关更多信息:
  10. https://www.shellcheck.net/wiki/SC1108 -- 在等号前后需要有空格...
  11. https://www.shellcheck.net/wiki/SC1007 -- 如果尝试...
  12. https://www.shellcheck.net/wiki/SC1072 -- 期望比较运算符(不...
  13. </br>
  14. 更正后的代码:
  15. ```bash
  16. $ cat file
  17. APP_FOLDER=./service
  18. MODIFIED_APP_FOLDER=$([[ $APP_FOLDER == './'* ]] && echo "${APP_FOLDER/.\//"$PWD/"}" || echo "$PWD/$APP_FOLDER")
  19. echo "$MODIFIED_APP_FOLDER"
  20. $ bash file
  21. /tmp/service
英文:

Like this:

Always pass your script to https://shelcheck.net before asking human help. Or use the command line:

  1. $ shellcheck file
  2. In file line 2:
  3. MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")
  4. ^-- SC1007 (warning): Remove space after = if trying to assign a value (for empty string, use var='' ... ).
  5. ^-- SC1009 (info): The mentioned syntax error was in this command expansion.
  6. ^-- SC1073 (error): Couldn't parse this test expression. Fix to allow more checks.
  7. ^-- SC1108 (error): You need a space before and after the = .
  8. ^-- SC1072 (error): Expected comparison operator (don't wrap commands in []/[[]]). Fix any mentioned problems and try again.
  9. For more information:
  10. https://www.shellcheck.net/wiki/SC1108 -- You need a space before and after...
  11. https://www.shellcheck.net/wiki/SC1007 -- Remove space after = if trying to...
  12. https://www.shellcheck.net/wiki/SC1072 -- Expected comparison operator (don...

</br>

Corrected code:

  1. $ cat file
  2. APP_FOLDER=./service
  3. MODIFIED_APP_FOLDER=$([[ $APP_FOLDER == &#39;./&#39;* ]] &amp;&amp; echo &quot;${APP_FOLDER/.\//&quot;$PWD/&quot;}&quot; || echo &quot;$PWD/$APP_FOLDER&quot;)
  4. echo &quot;$MODIFIED_APP_FOLDER&quot;
  5. $ bash file
  6. /tmp/service

huangapple
  • 本文由 发表于 2023年2月23日 19:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定