Bash从路径变量和sed命令中删除路径 [英] Bash removing path from path variable and sed command
问题描述
使用此代码
从Bash中的$ PATH变量中删除路径的最优雅的方式是什么?
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'` | sed 's/:*$//'
在这种情况下,如果我运行: p>
In this instance if I run just the:
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'`
我可以得到包含 / SDE /
的路径;然而,:
仍然存在。假设我应该删除 sed
命令。当我立即运行这个整个命令什么都没有被删除。导致路径不更新的 sed
语句有什么问题,如何使 sed
命令删除 / SDE /
路径变量删除后,冒号:
I can get the path containing /SDE/
removed; however a :
remains. The sed
command after I am assuming should remove that. When I run this entire command at once nothing gets removed at all. What is wrong with the sed
statement that is causing the path not to update and how can I make the sed
command remove the colon :
after the /SDE/
path variable is removed.
推荐答案
问题是关闭回报的位置`
在命令中:
The problem is the placement of the closing back-quote `
in the command:
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'` | sed 's/:*$//'
如果您使用推荐的 $(...)
符号,你会发现这相当于:
If you used the recommended $(...)
notation, you'd see that this is equivalent to:
export PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}') | sed 's/:*$//'
哪些管道输出的操作到
sed
,但导出
是静默的。
which pipes the output of the export
operation to sed
, but export
is silent.
使用:
export PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}' | sed 's/:*$//')
我已经修复了错误命令逐字复制的答案。作为评论中的三重符号注释,我并不完全相信 awk
解决方案,但问题是代码有什么问题,答案是后置引号放在哪里。 awk
脚本确实在PATH中的任何位置处理删除PATH的元素; sed
脚本只是确保没有尾随:
,以便最终没有隐式使用当前目录的路径。
I have fixed the answer from which the erroneous command was copied verbatim. As tripleee notes in a comment, I'm not wholly convinced by the awk
solution, but the question was 'what was wrong with the code' and the answer is 'where the back-quotes are placed'. The awk
script does handle removing elements of a PATH at any position in the PATH; the sed
script simply ensures there is no trailing :
so that there is no implicit use of the current directory at the end of the PATH.
另请参见:如何在shell脚本中操作PATH元素和 clnpath
脚本在如何避免在中重复PATH变量csh
- 脚本是用于POSIX-ish贝壳像Bourne,Korn,Bash shell,尽管有问题的主题。 clnpath
与此处使用的符号之间的区别是 clnpath
仅删除完整的路径名;它不会尝试执行部分路径元素匹配:
See also: How do I manipulate PATH elements in shell scripts and the clnpath
script at How to keep from duplicating PATH variable in csh
— the script is for POSIX-ish shells like the Bourne, Korn, Bash shells, despite the question's subject. One difference between clnpath
and the notation used here is that clnpath
only removes full pathnames; it does not attempt to do partial path element matching:
export PATH=$(clnpath $PATH /opt/SDE/bin)
如果要删除的路径元素为 / opt / SDE / bin
。请注意, clnpath
可用于维护 LD_LIBRARY_PATH
, CDPATH
, MANPATH
和任何其他类似路径的变量;所以当然可以 awk
调用。
if the path element to be removed was /opt/SDE/bin
. Note that clnpath
can be used to maintain LD_LIBRARY_PATH
, CDPATH
, MANPATH
and any other path-like variable; so can the awk
invocation, of course.
我注意到, / code $ c>脚本中的/ SDE /
模式将删除 / opt / USDER / bin
;正则表达式中的斜杠与路径名中的斜杠无关。
I note in passing that that the /SDE/
pattern in the awk
script will remove /opt/USDER/bin
; the slashes in the regex have nothing to do with slashes in the pathname.
这篇关于Bash从路径变量和sed命令中删除路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!