为脚本创建软连接到指定的PATH中,变为自定义命令程序

1.编写鸡兔同笼脚本

[root@centos7:~]#
vim chook_rabbit.sh 

#!/bin/bash
#****************************************************
#Date:         2022-08-22
#Author:       wwzroom
#FileName:     chook_rabbit.sh
#Description:  The test script
#QQ:           896517050
#****************************************************
HEAD=$1
FOOT=$2

RABBIT=$[(FOOT-HEAD-HEAD)/2]
CHOOK=$[HEAD-RABBIT]
echo RABBIT:$RABBIT
echo CHOOK:$CHOOK                       

2.添加执行权限

[root@centos7:~]#
chmod +x chook_rabbit.sh 

3.创建软连接到指定PATH环境变量目录下


[root@centos7:~]#
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7:~]#
ln -s ~/chook_rabbit.sh /usr/local/sbin/C-R    创建软连接PATH环境变量的路径,任意一个目录都可以。
#注意:最好是要是绝对路径,因为相对路径容易出问题,找不到真实文件路径,进而创建完成之后就会飘红且闪烁。

方法二:在/etc/profile.d/目录下设置把root目录添加到环境变量脚本并执行,这样root目录下的可执行脚本都可以直接执行。
[root@centos7:~]#
echo "export PATH=/root:$PATH" > /etc/profile.d/chook_rabbit.sh

2.生效[root@centos7:~]#
source /etc/profile.d/chook_rabbit.sh
. /etc/profile.d/chook_rabbit.sh  #也可以这样生效

4.脚本程序执行

1.根据自己创建的软连接进行执行
[root@centos7:~]#
C-R  20 50
RABBIT:5
CHOOK:15

2.添加root目录为环境变量后,可以直接执行
[root@centos7:~]#
chook_rabbit.sh 20 50
RABBIT:5
CHOOK:15

5.执行脚本你的方式(小总结)

1.[root@centos7:~]#
bash systeminfo.sh
-----------------------Host systeminfo------------------------------
HOSTNAME:           centos7.9.wwzroom.org
IPADDR:             172.31.4.10
OSVERSION:          CentOS Linux release 7.9.2009 (Core)
KERNEL:             3.10.0-1160.el7.x86_64
CPU:		     12th Gen Intel(R) Core(TM) i5-12600KF
MEMORY:             1.9G
DISK:		     200G
--------------------------------------------------------------------

2.[root@centos7:~]#
. systeminfo.sh
-----------------------Host systeminfo------------------------------
HOSTNAME:           centos7.9.wwzroom.org
IPADDR:             172.31.4.10
OSVERSION:          CentOS Linux release 7.9.2009 (Core)
KERNEL:             3.10.0-1160.el7.x86_64
CPU:		     12th Gen Intel(R) Core(TM) i5-12600KF
MEMORY:             1.9G
DISK:		     200G
--------------------------------------------------------------------

3.[root@centos7:~]#
source systeminfo.sh
-----------------------Host systeminfo------------------------------
HOSTNAME:           centos7.9.wwzroom.org
IPADDR:             172.31.4.10
OSVERSION:          CentOS Linux release 7.9.2009 (Core)
KERNEL:             3.10.0-1160.el7.x86_64
CPU:		     12th Gen Intel(R) Core(TM) i5-12600KF
MEMORY:             1.9G
DISK:		     200G
--------------------------------------------------------------------

4.需要执行权限才行
[root@centos7:~]#
chmod +x systeminfo.sh 
[root@centos7:~]#
./systeminfo.sh 
-----------------------Host systeminfo------------------------------
HOSTNAME:           centos7.9.wwzroom.org
IPADDR:             172.31.4.10
OSVERSION:          CentOS Linux release 7.9.2009 (Core)
KERNEL:             3.10.0-1160.el7.x86_64
CPU:		     12th Gen Intel(R) Core(TM) i5-12600KF
MEMORY:             1.9G
DISK:		     200G
--------------------------------------------------------------------

为脚本创建软连接到指定的PATH中,变为自定义命令程序_第1张图片

你可能感兴趣的:(sre,linux,bash,centos)