英文:
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?
APP_FOLDER=./service
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?
APP_FOLDER=./service
MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")
Causes "Wrong substitution"
答案1
得分: 2
不需要在Bash中使用子shell扩展:
#!/usr/bin/env bash
printf '%-10s %s\n' 'app_folder' 'modified_app_folder'
for app_folder in ./service foobar ./ /tmp/test; do
modified_app_folder=${app_folder/#.\//$PWD/}
printf '%-10s %s\n' "$app_folder" "$modified_app_folder"
done
示例输出:
app_folder modified_app_folder
./service /home/lea/StackOverflow/service
foobar foobar
./ /home/lea/StackOverflow/
/tmp/test /tmp/test
英文:
It is not needed to use sub-shell expansion here with Bash:
#!/usr/bin/env bash
printf '%-10s %s\n' 'app_folder' 'modified_app_folder'
for app_folder in ./service foobar ./ /tmp/test; do
modified_app_folder=${app_folder/#.\//$PWD/}
printf '%-10s %s\n' "$app_folder" "$modified_app_folder"
done
Sample output:
app_folder modified_app_folder
./service /home/lea/StackOverflow/service
foobar foobar
./ /home/lea/StackOverflow/
/tmp/test /tmp/test
答案2
得分: 1
这样:
始终在寻求人类帮助之前,通过 https://shelcheck.net 检查你的脚本。或者使用命令行:
$ shellcheck file
在文件的第2行:
MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")
^-- SC1007 (警告): 如果尝试分配一个值,请删除等号后的空格(对于空字符串,请使用 var='' ... )。
^-- SC1009 (信息): 上述语法错误在此命令扩展中。
^-- SC1073 (错误): 无法解析此测试表达式。修复以允许更多检查。
^-- SC1108 (错误): 等号前后需要有空格。
^-- SC1072 (错误): 期望比较运算符(不要在[]/ [[]]中包装命令)。修复任何提到的问题,然后重试。
有关更多信息:
https://www.shellcheck.net/wiki/SC1108 -- 在等号前后需要有空格...
https://www.shellcheck.net/wiki/SC1007 -- 如果尝试...
https://www.shellcheck.net/wiki/SC1072 -- 期望比较运算符(不...
</br>
更正后的代码:
```bash
$ cat file
APP_FOLDER=./service
MODIFIED_APP_FOLDER=$([[ $APP_FOLDER == './'* ]] && echo "${APP_FOLDER/.\//"$PWD/"}" || echo "$PWD/$APP_FOLDER")
echo "$MODIFIED_APP_FOLDER"
$ bash file
/tmp/service
英文:
Like this:
Always pass your script to https://shelcheck.net before asking human help. Or use the command line:
$ shellcheck file
In file line 2:
MODIFIED_APP_FOLDER= = $([[ $APP_FOLDER= './'* ]] && echo "${APP_FOLDER/'./'/"$PWD"}" || echo "$PWD/$APP_FOLDER")
^-- SC1007 (warning): Remove space after = if trying to assign a value (for empty string, use var='' ... ).
^-- SC1009 (info): The mentioned syntax error was in this command expansion.
^-- SC1073 (error): Couldn't parse this test expression. Fix to allow more checks.
^-- SC1108 (error): You need a space before and after the = .
^-- SC1072 (error): Expected comparison operator (don't wrap commands in []/[[]]). Fix any mentioned problems and try again.
For more information:
https://www.shellcheck.net/wiki/SC1108 -- You need a space before and after...
https://www.shellcheck.net/wiki/SC1007 -- Remove space after = if trying to...
https://www.shellcheck.net/wiki/SC1072 -- Expected comparison operator (don...
</br>
Corrected code:
$ cat file
APP_FOLDER=./service
MODIFIED_APP_FOLDER=$([[ $APP_FOLDER == './'* ]] && echo "${APP_FOLDER/.\//"$PWD/"}" || echo "$PWD/$APP_FOLDER")
echo "$MODIFIED_APP_FOLDER"
$ bash file
/tmp/service
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论