如果尚不存在多个别名,请将其添加到 /etc/bash.bashrc 别名中

如果尚不存在多个别名,请将其添加到 /etc/bash.bashrc 别名中

这是我添加多个别名的方式/etc/bash.bashrc

cat <<-"BASHRC" >> /etc/bash.bashrc
    alias rss="/etc/init.d/php*-fpm restart && systemctl restart nginx.service"
    alias brc="nano /etc/bash.bashrc"
    alias www="cd /var/www/html"
    alias imb="bash /opt/imb.sh"
    alias nwsm="bash /opt/nwsm.sh"
BASHRC
source /etc/bash.bashrc

我觉得我可以传递此处的文档并使用其他方法来检查别名是否已经存在,有没有简单的解决方案?

答案1

grep在将该行添加到之前,您可以使用它来检查该行是否存在/etc/bash.bashrc

#!/bin/bash
cat > /tmp/aliases.tmp << 'BASHRC'
    alias rss='/etc/init.d/php*-fpm restart && systemctl restart nginx.service'
    alias brc='nano /etc/bash.bashrc'
    alias www='cd /var/www/html'
    alias imb='bash /opt/imb.sh'
    alias nwsm='bash /opt/nwsm.sh'
BASHRC
for i in `cat /tmp/aliases.tmp`; do
    [[ grep "$i" /etc/bash.bashrc ]] || echo "$i" >> /etc/bash.bashrc
rm /tmp/aliases.tmp
source /etc/bash.bashrc

相关内容