在 Linux 环境中写了很多脚本,每一次都要手动去移动到 /bin 下,又要赋权限。有的时候我们会忘记自己曾经移动过什么,这时候怎么办呢? 不妨试一试这一个简单的小脚本程序 。
还是老规矩,先上代码,之后会对其中的几个部分做一下简单的解释。
#!/bin/bash
UPLOG_PATH="/home/zx/Desktop/tools/up.log"
DOWNLOG_PATH="/home/zx/Desktop/tools/down.log"
function echohelp(){
echo "-----------------------------------------------------------"
echo "- this is a pro used to upload or download the shell -"
echo "-----------------------------------------------------------"
echo "- -"
echo "- stay hungry! stay foolish! -"
echo "- -"
echo "-----------------------------------------------------------"
echo "- 1.upload 1 XXX.sh XXX -"
echo "- 将编写好的 shell 脚本上传到 bin 下 -"
echo "- 2.download 2 XXX -"
echo "- 将上传过的 shell 脚本删除 -"
echo "-----------------------------------------------------------"
}
function parseParams(){
for i in $*;do
if [ "$i" = "--help" ];then
echohelp
exit
else
echo "error input , please read '--help' carefully"
fi
done
}
function createlog(){
echo `date | cut -d ' ' -f 6,1-4` fromfile:$2 tofile:$3 >> $UPLOG_PATH
}
function deletelog(){
echo `date | cut -d ' ' -f 6,1-4` deletefile:$2 >> $DOWNLOG_PATH
}
function noipt(){
echo "please using '--help' to get info!"
}
if [ $# = 0 ];then
noipt
elif [ $1 = 1 ];then
if [ $# = 3 ];then
if [ -f $2 ];then
createlog $@
sudo ln -s $(pwd)/$2 /bin/$3
chmod 755 $2
echo "well done! the $3 is created~ you can check the log later"
else
echo "the file is not avaliable or is a directory! please check the filename"
fi
else
echo "error input , please read '--help' carefully"
fi
elif [ $1 = 2 ];then
if [ $# = 2 ];then
if [ -L /bin/$2 ];then
deletelog $@
sudo rm -rf /bin/$2
echo "well done! the $2 is deleted~ you can check the log later"
else
echo "the file is not avaliable or is a directory! please check the filename"
fi
else
echo "error input , please read '--help' carefully"
fi
else
parseParams $*
fi
使用方法:输入参数 --help 时,就会提示使用时应该输入的参数类型。为了方便我们记录曾经生成过什么,本脚本会生成两个日志文件(up.log 和 down.log,这个很活,大家想怎么重命名都可以)。大家需要更改最上面的这两个路径,需要写绝对路径哦
UPLOG_PATH="/home/zx/Desktop/tools/up.log"
DOWNLOG_PATH="/home/zx/Desktop/tools/down.log"
注意:这两个 log 文件一定要自己建好。名字随意,不过必须将两个 log 的绝对地址写对了。否则自动生成的文件会是 root 用户下的,会影响到日志记录功能。
这个小脚本有两个功能,一个功能是根据你写好的 shell 生成 /bin 下的可执行文件,以便我们在任意地方使用编辑好的 shell。还有一个功能则是删除上传的 /bin 下的可执行文件,前提是本脚本生成的,或者是其他软链接文件。
该脚本利用了软链接,如果有疑问,可以看我的另一篇文章 关于 Linux 环境中软链接和硬链接的理解
因为更改了根目录下的 /bin 文件,所以,会询问该 Linux 系统的 root 密码,来执行 sudo 指令。(输入一次就可以啦,重启后重新输入)
如果对本脚本有什么疑问,可以在底下的评论区问我,我会详细的解释的。
stay hungry! stay foolish!