bash 使用 sed 将别名添加到 .bashrc 文件中别名列表的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5573683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:45:14  来源:igfitidea点击:

Adding alias to end of alias list in .bashrc file using sed

bashshell

提问by Danny

I'm writing a shell script that I want to add an aliasto the end of the aliaslist in the .bashrcfile. I'm thinking something with sedwould work, just not sure how to find the last line that begins with alias, then add on the next line another alias.

我正在编写一个 shell 脚本,我想将它添加alias到文件alias列表的.bashrc末尾。我在想一些 withsed会起作用,只是不知道如何找到以 开头的最后一行alias,然后在下一行添加另一个alias.

回答by Andrea Spadaccini

Why do you need to add it to an "alias list"? If you don't have additional requirements that you did not specify in the question, just append your alias to .bashrc:

为什么需要将其添加到“别名列表”中?如果您没有在问题中未指定的其他要求,只需将您的别名附加到.bashrc

echo "alias youralias='yourcmd'" >> /home/user/.bashrc

回答by glenn Hymanman

When I hear "do something after the last whatever", I think of reversing the file and do something when I see the first whatever:

当我听到“在最后一个之后做某事”时,我想到了反转文件并在我看到第一个之后做某事:

tac .bashrc | 
awk -v newalias="alias new=foo" ' == "alias" {print newalias} 1' | 
tac > .bashrc.new
mv .bashrc .bashrc.$(date +%Y%m%d%H%M%S) && mv .bashrc.new .bashrc

回答by Mikel

IMHO this is much easier/cleaner to do in Perl, Python, etc.

恕我直言,这在 Perl、Python 等中更容易/更清晰。

But if you must use sed, here's a starting point:

但如果你必须使用 sed,这里有一个起点:

$ sed -ne ':START
/alias/b ALIASES
p
b
:ALIASES
p
n
/alias/b ALIASES
i \
alias foo=bar
:REST
p
n
b REST
' < aliases > aliases.new
$ diff -u aliases aliases.new
--- aliases     2011-04-07 08:30:30.000000000 +1000
+++ aliases.new 2011-04-07 08:34:09.000000000 +1000
@@ -3,6 +3,7 @@

 alias a=apple
 alias b=banana
+alias foo=bar

 echo something else
$ mv aliases.new aliases

A fuller version that works for me is

对我有用的完整版本是

$ name=b
$ replacement=barney
sed -i.bak -n -e '
:START
/^[[:space:]]*alias/ b NEXTALIAS
# not an alias, print it as-is and go to next line
p
b
:NEXTALIAS
# start processing this alias line
/^[[:space:]]*alias[[:space:]][[:space:]]*'"$name"'/ b REPLACE
/^[[:space:]]*alias/ b PRINT

# found the end of the alias block, insert the alias here
:INSERT
# grab the indentation back from the hold space
x
s/$/alias '"$name='$replacement'"'/
p
x
b REST

:PRINT
# remember how the last alias line was indented...
h
s/^\([[:space:]]*\).*//
x
# ... and print the line
p
n
b NEXTALIAS

:REPLACE
# we found an existing alias with a matching name, replace it
# I add single quotes around replacement so that the caller can write
# replacement='echo something' rather than replacement="'echo something'"
s/\(.*\)alias[[:space:]][[:space:]]*'"$name"'.*/alias '"$name='$replacement'"/'
b REST

:REST
# we made it past the aliases block, just print the remaining lines
p
n
b REST
' aliases

$ diff -u aliases.bak aliases
--- aliases.bak 2011-04-07 09:09:26.000000000 +1000
+++ aliases     2011-04-07 09:11:05.000000000 +1000
@@ -2,7 +2,7 @@
 echo blah

 alias a=apple
-alias b=banana
+alias b='barney'
 alias c=carrot

 echo something else

Note that there are some edge cases I have not handled explicitly. For example, what should happen if there are two blocks of aliases? What if there is a commented out alias in the middle of the alias block, etc.

请注意,有些边缘情况我没有明确处理。例如,如果有两个别名块会发生什么?如果别名块中间有注释掉的别名会怎样,等等。

回答by kurumi

awkis the tool to use if you can't use better scripting language. There's no need to use sed`

awk如果您不能使用更好的脚本语言,则可以使用该工具。没有必要使用sed`

awk 'FNR==NR&&/alias/{s=FNR;next}FNR==s{ ##代码##=##代码##"\nalias=new alias\n"}NR>FNR' .bashrc .bashrc > temp && mv temp .bashrc