巴什去除路径变量和sed命令路径 [英] Bash removing path from path variable and sed command
问题描述
本$ C $工作c
<一个href=\"http://stackoverflow.com/questions/370047/what-is-the-most-elegant-way-to-remove-a-path-from-the-path-variable-in-bash\">What是最优雅的方式来删除从猛砸$ PATH变量的路径?
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'` | sed 's/:*$//'
在这种情况下,如果我只运行:
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/:*$//')
我有固定的回答从错误的命令被逐字复制。正如评论 tripleee 的笔记,我不是完全由 AWK $ C相信$ C>的解决方案,但问题是什么是错的code'答案是',其中反引号都放在'。在
AWK
脚本不会处理在路径中的任何位置删除路径的元素;在 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.
参见:<一href=\"http://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts\">How我操纵shell脚本路径元素并在<一的 clnpath
脚本href=\"http://stackoverflow.com/questions/135754/how-to-keep-from-duplicating-path-variable-in-csh\">How从在 CSH
复制PATH变量保持 - 剧本是POSIX杂交炮弹像伯恩,科恩,击炮弹,尽管问题的主题。这里使用的标记 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 /斌
。需要注意的是 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.
我顺便指出,在 AWK
脚本 / SDE /
模式将删除的/ opt / USDER /斌
;在正则表达式的斜线都无关的路径斜杠。
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.
这篇关于巴什去除路径变量和sed命令路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!