1.1. MySQL安装
启动 service mysqld start
1.2. ssh免密登录
ssh协议
密码 ssh ip
免密
1 生成密钥 ssh-keygen
2 发送公钥 ssh-copy-id ip ----> 密码
3 ssh ip
集群互相配置免密登录
在每台主机上生成密钥 将公钥发送给每台电脑
scp -r /test/last.jar 192.168.133.4:$PWD
时间同步
1.2.1. date 时间
+%y-%m-%d
date "+%y-%m-%d %H:%M:%S"
mkdir /`date +%y-%m-%d`
1.2.2. 时间同步
ntpdate 安装
执行时间同步命令
ntpdate 0.asia.pool.ntp.org
[root@linux01 /]# date "+%y-%m-%d"
18-05-24
[root@linux01 /]# date -d "1 day ago" "+%y-%m-%d"
18-05-23
[root@linux01 /]# date -d "-1 day" "+%y-%m-%d"
18-05-23
[root@linux01 /]# date -d "+1 day" "+%y-%m-%d"
18-05-25
2. shell编程
java 数据+逻辑运算(数学运算) = 编程
shell 命令解析器
2.1. 变量
a=1
b=lisi
c=3
`expr $a + $b`
#!/bin/bash eccho "This is my first niubi de shell" # 报错不会影响下面代码的执行 echo "开始创建文件” echo "文件创建成创建文件....." touch /niubi.doc.txt.java." echo "开始创建文件夹..." mkdir /niubi |
1 添加执行权限
2 bash
3 sh
4 source .
2.2. source和export
source将
parent.sh
#!/bin/bash a=keyan echo $a sh /last/child.sh |
child.sh
#!/bin/bash echo $a |
2.3. 运算
取变量的值
Sname ${name} “$name”
$(( ))
`expr `
[root@linux01 /]# echo `expr $a + $b ` 25 [root@linux01 /]# echo $(($a+$b)) 25 [root@linux01 /]# echo $((($a+$b)*4)) 100 [root@linux01 /]# echo $(((2+2)/2)) 2 [root@linux01 /]# echo $(((2+2)*2)) 8 [root@linux01 /]# echo ` expr 3 \* 4 ` 12 |
$str”” ${str} 字符串的拼接
2.4. 测试判断
$? 返回上条命令执行的结果 0
[root@linux01 /]# [ 1 == 1 ]
[root@linux01 /]# echo $?
0
[root@linux01 /]# [ 1 != 1 ]
[root@linux01 /]# echo $?
1
[ 条件表达式 ]
[ $name ] 是否不为空 0
【 strkjkl 】 0
【 $dfhdisfds 】 1
[ 1 == 1 ] 0
[ 1 != 1 ] 1
[ 1 != 1 ] && echo "en" || echo "ene~~"
[root@linux01 /]# [ $name ] && echo "$name" || echo "hello"
hello
[root@linux01 /]# [ 1 == 1 ] && echo "en"
en
[root@linux01 /]# [ 1 != 1 ] && echo "en" || echo "ene~~"
ene~~
测试
[root@linux01 /]# [ -e /1.txt ] && echo "en" || echo "ene~~"
en
[root@linux01 /]# [ -e /2.txt ] && echo "en" || echo "ene~~"
ene~~
[root@linux01 /]# [ -e /bin ] && echo "en" || echo "ene~~"
en
[root@linux01 /]# [ -e /ssbin ] && echo "en" || echo "ene~~"
ene~~
[root@linux01 /]# [ -r /1.txt ] && echo "en" || echo "ene~~"
en
[root@linux01 /]# [ -w /1.txt ] && echo "en" || echo "ene~~"
en
[root@linux01 /]# [ -x /1.txt ] && echo "en" || echo "ene~~"
ene~~
[root@linux01 /]# [ -d /1.txt ] && echo "en" || echo "ene~~"
ene~~
[root@linux01 /]# [ -d /bin ] && echo "en" || echo "ene~~"
en
2.5. 流程控制
2.5.1. if
#!/bin/bash if [ 1 != 1 ] then echo "对" else echo "cuo" fi |
name=$1 $1获取第一个输入参数 if [ $name ] then echo "$name" else echo "null" fi |
#!/bin/bash name=$1 if [ $name == keyan ] then echo "welcome $name" elif [ $name == fengjie ] then echo "get out $name" else echo "nishi other" fi |
练习
#!/bin/bash if [ -e /root/logs ] then if [ -e /`date +%y-%m-%d` ] then cp /root/logs/*.log /`date +%y-%m-%d` else mkdir /`date +%y-%m-%d` cp /root/logs/*.log /`date +%y-%m-%d` fi else echo "logs文件不存在" fi |
2.5.2. for
[root@linux01 last]# cat hostnames.txt
bg01
bg02
bg03
[root@linux01 last]# echo `cat hostnames.txt`
bg01 bg02 bg03
for n in 1 2 3 4 5 do echo $n done |
#!/bin/bash for hostname in `cat /last/hostnames.txt` do echo $hostname done |
#!/bin/bash for hostname in bigdata01 bigdata02 bigdata03 do ssh $hostname "source /etc/profile ; java -cp /test/last.jar com.test.Test1" done |
#!/bin/bash source /etc/profile ; java -cp /test/last.jar com.test.Test1 |
2.5.3. while
#!/bin/bash
i=1
while [ $i -le 10 ]
do echo $(($i*$i))
let i++
done
2.5.4. case
#!/bin/bash
name=$1
case $name in
lisi)
echo "$name"
;;
keyan)
echo "hello $name"
;;
*)
echo "bye~~~~"
esac
1 从指定的文件夹下复制文件到指定的文件夹下
2 批量启动多台电脑的java程序
3. 定时器
在指定的时间执行指定的命令或者脚本(shell)
crontab -e 设置定时器
* * * * * 分 时 日 月 周 sh run.sh
* * * * * 分 时 日 月 周 sh run.sh
4. 函数 方法
function 方法名(){}
方法名
[root@linux01 /]# vi funTom.sh m=$1 #!/bin/bash function start { echo "startting..." /usr/apps/apache-tomcat-7.0.47/bin/startup.sh }
function stop(){ echo "stopping........." /usr/apps/apache-tomcat-7.0.47/bin/shutdown.sh }
$1 |