Wednesday, 24 October 2012

Linux for you Admin Tips:6

Linux for you Admin Tips:1
Linux for you Admin Tips:2
Linux for you Admin Tips:3
Linux for you Admin Tips:4
Linux for you Admin Tips:5

Is there a simple way to list all the command conflicts that have occurred in the system due to the bashrc update involving alias commands?

For example, someone writes alias ls=/path/to/user-generated/executable in bashrc. How does one find out that this is masking an actual command (ls). One way seems to be to run all the aliases before and after sourcing bashrc and diff the output. Are there any better ways?

bash --version
GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)
1. To find out what commands are masked by aliases, do something like this:
alias | sed 's/^[^ ]* *\|=.*$//g' | while read a; do
  printf "%20.20s : %s\n" $a "$(type -ta $a | tr '\n' ' ')"
done | awk -F: '$2 ~ /file/'


alias alone lists defined aliases and sed extracts their name. The while loop runs type -ta on each of them and awk prints the lines that both contain alias and file.
sankar@new-host ~]$ alias
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mc='. /usr/share/mc/bin/'
alias scpresume='rsync -av --partial --progress --rsh=ssh'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2. You can use type to find out how a command would be interpreted by bash.
sankar@new-host ~]$
[sankar@new-host ~]$ type ls
ls is aliased to `ls --color=tty'
[sankar@new-host ~]$ type vi
vi is aliased to `vim'
[sankar@new-host ~]$
3.compgen -a | sort is the list of all aliases (sorted for comm).             compgen -ac | sort | uniq -dis the list of all duplicates from the list of commands and aliases. comm -12 outputs only those lines that are common to both.
sankar@new-host ~]$  comm -12 <(compgen -a | sort) <(compgen -ac | sort | uniq -d)
++ compgen -a
++ sort
++ compgen -ac
+ comm -12 /dev/fd/63 /dev/fd/62
++ sort
++ uniq -d
++ echo -ne '\033]0;sankar@new-host:~'

[sankar@new-host ~]$

4. cat /home/jordan/.bashrc
[sankar@new-host ~]$ cat .bashrc
+ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc

# User specific aliases and functions
++ echo -ne '\033]0;sankar@new-host:~'

[sankar@new-host ~]$ pwd
+ pwd

No comments:

Post a Comment