One minute to teach you to write global Linux built-in command

Introduction: In linux command Some commands are always long and difficult to remember, even if it is frequently used commands every knock is also really annoying, so teach us today a way to simplify the command to create our own internal built command! ! !

Create a built-in command

Create a command storage directory

Now create linux on a folder to save our own command

[root@localhost /]# cd /
[root@localhost /]# mkdir scprict
[root@localhost /]# cd scprict

Write command

[root@localhost scprict]# vi hello

After entering the document compiled mode, press istart writing command, as follows

#!/bin/sh 
echo hello

Press ESCthe, then Shift+:, when the bottom of the screen appears :, enter wqhit Enter to save so that the command has been created. Use llthe command View
Here Insert Picture Description

Authorization command

Note that, when the command can not be executed directly, or will prompt you to access is not enough, we need to be authorized command

[root@localhost scprict]# chmod 777 hello

After authorization, we take a look at, and execute commands
Here Insert Picture Description
you can see, the command is authorized to green, and after performing also outputhello

Shell built-in command command change

Although we currently have finished creating the Shell command, but this time there are two problems:

  • hello command need to add in front ./before being executed
  • Use of this command is currently limited to the current folder
    so at this time the command is just a hello Shell command, we need to take it into a built-in command, as follows
[root@localhost scprict]# cp hello /bin

You're not wrong, we just need to put this command to copy the bin directory on it, and then we try to
Here Insert Picture Description
see through can now be executed directly either in the directory in which hellocommands it, and when we enter the part of the command then press the TABkey, the system will automatically complete the command prompt is not very Nice ~.
So far, we've had to create a built-in command of the operation, Here are some samples of my writing to docker command built-in command.

Docker command examples

Example 1: In docker ps -aorder Acronym dp command
Here Insert Picture Description
command:

#!/bin/sh
docker ps -a

Example 2: In docker logsorder for the abbreviated ds command
Here Insert Picture Description
command:

#!/bin/sh
docker logs -f "$@"

Example 3: docker execCommand of de command
Here Insert Picture Description
command:

#!/bin/sh
docker exec -it "$@" sh

to sum up

So far, we have learned how to create a global Linux built-in command, let's look at the steps of:

 1. 创建命令存储目录
 2. 编写命令
 3. 授权命令
 4. 移入bin目录下

In Linux, there are many orders we often have very long but harder to remember, we can look at in this way to simplify the use of commonly used commands, but need 注意a little, that is 创建的命令不要和/bin目录下的原有命令重名!.

Finally, if you have a better command abbreviation, welcome to leave a comment below to communicate and discuss with our. thanks for watching!

Guess you like

Origin www.cnblogs.com/Survivalist/p/11297938.html