改进从命令行向 .bashrc 添加别名的功能
Improve function that adds aliases to .bashrc from command line
我编写了这个函数,用于将别名添加到 .bashrc 文件。
该功能运行良好但不完整,如果添加的别名已经存在,我想请用户确认并编写if条件以实现代码的"modified"部分,如果给出确认,就像安装新包一样。
add_alias(){
d_alias=
d_command=""
replacing=alias|grep "alias $d_alias"
if [[ "$replacing" -ne 0 ]];
then
sed -i "/alias $d_alias/d" $HOME/.bashrc
echo "alias $d_alias modified in ~/.bashrc"
else
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$d_alias"'='"'$d_command'"',2' $HOME/.bashrc
source ~/.bashrc
echo "alias $d_alias added to ~/.bashrc"
fi
}
#auto-generated code
alias brc='source ~/.bashrc'
alias client='/home/user/workspace/client'
alias workspace='/home/user/workspace'
我正在添加一个答案以供将来参考,归功于@4ae1e1 和@ArunSangal。他们的建议在评论中。
# TODO Room to improve - add_alias()
# Add new aliases to this .bashrc file
#-------------------------------------
add_alias(){
n_alias=
shift
n_command="$@"
replacing=$( alias $n_alias 2>/dev/null|wc -l )
if [[ "$replacing" = "1" ]];
then
b $n_alias # cat|grep .bashrc in color
read -p "Do you wish to overwrite this(these) alias(es)? [y/n]" yn
case $yn in
[Yy]* ) sed -i '/alias '"$n_alias"'/d' $HOME/.bashrc;
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$n_alias"'='"'$n_command'"',3' $HOME/.bashrc;
source ~/.bashrc;
echo "alias $n_alias modified in ~/.bashrc" ;;
[Nn]* ) echo "Operation canceled";;
* ) echo "Please answer yes or no.";;
esac
else
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$n_alias"'='"'$n_command'"',3' $HOME/.bashrc
source ~/.bashrc
echo "alias $n_alias added to ~/.bashrc"
fi
}
alias aa='add_alias'
# end of add_alias()
# auto-generated code
alias cdn='ssh cdn'
alias cdn2='ssh cdn2'
alias sxdev64="ssh sxdev64"
alias csi="ssh csi"
alias malt="ssh malt"
此函数同时为当前 shell 和 ~/.bash_aliases 添加别名。我之前总是手动测试别名
将其提交给 add-alias。它对我来说效果很好,我已经使用了很长时间。当然我没有运行生产系统。
YMMV.
add-alias ()
{
local name= value=""
echo alias $name=\'$value\' >> ~/.bash_aliases
eval alias $name=\'$value\'
alias $name
}
您可以添加铃声和口哨声。
我编写了这个函数,用于将别名添加到 .bashrc 文件。 该功能运行良好但不完整,如果添加的别名已经存在,我想请用户确认并编写if条件以实现代码的"modified"部分,如果给出确认,就像安装新包一样。
add_alias(){
d_alias=
d_command=""
replacing=alias|grep "alias $d_alias"
if [[ "$replacing" -ne 0 ]];
then
sed -i "/alias $d_alias/d" $HOME/.bashrc
echo "alias $d_alias modified in ~/.bashrc"
else
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$d_alias"'='"'$d_command'"',2' $HOME/.bashrc
source ~/.bashrc
echo "alias $d_alias added to ~/.bashrc"
fi
}
#auto-generated code
alias brc='source ~/.bashrc'
alias client='/home/user/workspace/client'
alias workspace='/home/user/workspace'
我正在添加一个答案以供将来参考,归功于@4ae1e1 和@ArunSangal。他们的建议在评论中。
# TODO Room to improve - add_alias()
# Add new aliases to this .bashrc file
#-------------------------------------
add_alias(){
n_alias=
shift
n_command="$@"
replacing=$( alias $n_alias 2>/dev/null|wc -l )
if [[ "$replacing" = "1" ]];
then
b $n_alias # cat|grep .bashrc in color
read -p "Do you wish to overwrite this(these) alias(es)? [y/n]" yn
case $yn in
[Yy]* ) sed -i '/alias '"$n_alias"'/d' $HOME/.bashrc;
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$n_alias"'='"'$n_command'"',3' $HOME/.bashrc;
source ~/.bashrc;
echo "alias $n_alias modified in ~/.bashrc" ;;
[Nn]* ) echo "Operation canceled";;
* ) echo "Please answer yes or no.";;
esac
else
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\nalias '"$n_alias"'='"'$n_command'"',3' $HOME/.bashrc
source ~/.bashrc
echo "alias $n_alias added to ~/.bashrc"
fi
}
alias aa='add_alias'
# end of add_alias()
# auto-generated code
alias cdn='ssh cdn'
alias cdn2='ssh cdn2'
alias sxdev64="ssh sxdev64"
alias csi="ssh csi"
alias malt="ssh malt"
此函数同时为当前 shell 和 ~/.bash_aliases 添加别名。我之前总是手动测试别名 将其提交给 add-alias。它对我来说效果很好,我已经使用了很长时间。当然我没有运行生产系统。
YMMV.
add-alias ()
{
local name= value=""
echo alias $name=\'$value\' >> ~/.bash_aliases
eval alias $name=\'$value\'
alias $name
}
您可以添加铃声和口哨声。