shell
一、shell概述
shell是操作系统的最外层,可以合并编程语言一控制进程和文件,以及启动和控制其他程序。shell通过提示输入,向操作系统解释该输入。shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核。
Shell还是一个功能相当强大的编程语言,易编写、易调试、灵活性强。
Linux提供的shell解析器
root@zhangkun:~# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/dash
/usr/bin/dash
root@zhangkun:~# echo $SHELL
/bin/bash
二、第一个shell脚本
先新建一个sh脚本文件
root@zhangkun:~# mkdir study
root@zhangkun:~/study# touch hello.sh
root@zhangkun:~/study# vi hello.sh
在helloworld.sh中输入如下内容
#!/bin/bash
echo "Hello,world"
脚本常用的执行方式
Linux shell种类非常多,常见的有:Bourne Shell(/usr/bin/sh或/bin/sh)、Bourne Again Shell(/bin/bash)、C Shell(/usr/bin/csh)、K shell(/usr/bin/ksh)、Shell for Root(/sbin/sh)等。不同的Shell语言的语法有所不同,所以不能交换使用。
最常用的shell是Bash,也就是Bourne Again Shell,是大多是Linux系统默认的Shell。
1、第一种采用bash或sh+脚本的相对路径或者绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径
root@zhangkun:~/study# sh hello.sh
Hello,world
sh+脚本的绝对路径
root@zhangkun:~/study# sh /root/study/hello.sh
Hello,world
bash+脚本的相对路径
root@ zhangkun:~/study# bash hello.sh
Hello,world
bash+脚本的绝对路径
root@ zhangkun:~/study# bash /root/study/hello.sh
Hello,world
2、第二种:采用输入脚本的绝对路径和相对路径执行脚本(必须具有可执行权限+x)
1、首先赋予helloworld.sh脚本+x权限
root@zhangkun:~/study# chmod +x hello.sh
2、./+脚本名
root@zhangkun:~/study# ./hello.sh
Hello,world
3、/+绝对路径
root@ zhangkun:~/study# /root/study/hello.sh
Hello,world
第三种:在脚本的路径前加上”. ”或者source命令
root@zhangkun:~/study# source hello.sh
Hello,world
root@zhangkun:~/study# source /root/study/hello.sh
Hello,world
root@zhangkun:~/study# . hello.sh
Hello,world
source是shell的内嵌
root@zhangkun:~/study# type source
source is a shell builtin
前两种都是在当前shell中打开一个子shell来执行脚本内容,当脚本内容结束,则子shell关闭,回到父shell中。
第三种,也就是使用在脚本路径前加”.”或者source的方式,可以使脚本内容在当前shell里执行,而无需打开子shell,也就是为什么我们每次修改完/etc/profile文件以后,需要source一下的原因。
三、父子shell
查看bash进程
root@zhangkun:~/study# ps -f
UID PID PPID C STIME TTY TIME CMD
root 1623611 1623417 0 Jul16 pts/4 00:00:00 -bash
root 2973460 1623611 0 16:18 pts/4 00:00:00 ps -f
在敲一下bash,生成一个bash的子进程
root@ zhangkun:~/study# bash
root@ zhangkun:~/study# ps -f
UID PID PPID C STIME TTY TIME CMD
root 1623611 1623417 0 Jul16 pts/4 00:00:00 -bash
root 2975205 1623611 0 16:19 pts/4 00:00:00 bash
root 2975216 2975205 0 16:19 pts/4 00:00:00 ps -f
后面进行的操作是在子shell中进行。
然后执行exit退出子shell
root@zhangkun:~/study# exit
exit
root@zhangkun:~/study# ps -f
UID PID PPID C STIME TTY TIME CMD
root 1623611 1623417 0 Jul16 pts/4 00:00:00 -bash
root 2979056 1623611 0 16:21 pts/4 00:00:00 ps -f
开子shell和不开子shell的区别在于,环境变量的继承关系,如在子shell中设置的当前变量,父shell中是不可见的。
四、变量
shell编程语言是非典型的解释型语言,不像C++/Java语言编程时需要事先声明变量,SHELL给一个变量赋值,实际上就是定义了变量,在Linux支持的所有shell中,都可以使用赋值符号(=)为变量赋值。
SHELL变量可分为两类:局部变量和环境变量,局部变量只在创建它们的shell脚本中使用,而环境变量则可以再创建它们的shell以及其派生出来的任意子进程中使用,有些变量使用户创建的,其他的则是专用shell变量。
4.1 系统预定义变量
(1)常用系统变量
$HOME、$PWD、$SHELL、$USER等。
$0 当前程序的名称
$n 当前程序的第n个参数,n=1,2,....9
$* 当前程序的所有参数(不包括程序本身)
$? 命令或程序执行完后的状态,一般返回0表示执行成功。
$UID 当前用户的ID
$PWD 当前所在的目录
$#:表示执行脚本传入参数的个数
(2) 案例实操
查看系统变量的值
root@zhangkun:~/study# env
root@zhangkun:~/study# echo $HOME
/root
root@zhangkun:~/study# echo $PWD
/root/study
root@zhangkun:~/study# printenv
root@zhangkun:~/study# echo $USER
root
root@zhangkun:~/study# printenv | less
查看所有的变量
root@zhangkun:~/study# set
输出所有参数、执行结果和参数个数
#!/bin/bash
# author print variables
echo -e '\033[32m----------------------\033[0m'
echo "This is $0 param !"
echo "This is \$1 is $1 param !"
echo "This is \$2 is $2 param !"
echo -e '\033[32m----------------------\033[0m'
echo "This is \$# is $# param !"
echo "This is \$* is $* param !"
echo "This is \$? is $? param !"
运行结果如下:
(3)LAMP导航菜单编写
#!/bin/bash
#by authors zk
echo -e "\033[32mPlease select Menu follow:\033[1m"
echo "1)install apache server"
echo "2)install Mysql server"
echo "3)install PHP server"
echo "4)install LAMP WEB "
echo "--------------------------------------------"
输出结果如下:
4.2 用户自定义变量
(1)基本语法
1、定义变量:变量名=变量值,注意,=号前后不能有空格
root@zhangkun:/# a=2
root@ zhangkun:/# echo $a
2
root@ zhangkun:/# echo $my_var
root@ zhangkun:/# my_var=hello
root@ zhangkun:/# echo $my_var
hello
root@ zhangkun:/# my_var='Hello,world'
root@ zhangkun:/# echo $my_var
Hello,world
root@ zhangkun:/# my_var="Hello,world"
root@ zhangkun:/# echo $my_var
Hello,world
#自定义变量是一个局部变量
root@ zhangkun:/# env | grep $my_var
root@ zhangkun:/# set | grep $my_var
_=Hello,world
my_var=Hello,world
#定义一个全局变量,变量升级
root@ zhangkun:/# export my_var
root@ zhangkun:/# echo $my_var
Hello,world
#切换进入子shell
root@ zhangkun:/# bash
root@ zhangkun:/# echo $my_var
Hello,world
#如果在子shell中更改变量的值只会在子shell中有效
root@ zhangkun:/# echo $my_var
Hello,world
root@ zhangkun:/# bash
root@ zhangkun:/# my_var='Hello'
root@ zhangkun:/# echo $my_var
Hello
root@ zhangkun:/# exit
exit
root@ zhangkun:/# echo $my_var
Hello,world
用户自定义变量
五、IF条件控制语句
在Linux Shell编程中,if、for、while、case等条件流程控制语句用的非常多。
IF (表达式) #if(Variable in Array)
语句1
else
语句2
fi
例一:判断数字大小
#!/bin/bash
NUM1=100
NUM2=200
if (($NUM2 > $NUM1));then
echo "$NUM2 greater $NUM1!"
fi
运行结果如下:
逻辑运算符解析:
-f 判断文件是否存在 eg: if [ -f file name ]
-d 判断目录是否存在 eg: if [ -d dir ]
-eq 等于 应用于:整型比较
-ne 不等于 应用于:整型比较
-lt 小于 应用于:整型比较
-gt 大于 应用于:整型比较
-le 小于或等于 应用于:整型比较
-ge 大于或等于 应用于:整型比较
-a 双方都成立(and)逻辑表达式 -a 逻辑表达式
-o 单方成立(or)逻辑表达式 -o 逻辑表达式
-z 空字符串
例二:判断目录是否存在,如果目录不存在则创建
#!/bin/bash
# judge dir exists
dir=~/Desktop/test
if [ -d $dir ];then
echo "test exist"
else
mkdir -p $dir
echo "$dir mkdir success"
fi
运行结果如下:
例三:判断文件是否存在,如果存在,打印输出,如果不存在创建文件
#!/bin/bash
filename="file.txt"
if [ ! -f $filename ];then
echo "OK">> $filename
else
echo "file is exists"
cat $filename
fi
运行结果如下:
例四:根据分数判断成绩等级
#!/bin/bash
score=$1
if [ -z $score ];then
echo "Usage:{60|80}"
exit
fi
if [[ $score -gt 85 ]];then
echo "very good"
elif [[ $score -gt 75 ]];then
echo "good"
elif [[ $score -gt 60 ]];then
echo "pass"
else
echo "no pass"
fi
运行结果如下:
例五:使用if条件语句编写Mysql备份脚本
#!/bin/bash
# auto backup mysql db
# by author zk
DATE=$(date +%F-%H-%M-%S)
echo $DATE
MYSQLDB=school
BAK_DIR=/root/data/backup/${MYSQLDB}_${DATE}
echo $BAK_DIR
MYSQLUSR=root
MYSQLPW=123456
MYSQLCMD=/usr/bin/mysqldump
if [ $UID -ne 0 ];then
echo "Must to be use root for exec shell."
fi
if [ ! -d $BAK_DIR ];then
mkdir -p $BAK_DIR
echo -e "\033[32mThe $BAK_DIR Create Successfully!"
else
echo "This $BAK_DIR is exists..."
fi
$MYSQLCMD -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >$BAK_DIR/$MYSQLDB.sql
if [ $? -eq 0 ];then
echo -e "\033[32mThe Mysql Backup $MYSQLDB Successfully!"
else
echo -e "\033[32mThe Mysql Backup $MYSQLDB Failed,Please check!"
fi
先使用sh -n检查脚本是否存在问题
然后执行脚本
执行成功
可以看到备份sql
看一下备份sql的内容
指定每日备份计划
输入如下代码
0 0 * * * /bin/bash /home/zk/Desktop/shelltext/mysql_bak.sh >> /tmp/mysql_bak.log 2>&1
指定每日0点0分执行mysql_bak.sh备份数据库
例六:if条件综合LAMP一键安装脚本
一键源码安装LAMP脚本,先分解任务:
打印菜单
(1)安装apache WEB服务器
(2)安装Mysql DB服务器
(3)安装PHP服务器
(4)整合LAMP架构并启动服务
(1)安装apache WEB服务器
下载httpd-2.4.62.tar.bz2版本,下载URL,解压,进入安装目录,configure;make ; make install
遇到过如下错误
错误一:
checking for APR... no configure: error: APR not found. Please read the documentation.
这个错误需要先安装APR,在脚本里实现
错误二:
error: no acceptable C compiler found in $PATH
这个错误需要安装GCC
zk@zk-None:~/Desktop/shelltext$ sudo apt install gcc
错误三:
LAMP.sh: line 37: make: command not found
一般出现这个-bash: make: command not found提示,系统没有安装make等常用命令,直接sudo apt-get install安装下即可。
解决方法:
sudo apt-get update
sudo apt-get install gcc automake autoconf libtool make
错误四:
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory
解决方法:
sudo apt-get update
sudo apt-get install libexpat1-dev
错误五:安装pcre时
configure: error: Invalid C++ compiler or C++ compiler flags
解决方法:
sudo apt-update
sudo apt-get install g++
安装apache web服务器LAMP.sh
错误六:启动apache报错
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
到httpd目录下配置httpd.conf中Servername
ServerName localhost:80
(1)安装apache服务器的脚本LAMP.sh
#!/bin/bash
# auto make install LAMP
# by authors zk
# HTTP define path variable
H_PREFIX_APACHE=/usr/local/apache
H_FILES_HTTPD=httpd-2.4.62.tar.bz2
H_FILES_HTTPD_DIR=httpd-2.4.62
H_HTTPD_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
H_PREFIX_HTTPD=/usr/local/apache/httpd/
H_FILES_APR=apr-1.6.5.tar.bz2
H_FILES_APR_DIR=apr-1.6.5
H_APR_URL=http://mirrors.tuna.tsinghua.edu.cn/apache/apr/
H_PREFIX_APR=/usr/local/apache/apr/
H_FILES_APR_UTIL=apr-util-1.6.3.tar.bz2
H_FILES_APR_UTIL_DIR=apr-util-1.6.3
H_APR_UTIL_URL=http://mirrors.tuna.tsinghua.edu.cn/apache/apr/
H_PREFIX_APR_UTIL=/usr/local/apache/apr-util/
H_FILES_PCRE=pcre-8.45.tar.gz
H_FILES_PCRE_DIR=pcre-8.45
H_PCRE_URL=https://sourceforge.net/projects/pcre/files/pcre/8.45/
H_PREFIX_PCRE=/usr/local/apache/pcre/
H_FILES_PCRE2=pcre2-10.37.tar.gz
H_FILES_PCRE2_DIR=pcre2-10.37
H_PCRE2_URL=https://sourceforge.net/projects/pcre/files/pcre2/10.37/
H_PREFIX_PCRE2=/usr/local/apache/pcre2/
if [ -z "$1" ];then
echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
echo -e "\033[32m1)install apache server\033[1m"
echo -e "\033[32m2)install Mysql server\033[1m"
echo -e "\033[32m3)install PHP server\033[1m"
echo -e "\033[31mUsage:{ /bin/sh $0 1|2|3|help}\033[0m"
exit
fi
if [[ "$1" -eq "help" ]];then
echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
echo -e "\033[32m1)install apache server\033[1m"
echo -e "\033[32m2)install Mysql server\033[1m"
echo -e "\033[32m3)install PHP server\033[1m"
echo -e "\033[31mUsage:{ /bin/sh $0 1|2|3|help}\033[0m"
exit
fi
if [[ "$1" -eq "1" ]];then
wget -c $H_APR_URL/$H_FILES_APR &&tar -jxvf $H_FILES_APR &&cd $H_FILES_APR_DIR ;./configure --prefix=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_APR_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_APR_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_APR_UTIL_URL/$H_FILES_APR_UTIL &&tar -jxvf $H_FILES_APR_UTIL &&cd $H_FILES_APR_UTIL_DIR ;./configure --prefix=$H_PREFIX_APR_UTIL --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_APR_UTIL_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_APR_UTIL_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_PCRE_URL/$H_FILES_PCRE &&tar -zxvf $H_FILES_PCRE &&cd $H_FILES_PCRE_DIR ;./configure --prefix=$H_PREFIX_PCRE --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_PCRE_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_PCRE_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_PCRE2_URL/$H_FILES_PCRE2 &&tar -zxvf $H_FILES_PCRE2 &&cd $H_FILES_PCRE2_DIR ;./configure --prefix=$H_PREFIX_PCRE2 --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_PCRE2_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_PCRE2_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
export PATH=$PATH:$H_PREFIX_PCRE
echo $PATH
wget -c $H_HTTPD_URL/$H_FILES_HTTPD &&tar -jxvf $H_FILES_HTTPD &&cd $H_FILES_HTTPD_DIR ;./configure --prefix=$H_PREFIX_HTTPD --with-apr=$H_PREFIX_APR --with-apr-util=$H_PREFIX_APR_UTIL --enable-so --enable-mods-shared=most --with-pcre=$H_PREFIX_PCRE --with-pcre2=$H_PREFIX_PCRE2
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_HTTPD_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_HTTPD_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
fi
执行完以上脚本后,启动apache服务器
root@zk-None:/usr/local/apache/httpd/bin# /usr/local/apache/httpd/bin/apachectl -k start
httpd (pid 351155) already running
root@zk-None:/usr/local/apache/httpd/bin# /usr/local/apache/httpd/bin/apachectl -k stop
root@zk-None:/usr/local/apache/httpd/bin# /usr/local/apache/httpd/bin/apachectl -k start
查看到80端口正在监听
root@zk-None:/usr/local/apache/httpd/bin# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.54:53 0.0.0.0:* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
访问localhost:80
apache服务器安装成功。
(2)安装Mysql DB服务器
安装mysql服务器脚本LAMP.sh
#!/bin/bash
# auto make install LAMP
# by authors zk
# HTTP define path variable
H_PREFIX_APACHE=/usr/local/apache
H_FILES_HTTPD=httpd-2.4.62.tar.bz2
H_FILES_HTTPD_DIR=httpd-2.4.62
H_HTTPD_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
H_PREFIX_HTTPD=/usr/local/apache/httpd/
H_FILES_APR=apr-1.6.5.tar.bz2
H_FILES_APR_DIR=apr-1.6.5
H_APR_URL=http://mirrors.tuna.tsinghua.edu.cn/apache/apr/
H_PREFIX_APR=/usr/local/apache/apr/
H_FILES_APR_UTIL=apr-util-1.6.3.tar.bz2
H_FILES_APR_UTIL_DIR=apr-util-1.6.3
H_APR_UTIL_URL=http://mirrors.tuna.tsinghua.edu.cn/apache/apr/
H_PREFIX_APR_UTIL=/usr/local/apache/apr-util/
H_FILES_PCRE=pcre-8.45.tar.gz
H_FILES_PCRE_DIR=pcre-8.45
H_PCRE_URL=https://sourceforge.net/projects/pcre/files/pcre/8.45/
H_PREFIX_PCRE=/usr/local/apache/pcre/
H_FILES_PCRE2=pcre2-10.37.tar.gz
H_FILES_PCRE2_DIR=pcre2-10.37
H_PCRE2_URL=https://sourceforge.net/projects/pcre/files/pcre2/10.37/
H_PREFIX_PCRE2=/usr/local/apache/pcre2/
if [ -z "$1" ];then
echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
echo -e "\033[32m1)install apache server\033[1m"
echo -e "\033[32m2)install Mysql server\033[1m"
echo -e "\033[32m3)install PHP server\033[1m"
echo -e "\033[31mUsage:{ /bin/sh $0 1|2|3|help}\033[0m"
exit
fi
if [[ "$1" -eq "help" ]];then
echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
echo -e "\033[32m1)install apache server\033[1m"
echo -e "\033[32m2)install Mysql server\033[1m"
echo -e "\033[32m3)install PHP server\033[1m"
echo -e "\033[31mUsage:{ /bin/sh $0 1|2|3|help}\033[0m"
exit
fi
if [[ "$1" -eq "1" ]];then
wget -c $H_APR_URL/$H_FILES_APR &&tar -jxvf $H_FILES_APR &&cd $H_FILES_APR_DIR ;./configure --prefix=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_APR_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_APR_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_APR_UTIL_URL/$H_FILES_APR_UTIL &&tar -jxvf $H_FILES_APR_UTIL &&cd $H_FILES_APR_UTIL_DIR ;./configure --prefix=$H_PREFIX_APR_UTIL --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_APR_UTIL_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_APR_UTIL_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_PCRE_URL/$H_FILES_PCRE &&tar -zxvf $H_FILES_PCRE &&cd $H_FILES_PCRE_DIR ;./configure --prefix=$H_PREFIX_PCRE --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_PCRE_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_PCRE_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
wget -c $H_PCRE2_URL/$H_FILES_PCRE2 &&tar -zxvf $H_FILES_PCRE2 &&cd $H_FILES_PCRE2_DIR ;./configure --prefix=$H_PREFIX_PCRE2 --with-apr=$H_PREFIX_APR
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_PCRE2_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_PCRE2_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
export PATH=$PATH:$H_PREFIX_PCRE
echo $PATH
wget -c $H_HTTPD_URL/$H_FILES_HTTPD &&tar -jxvf $H_FILES_HTTPD &&cd $H_FILES_HTTPD_DIR ;./configure --prefix=$H_PREFIX_HTTPD --with-apr=$H_PREFIX_APR --with-apr-util=$H_PREFIX_APR_UTIL --enable-so --enable-mods-shared=most --with-pcre=$H_PREFIX_PCRE --with-pcre2=$H_PREFIX_PCRE2
if [ $? -eq 0 ];then
make && make install
echo -e "\033[32mThe $H_FILES_HTTPD_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_HTTPD_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
fi
if [[ "$1" -eq "2" ]];then
sudo systemctl stop mysql
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get remove --purge mysql-*
sudo apt update
sudo apt install mysql-server mysql-client
if [ $? -eq 0 ];then
echo -e "\033[32mThe $H_FILES_MYSQL_DIR Server Install Successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_MYSQL_DIR Server Install Failed,Please check...!\033[0m"
exit
fi
fi
运行脚本LAMP.sh后,输入mysql登录数据库
root@zk-None:~# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
(3)安装PHP服务器
在LAMP.sh中配置安装PHP服务器功能
六、for循环语句
For 变量 in 字符串
do
语句1
done
例一:打印seq数字循环
#!/bin/bash
for i in $(seq 1 15);
do
echo -e "\033[32mThe Num is $i\033[32m"
done
运行结果如下:
例二:求和1-100的值
#!/bin/bash
# auto sum 1 100
j=0
for ((i=1;i<=100;i++));
do
j=$(($i+$j))
done
echo $j
#!/bin/bash
# auto sum 1 100
j=0
for ((i=1;i<=100;i++));
do
#j=$(($i+$j))
j=$(expr $j + $i)
done
echo $j
运行结果如下:
例三:批量打包sh文件
#!/bin/bash
for i in `find * . -maxdepth 1 -name "*.sh"`
do
tar -czvf $i.tgz $i
done
运行结果如下:
例四:批量解压文件
#!/bin/bash
for i in `find * . -maxdepth 1 -name "*.tgz"`
do
tar -xzvf $i -C /home/zk/Desktop/shelltext
done
执行前
执行后
例五:服务器远程批量传输文件
首先需要开放虚拟机中的22端口号
Linux 中 22 端口是 ssh 应用端口用以进行远程访问,正常情况下 Linux 服务器要打开 22 端口。
如下命令检查服务器是否启用 22 端口:
netstat -tln | grep 22
如果结果出现 xxx:22 等结果则说明,22 端口已开启,否则需要手动开启。
开启 22 端口
首先,检查是否安装 SSH 服务器,执行如下命令
sudo apt-get update
sudo apt-get install openssh-server
然后,开启服务器上的 22 端口。需要修改 SSH 服务器配置文件/etc/ssh/sshd_config
。执行如下命令:
sudo vi /etc/ssh/sshd_config
找到“#Port 22”一行,然后将其改为“Port 22”。
最后,需要重启SSH服务以使配置更改生效,执行如下命令:
sudo service ssh restart
再次执行
netstat -tln | grep 22
检查 22 端口状态。如果开启,则可以通过 ssh 链接该服务器。
开启ssh服务
systemctl enable ssh.service
service sshd start
查看ssh服务状态
service ssh status
Linux的ip地址是192.168.67.128
#!/bin/bash
#auto ssh ls /tmp
#by authors zk 2024
for i in `echo 192.168.67.128 127.0.0.1`
do
scp /tmp/test.txt root@$i:/root/
done
在执行脚本的时候我遇到如下问题:
更改了/etc/ssh/sshd_config文件下的配置不好用
root@zk-None:/home/zk/Desktop/shelltext# cat /etc/ssh/sshd_config | grep 'PermitRootLogin yes\|PasswordAuthentication yes'
PermitRootLogin yes
PasswordAuthentication yes
最后更改了/etc/ssh/sshd_config下UsePAM解决问题
root@zk-None:/var/log# cat /etc/ssh/sshd_config | grep 'UsePAM'
UsePAM no
生成免密钥登陆
root@zk-None:/home/zk/Desktop/shelltext# ssh-keygen
例六:远程主机批量执行命令
#!/bin/bash
#auto ssh ls /tmp
#by authors zk 2024
for i in `seq 100 200`
do
ssh -l root 192.168.1.$i 'ls /tmp'
done