linux: create original Terminal command

Conclusion only

Create command.sh and describe function Open .bashrc and add source $ {path-to-command.sh} /command.sh Details below

1 Basic

terminal


#open terminal
#Enter below(shift +Line break with Enter)
user@pop-os:~$ function test() {
> echo $1
> }

#Call the defined function
user@pop-os:~$ test "hello bash script"
hello bash script

Easy. But once you close the terminal, you can't use it anymore

2 Save the function to a file

terminal


# rotate.make sh
user@pop-os:~$ mkdir commands
user@pop-os:~$ vim commands/rotate.sh

.sh:rotate.sh


#!/bin/bash
# rotate screen
function rotate() {
  case $1 in
    # left
    [lL])
        xinput set-prop 13 'Coordinate Transformation Matrix' 0 -1 0 1 0 0 0 0 1
        xrandr -o left
	;;
    # right
    [rR])
	xinput set-prop 13 'Coordinate Transformation Matrix' 0 1 0 -1 0 0 0 0 1
	xrandr -o right
	;;
    # upside down
    [bBdD])
	xinput set-prop 13 'Coordinate Transformation Matrix' -1 0 0 0 -1 0 0 0 1
	xrandr -o inverted 
	;;

    # normal
    *)
        xinput set-prop 13 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
        xrandr -o normal
	;;
  esac
}

\ # Is a comment. $ 1 is the first argument. case is a switch statement and is executed only when the argument value matches the following regular expression. [lL] is a regular expression, either lowercase l or uppercase L. ) Indicates that it is one case of the case statement. ;; is break. esac is the end tag of the case. xinput set-prop and xrandr rotate the screen and rotate the mouse input direction. For details, see another article.

Of course, you can't use the function with this alone, so you need to load the function into bash.

terminal


user@pop-os:~$ rotate
rotate: command not found

3 Have bash load the created function

terminal


#Add function to bash
user@pop-os:~$ source ~/commands/rotate.sh
#Execute function
user@pop-os:~$ rotate l

Easy. When you close the terminal, you have to run source again. Therefore, it is necessary to write a process to call a function in .bashrc (bash run control).

4 Add the created function to .bashrc (bash run control)

terminal


user@pop-os:~ sudo vim ~/.bashrc 

Add the following to the bottom line

.shell:.bashrc


#Read the original command
source ~/command/*.sh

With this, future eternity, commands can be used

terminal


user@pop-os:~ rotate r

Recommended Posts

linux: create original Terminal command
Linux command # 4
Linux command # 3
Command to create Linux Live USB
linux at command
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
Try to create a new command on linux
How to create a shortcut command for LINUX
[Linux] Volume configuration command
Linux command (sequential update)
Linux basic command memorandum
Linux command [File operation]
[Linux] Basic command summary
Linux command for self-collection
linux command error collection 1
Linux command line shortcut
linux sar command CPU usage
[Linux] tar.gz compression / decompression command
What is Linux? [Command list]
Create command shortcuts on Ubuntu 16.04
Easy df command on Linux
Linux Command Dictionary (for myself)
[Note] Useful linux command collection
Linux command memorandum [for beginners]
Linux PC spec check command
[Linux] User / group command summary
[C language] [Linux] Try to create a simple Linux command * Just add! !!
[Linux convenient command] Try inserting exa
[Linux] OS recovery with restore command
Completion of docker command on Linux
[Linux convenient command] Try inserting csview
Permission and ownership change command [Linux]
Create Scratch Offline Editor for Linux
LINUX command [wc edition] Usage example
Linux command [ldconfig] LPIC learning memo
[linux] kill command to kill the process
Create a Linux environment on Windows 10
[Linux] [kernel module] Create kthread in kernel module
[Linux convenient command] Try inserting bat
Create Splunk custom search command Part 2
Linux command 16 procedure manual folder (completed)
Linux command (basic in basic) personal memo