My new understanding of alias: Using alias to make rm safer, aliasrm

Source: Internet
Author: User

My new understanding of alias: Using alias to make rm safer, aliasrm

Rm's tragedy always happens inadvertently. Therefore, whether in shell scripts or interactive bash environments, you should always think twice before executing rm commands. As a result, many people try their best to prevent accidental deletion of files, and there are various methods.

1.1 General Usage of alias

By default, rm is the alias of "rm-I", and ll is the alias of "ls-l. You can customize aliases to replace some commands with some options, or you can define aliases to combine multiple commands. For example:

[root@xuexi ~]# alias ls='ls -lA'

In this way, hidden files will be listed at the same time when the directory is listed.

Using alias without parameters will list all the defined aliases in the Current shell environment.

In addition, when the alias and command have the same name, the alias will be preferentially executed (otherwise, the alias will be meaningless). This can be seen from the which results:

[root@xuexi ~]# which mvalias mv='mv -i'        /bin/mv

If the name defined is the same as the name of the original Command (for example, the alias ls = 'LS-l'), If You Want To explicitly use the original command, you can delete aliases, use absolute paths, or use escape characters to restore commands.

The alias command is a temporary alias definition. to define an alias that takes effect for a long time, write the alias Definition Statement to/etc/profile or ~ /. Bash_profile or ~ /. Bashrc. The first one is valid for all users, and the last two are valid for the corresponding users. After modification, remember to use source to retrieve these configuration files again.

You can use unalias to temporarily cancel an alias.

1.2 alias Defects

Alias is a bit vague in definition and usage. The alias command below is used as an example to describe why it is incorrect in some shell scripts.

alias rmm='cp $@ ~/backup;rm $@'

This alias is used to back up a file to a directory before deleting it.According to man bash, the alias rmm is only the alias of the first cp command. The rm after the semicolon is not part of the alias, but is followed by the next command after the alias. When executing the alias rmm, read the alias to the position of the semicolon, then perform alias extension. After executing the alias command, execute the rm command after the semicolon.

The above command is an incorrect command. The problem lies in the cp parameter "$ @", which indicates all the parameters provided, however, since the cp command is separated by semicolons and another command is defined, when the alias command is executed, the parameter cannot be passed to the cp command, but can only be passed to the last command rm, that is to say, "$ @" after cp is a null value. Therefore, this alias is equivalent:

alias rmm='cp ~/backup;rm $@'

If so, use echo to test it.

[root@xuexi ~]# alias rmm='echo cp $@ ~/backup;echo rm $@'
[root@xuexi ~]# rmm /etc/fstab /etc/hostscp /root/backuprm /etc/fstab /etc/hosts

From the above results, we can see that "$ @" after cp is not expanded at all, but is null.

What if the alias definition statement does not use semicolons or other methods to define additional commands, but only one command? Can aliases work correctly? None. The following is an example:

[root@xuexi ~]# alias rmm='echo mv -f $@ ~/backup'[root@xuexi ~]# rmm /etc/fstab /etc/hostsmv -f /root/backup /etc/fstab /etc/hosts

Have you found any problems? "$ @" Is extended in "~ After the/backup "directory, that is, the alias of the following mv cannot work normally if you want to replace rm:

alias rm='mv -f $@ ~/backup'

The reason why I cannot work properly is that ~ /Backup is also part of "$ @" and is the first parameter in "$. Run the following command:

[root@xuexi ~]# echo mv -f "$@" ~/backup /etc/fstab /etc/hostsmv -f /root/backup /etc/fstab /etc/hosts

From the above analysis, we can know that alias has its own shortcomings. It is only suitable for simple command and parameter replacement and completion. It is difficult to implement complicated command substitution. Therefore, in man bash, we recommend that you use functions to replace aliases (For almost every purpose, aliases are superseded by shell functions ).

1.3 Best Implementation of aliases

There is no doubt that writing a shell script is much safer and more complete than an alias. This is a way to replace an alias. My personal suggestion is to use functions in alias definition statements to overcome alias defects.

For example, to ensure secure execution of rm, use the following two methods to define aliases:

alias rm='copy1(){ /bin/cp -a $@ ~/backup;rm $@; };copy1 $@'alias rm='move1(){ /bin/mv -f $@ ~/backup; };move1 $@'

Because the parameter used to execute the alias can only be passed to the last command, namely, the copy1 or move1 function, but the parameter represented by "$ @" can be passed to the function, make "$ @" in the function get the correct extension, so the entire alias can be reasonably and correctly executed.

Or define a shell function to replace rm. For example, write to the/etc/profile. d/rm. sh file:

function rm(){ [ -d ~/rmbackup ] || mkdir ~/rmbackup;/bin/mv -f $@ ~/backup; }
chmod +x /etc/profile.d/rm.sh

In this way, when you execute the rm command, the rm function defined here will be executed to make rm safer. Note that such functions (and aliases) cannot be directly used in scripts.

 

Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprinted please indicate the source: Success!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.