Create command.sh and describe function
Open .bashrc and add source $ {path-to-command.sh} /command.sh
Details below
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
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
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).
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