引入:在工作中,为了在开机前预置一些资源在手机系统中,可能有人会用很多方法去实现这个功能?不过,今天告诉大家一个用shell脚本去拷贝的方法,大家先看看以下代码,看是否能看懂 。
#!/bin/bash PRELOAD_DEST_PICTURES=/data/media/0/Pictures PRELOAD_FLAG_PICTURES=${PRELOAD_DEST_PICTURES}/.copyfiles PRELOAD_SOURCE_PICTURES=/vendor/sprd/sprd_media/Pictures if [ ! -f ${PRELOAD_FLAG_PICTURES} ] then echo " ${PRELOAD_SOURCE_PICTURES}/" cp -f ${PRELOAD_SOURCE_PICTURES}/Pic1.jpg ${PRELOAD_DEST_PICTURES} cp -f ${PRELOAD_SOURCE_PICTURES}/Pic2.jpg ${PRELOAD_DEST_PICTURES} cp -f ${PRELOAD_SOURCE_PICTURES}/Pic3.jpg ${PRELOAD_DEST_PICTURES} echo "finished" > ${PRELOAD_FLAG_PICTURES} echo " 执行结束 " fi
以上代码功能很简单,就是把/vendor/sprd/sprd_media/Pictures目录下的几张图片拷贝到/data/media/0/Pictures目录下,然后用一个 .copyfiles 文件是否已经存在去判断是否需要进行拷贝操作,拷贝命令是用Linux命令 。
接下来,我们简单介绍一下上面几行代码的脚本语法规则 :
shell程序的第一行一般都是: #!/bin/sh
/bin/sh指定shell程序被/bin目录下的sh可执行程序执行。
PRELOAD_DEST_PICTURES=/data/media/0/Pictures ,这是Shell变量赋值语句,Shell是弱类型语言,没有类型这个概念。变量定义时可以初始化,使用=进行初始化赋值。注意:shell对语法非常严格,= 两边不能有空格。变量名的命名须遵循如下规则:首个字符必须为字母(a-z,A-Z);中间不能有空格,可以使用下划线;不能使用标点符号;不能使用关键字。
以上代码还有个知识点就是流程控制:
if [....]
then….
fi
“if” 表达式 如果条件为真则执行then后面的部分 。
通常用” [ ] “来表示条件测试。
注意:这里的空格很重要。shell对语法非常严格,要确保方括号左右两边的空格不能少。
[ -f “somefile” ] :判断是否是一个文件
[ -x “/bin/ls” ] :判断/bin/ls是否存在并有可执行权限
[ -n “$var” ] :判断$var变量是否有值
[ “$a” = “$b” ] :判断$a和$b是否相等
上面的代码,就解释到这边 。看看自己对于这个脚本有没有产生兴趣继续学习,如果有,请继续往下看一个比较复杂的案例 。遇到不懂的自己百度学习,学习这个,应该难不倒聪明的你!
最近在安装阿里云服务器的时候,也看到代码中也有一个卸载阿里云linux一键安装web环境的uninstall.sh脚本。如果你有兴趣继续研究的,不妨把这里面的代码研究看看,我已经把全部的代码写出来了,有兴趣的阅读!
#!/bin/bash if [ "$1" != "in" ];then echo "Before cleaning the installation script environment !" echo "Please backup your data !!" read -p "Enter the y or Y to continue:" isY if [ "${isY}" != "y" ] && [ "${isY}" != "Y" ];then exit 1 fi fi mkdir -p /alidata if which mkfs.ext4 > /dev/null ;then if ls /dev/xvdb1 &> /dev/null;then if cat /etc/fstab|grep /alidata > /dev/null ;then if cat /etc/fstab|grep /alidata|grep ext3 > /dev/null ;then sed -i "/\/alidata/d" /etc/fstab fi else echo '/dev/xvdb1 /alidata ext4 defaults 0 0' >> /etc/fstab fi mount -a fi else if ls /dev/xvdb1 &> /dev/null;then if cat /etc/fstab|grep /alidata > /dev/null ;then echo "" else echo '/dev/xvdb1 /alidata ext3 defaults 0 0' >> /etc/fstab fi mount -a fi fi /etc/init.d/mysqld stop &> /dev/null /etc/init.d/nginx stop &> /dev/null /etc/init.d/php-fpm stop &> /dev/null /etc/init.d/vsftpd stop &> /dev/null /etc/init.d/httpd stop &> /dev/null /etc/init.d/tomcat7 stop &> /dev/null killall mysqld &> /dev/null killall nginx &> /dev/null killall httpd &> /dev/null killall apache2 &> /dev/null killall vsftpd &> /dev/null killall php-fpm &> /dev/null killall java &> /dev/null echo "--------> Clean up the installation environment" rm -rf /usr/local/freetype.2.1.10 rm -rf /usr/local/libpng.1.2.50 rm -rf /usr/local/freetype.2.1.10 rm -rf /usr/local/libpng.1.2.50 rm -rf /usr/local/jpeg.6 echo "" echo "--------> Delete directory" echo "/alidata/server/mysql delete ok!" rm -rf /alidata/server/mysql echo "rm -rf /alidata/server/mysql-* delete ok!" rm -rf /alidata/server/mysql-* echo "/alidata/server/tomcat7 delete ok!" rm -rf /alidata/server/tomcat7 echo "rm -rf /alidata/server/tomcat-* delete ok!" rm -rf /alidata/server/tomcat-* echo "/alidata/server/java delete ok!" rm -rf /alidata/server/java echo "rm -rf /alidata/server/java-* delete ok!" rm -rf /alidata/server/java-* echo "/alidata/server/php delete ok!" rm -rf /alidata/server/php echo "/alidata/server/php-* delete ok!" rm -rf /alidata/server/php-* echo "/alidata/server/nginx delete ok!" rm -rf /alidata/server/nginx echo "rm -rf /alidata/server/nginx-* delete ok!" rm -rf /alidata/server/nginx-* echo "/alidata/server/httpd delete ok!" rm -rf /alidata/server/httpd echo "/alidata/server/httpd-* delete ok!" rm -rf /alidata/server/httpd-* echo "" echo "/alidata/log/php delete ok!" rm -rf /alidata/log/php echo "/alidata/log/mysql delete ok!" rm -rf /alidata/log/mysql echo "/alidata/log/nginx delete ok!" rm -rf /alidata/log/nginx echo "/alidata/log/httpd delete ok!" rm -rf /alidata/log/httpd echo "" echo "/alidata/www/phpwind delete ok!" rm -rf /alidata/www/phpwind echo "" echo "--------> Delete file" echo "/etc/my.cnf delete ok!" rm -f /etc/my.cnf echo "/etc/init.d/mysqld delete ok!" rm -f /etc/init.d/mysqld echo "/etc/init.d/nginx delete ok!" rm -f /etc/init.d/nginx echo "/etc/init.d/php-fpm delete ok!" rm -r /etc/init.d/php-fpm echo "/etc/init.d/httpd delete ok!" rm -f /etc/init.d/httpd echo "/etc/init.d/tomcat7 delete ok!" rm -f /etc/init.d/tomcat7 echo "" ifrpm=$(cat /proc/version | grep -E "redhat|centos") ifdpkg=$(cat /proc/version | grep -Ei "ubuntu|debian") ifcentos=$(cat /proc/version | grep centos) echo "--------> Clean up files" echo "/etc/rc.local clean ok!" if [ "$ifrpm" != "" ];then if [ -L /etc/rc.local ];then echo "" else \cp /etc/rc.local /etc/rc.local.bak rm -rf /etc/rc.local ln -s /etc/rc.d/rc.local /etc/rc.local fi sed -i "/\/etc\/init\.d\/mysqld.*/d" /etc/rc.d/rc.local sed -i "/\/etc\/init\.d\/nginx.*/d" /etc/rc.d/rc.local sed -i "/\/etc\/init\.d\/php-fpm.*/d" /etc/rc.d/rc.local sed -i "/\/etc\/init\.d\/vsftpd.*/d" /etc/rc.d/rc.local sed -i "/\/etc\/init\.d\/httpd.*/d" /etc/rc.d/rc.local sed -i "/\/etc\/init\.d\/tomcat7.*/d" /etc/rc.d/rc.local sed -i "/JAVA_HOME=\/alidata\/server\/java/d" /etc/rc.d/rc.local else sed -i "/\/etc\/init\.d\/mysqld.*/d" /etc/rc.local sed -i "/\/etc\/init\.d\/nginx.*/d" /etc/rc.local sed -i "/\/etc\/init\.d\/php-fpm.*/d" /etc/rc.local sed -i "/\/etc\/init\.d\/vsftpd.*/d" /etc/rc.local sed -i "/\/etc\/init\.d\/httpd.*/d" /etc/rc.local sed -i "/\/etc\/init\.d\/tomcat7.*/d" /etc/rc.local sed -i "/JAVA_HOME=\/alidata\/server\/java/d" /etc/rc.local fi echo "" echo "/etc/profile clean ok!" sed -i "/export PATH=\$PATH\:\/alidata\/server\/mysql\/bin.*/d" /etc/profile sed -i "/export CLASSPATH=\.\:\$JAVA_HOME\/lib\/dt\.\jar.*/d" /etc/profile sed -i "/JAVA_HOME=\/alidata\/server\/java/d" /etc/profile sed -i "/JRE_HOME=\/alidata\/server\/java\/jre/d" /etc/profile source /etc/profile echo "" if [ "$ifrpm" != "" ];then yum -y remove vsftpd &> /dev/null cp -f ./ftp/config-ftp/rpm_ftp/* /etc/vsftpd/ rm -f /etc/vsftpd/chroot_list rm -f /etc/vsftpd/ftpusers rm -f /etc/vsftpd/user_list rm -f /etc/vsftpd/vsftpd.conf else apt-get -y remove vsftpd rm -f /etc/vsftpd.conf rm -f /etc/vsftpd.chroot_list rm -f /etc/vsftpd.user_list rm -rf /etc/pam.d/vsftpd fi echo "vsftpd remove ok!"
看完了吗?看到这边的人,说明你对这个Shell脚本很有兴趣,我上面讲的都是一些语法皮毛,我特意给大家准备了本关于Shell脚本学习指南电子书和一些学习视频,有助于你更进一步的深入学习,大家可以扫描下面的二维码,在公众号后台聊天窗口回复:Shell脚本,获得这本电子书!