bash脚本2

[[email protected] mnt]# a=1
[[email protected] mnt]# echo $a
1
[[email protected] mnt]# vim file
[[email protected] mnt]# sh file


[[email protected] mnt]# export a=1
[[email protected] mnt]# sh file
1
[[email protected] mnt]# sh file
1
[[email protected] mnt]# logout //切换用户试
[[email protected] Desktop]$ cd /mnt
[[email protected] mnt]$ sh file //空


[[email protected] mnt]$ su - root //再换回来试
Password: 
Last login: Sun Jun 18 08:52:01 CST 2017 on pts/0
[[email protected] ~]# cd /mnt
[[email protected] mnt]# sh file //证明是暂时的


[[email protected] mnt]# cd
[[email protected] ~]# vim .bash_profile //永久设定方法,只在当前用户可用
bash脚本2
[[email protected] ~]# sh /mnt/file


[[email protected] ~]# logout
[[email protected] mnt]$ su - root
Password: 
Last login: Sun Jun 18 08:52:57 CST 2017 on pts/0
[[email protected] ~]# sh /mnt/file
1
[[email protected] ~]# logout
[[email protected] mnt]$ sh /mnt/file //其他用户不可用


[[email protected] mnt]$ su - root
Password: 
Last login: Sun Jun 18 08:54:31 CST 2017 on pts/0
[[email protected] ~]# sh /mnt/file


[[email protected] ~]# vim .bash_profile 
[[email protected] ~]# vim /etc/profile //永久设定方法,所有用户都可以
********************** source /mnt/profile
bash脚本2
[[email protected] ~]# sh /mnt/file
1
[r[email protected] ~]# su - kiosk
Last login: Sun Jun 18 00:44:25 CST 2017 on :0
[[email protected] ~]$ sh /mnt/file
2
[[email protected] ~]$ logout
[[email protected] ~]# sh /mnt/file
1

[[email protected] ~]# echo $PATH 

bash脚本2


-------------------------------------------------------------------------


alias别名

bash脚本2

bash脚本2

bash脚本2

bash脚本2

bash脚本2


bash脚本2


















数值比较[[ <条件> ]]
-eq 相等
-ne 不相等
-lt 小于
-le 小于等于
-gt 大于
-ge 大于等于


字符串比较[[ <条件> ]]
字符串1 == 字符串2 字符串匹配
字符串1 != 字符串2 字符串不匹配
-z 字符串 字符串为空
-n 字符串 字符串不为空
字符串 == 模式 字符串于模式匹配
字符串 != 模式 字符串于模式不匹配


文件比较[[ <条件> ]]
-e 文件名 该文件存在
-d 文件名 是目录
-h 文件名 是符号链接
-f 文件名 是常规文件


条件逻辑运算[[ <条件> ]]
条件1 && 条件2 两者都成立
条件1 || 条件2 两者之一成立
! 条件 条件不成立
true 始终成立
false 始终不成立






test条件判断
test命令可用于评估bash脚本中的表达式。它评估其参数所指定的表达式,如果表达式
为true,返回零退出状态,如果表达式为false,则返回非零退出状态。test具有替代语
法,使用方括号"[]"将表达式括起来,这样更易于阅读。
语法:test EXPRESSION 或 [EXPRESSION]
非零或零长度字符串运算符:test -{n|z} STRING
[[email protected] ~]# [ -n westos ]; echo $?
0
[[email protected] ~]# test -n westos;echo $?
0
[[email protected] ~]# [ -z westos ]; echo $?
1
-----------
[[email protected] ~]# a=1
[[email protected] ~]# [ -n "$a" ]&& echo yes
yes
[[email protected] ~]# [ -n "$b" ]&& echo yes|| echo no
no
-----------
字符串比较运算符:=、!=
[[email protected] ~]# [ abc = abc ]; echo $?
0
[[email protected] ~]# [ abc = ABC ]; echo $?
1
[[email protected] ~]# [ abc != ABC ]; echo $?
0
数字比较运算符:-eq、-ne、-lt、-le、-gt、-ge
[[email protected] ~]# [ 1 -eq 1 ]; echo $?
0
[[email protected] ~]# [ 1 -ne 1 ]; echo $?
1
[[email protected] ~]# [ 1 -gt 2 ]; echo $?
1
文件状态运算符:test -{b|c|e|f|d|r|w|x|s|L} FILE/DIRECTORY
[[email protected] ~]# [ -b /dev/sda ]; echo $?
1
[[email protected] ~]# [ -c /dev/zero ]; echo $?
0
[[email protected] ~]# [ -e /etc/passwd ]; echo $?
0
[[email protected] ~]# [ -f /etc/passwd ]; echo $?
0
[[email protected] ~]# [ -d /etc/passwd ]; echo $?
1
[[email protected] ~]# [ -L /etc/passwd ]; echo $?
1
~~~~~~~~~~~~~~~~~~~~~~~~~
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# ll /var/lib/mysql/
total 36892
-rw-rw----. 1 mysql mysql    16384 Jun 22 10:05 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 Jun 22 10:05 aria_log_control
-rw-rw----. 1 mysql mysql 18874368 Jun 22 10:05 ibdata1
-rw-rw----. 1 mysql mysql  5242880 Jun 22 10:05 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 Jun 22 10:05 ib_logfile1
drwx------. 2 mysql mysql     4096 Jun 22 10:05 mysql
srwxrwxrwx. 1 mysql mysql        0 Jun 22 10:05 mysql.sock
drwx------. 2 mysql mysql     4096 Jun 22 10:05 performance_schema
drwx------. 2 mysql mysql        6 Jun 22 10:05 test
[[email protected] ~]# [ -s "/var/lib/mysql/mysql.sock" ] && echo yes || echo no
no
[[email protected] ~]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no
yes
~~~~~~~~~~~~~~~~~~~~~~~~~~
二进制文件运算符:-ef、-nt、-ot
[[email protected] bin]# [ /bin/mount -ef /usr/bin/mount ]; echo $?
0
[[email protected] bin]# [ /bin/mount -nt /usr/bin/mount ]; echo $?
1
[[email protected] bin]# [ /bin/mount -ot /usr/bin/mount ]; echo $?
1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[[email protected] mnt]# touch /mnt/file
[[email protected] mnt]# ln -s /mnt/file /mnt/file1
[[email protected] mnt]# ls -l
total 16
-rw-r--r--. 1 root root   0 Jun 21 20:09 file
lrwxrwxrwx. 1 root root   9 Jun 21 20:09 file1 -> /mnt/file
-rwxr-xr-x. 1 root root 189 Jun 21 17:08 judge1.sh
-rwxr-xr-x. 1 root root 190 Jun 21 16:59 judge.sh
-rwxr-xr-x. 1 root root 147 Jun 21 16:32 state1.sh
-rwxr-xr-x. 1 root root 176 Jun 21 16:03 state.sh
[[email protected] mnt]# [ -L "/mnt/file1" ]&& echo yes ||echo no
yes
[[email protected] mnt]# [ -L "/mnt/file" ]&& echo yes ||echo no
no
[[email protected] mnt]# ln /mnt/file /mnt/file2
[[email protected] mnt]# ll
total 16
-rw-r--r--. 2 root root   0 Jun 21 20:09 file
lrwxrwxrwx. 1 root root   9 Jun 21 20:09 file1 -> /mnt/file
-rw-r--r--. 2 root root   0 Jun 21 20:09 file2
-rwxr-xr-x. 1 root root 189 Jun 21 17:08 judge1.sh
-rwxr-xr-x. 1 root root 190 Jun 21 16:59 judge.sh
-rwxr-xr-x. 1 root root 147 Jun 21 16:32 state1.sh
-rwxr-xr-x. 1 root root 176 Jun 21 16:03 state.sh
[[email protected] mnt]# [ "/mnt/file" -ef "/mnt/file2" ]
[[email protected] mnt]# [ "/mnt/file" -ef "/mnt/file2" ]&& echo yes ||echo no
yes
[[email protected] mnt]# [ "/mnt/file" -ot "/mnt/file2" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file" -nt "/mnt/file2" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes ||echo no
yes
[[email protected] mnt]# [ "/mnt/file" -ot "/mnt/file1" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file" -nt "/mnt/file1" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file2" -ef "/mnt/file1" ]&& echo yes ||echo no
yes
[[email protected] mnt]# [ "/mnt/file2" -ot "/mnt/file1" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file2" -nt "/mnt/file1" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file" -ot "/mnt/judge" ]&& echo yes ||echo no
no
[[email protected] mnt]# [ "/mnt/file" -nt "/mnt/judge" ]&& echo yes ||echo no
yes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
逻辑运算符:-o、-a、!、&&、||
[[email protected] bin]# [ 2 -gt 1 -a 1 -gt 2 ]; echo $?
1
[[email protected] bin]# [ 2 -gt 1 -o 1 -gt 2 ]; echo $?
0
[[email protected] bin]# [ ! 2 -gt 1 ]; echo $?
1
-------------------------------------------------------------------------






脚本


1.判断一个ip是否能ping通
(1)用if实现
[[email protected] mnt]# vim /mnt/state.sh
[[email protected] mnt]# /mnt/state.sh 
please give me a address
[[email protected] mnt]# /mnt/state.sh 172.25.254.44
172.25.254.44 is up
[[email protected] mnt]# /mnt/state.sh 172.25.254.41
172.25.254.41 is down
[[email protected] mnt]# cat /mnt/state.sh 
#!/bin/bash
if
[ ! "$#" -eq "1" ]
then
echo "please give me a address"
else
for IP in $1
do ping -c1 -w1 $1 >> /dev/mull && echo "$1 is up" || echo "$1 is down"
done
fi
(2)用while实现
[[email protected] mnt]# /mnt/state1.sh
please give me an ip address!
[[email protected] mnt]# /mnt/state1.sh 172.25.254.44
172.25.254.44 is up
[[email protected] mnt]# /mnt/state1.sh 172.25.254.41
172.25.254.41 is down
[[email protected] mnt]# cat /mnt/state1.sh
#!/bin/bash
while
[ -z "$*" ]
do
echo "please give me an ip address!"
exit 1
done
ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down






2.判断两数相加与10的关系
(1)
[[email protected] mnt]#  /mnt/judge.sh 1 2
the result is lower 10
[[email protected] mnt]#  /mnt/judge.sh 1 20
the result is over 10
[[email protected] mnt]# cat /mnt/judge.sh
#!/bin/bash
a=$1
b=$2
#[ -z $a $b ] && echo "please input numbers" ||"compare to 10"
c=$[$a + $b]
if [ "$c" -gt 10 ];
then
echo "the result is lower than 10"
elif [ "$c" -eq 10 ]
echo "the result is equle than 10"
else
echo "the result is over than 10"
fi


(2)简洁版(缺点:不能输出相等情况)
[[email protected] mnt]# vim /mnt/judge1.sh
[[email protected] mnt]# chmod +x /mnt/judge1.sh
[[email protected] mnt]# /mnt/judge1.sh
please give me two number
[[email protected] mnt]# /mnt/judge1.sh 2 5
the results is smaller than 10
[[email protected] mnt]# /mnt/judge1.sh 2 50
the result is bigger than 10
[[email protected] mnt]# cat /mnt/judge1.sh 
#!/bin/bash
while
[ -z "$1" -o -z "$2" ]
do
echo "please give me two number"
exit 1
done
NUM=$[$1+$2]
[ $NUM -le 10 ] && echo "the results is smaller than 10" || echo "the result is bigger than 10"


(3)
#!/bin/bash
while
[ -z "$1" -o -z "$2" ]
do
echo "please give me two number"
exit 1
done


((NUM=$1+$2))
while
[ "$NUM" -lt "10" ]
do
echo "$1+$2 is smaller than 10"
exit 0
done
echo "$1+$2 is bigger than 10"




3.判断一个文件是否存在并判断是何种类型
(1)
[[email protected] mnt]# vim type.sh
[[email protected] mnt]# chmod +x /mnt/type.sh 
[[email protected] mnt]#  /mnt/type.sh 
please give me a file/directory!
[[email protected] mnt]#  /mnt/type.sh /mmm
/mmm is not exist
[[email protected] mnt]#  /mnt/type.sh /mnt
/mnt is a directory
[[email protected] mnt]#  /mnt/type.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is a SSSS
[[email protected] mnt]# cat /mnt/type.sh 
#!/bin/bash
while
[ -z $* ]
do
echo "please give me a file/directory!"
exit 1
done


if [ ! -e $1 ]
then
echo $1 is not exist
exit 1
elif [ -f $1 ]
then
echo $1 is a file
elif [ -d $1 ]
then
echo $1 is a directory
elif [ -b $1 ]
then
echo $1 is a 块文件
elif [ -c $1 ]
then
echo $1 is a 字符设备文件
elif [ -L $1 ]
then
echo $1 is a 链接
elif [ -S $1 ]
then
echo $1 is a SSSS
fi


(2)
#!/bin/bash
while [ -z "$*" ]
do
echo "please give me a file/directory!"
exit 1
done


[ -e $1 ] || (echo $1 is not exist!;exit 1 )
[ -b $1 ] && echo "$1 is a blockfile"
[ -d $1 ] && echo "$1 is a directory"
[ -S $1 ] && echo "$1 is a socket file"
[ -L $1 ] && echo "$1 is a link file"
[ -c $1 ] && echo "$1 is a charactor file"
[ -f "$1" -a ! -L "$1" ] && echo "$1 is a file"




4.创建文件中不存在的用户名,并赋密码
(1)
#!/bin/bash
if
[ -z "$1" ]
then
echo please give me an userfile!
elif [ ! -e "$1" ]
then
echo "$1 is not exist!"
else
for NAME in `cat $1`
do
USER=`geten passwd $NAME`
if
[ -z "$USER" ]
then
useradd $NAME
echo westos | passwd --stdin $NAME
else
echo $NAME is exist!!
fi
done
fi




(2)
#!/bin/bash
ACTION () {
if [ -z "$4" ]
then
echo please give me a userfile
elif
[ ! -e "$4" ]
then
echo "$4 is not exist!"
else 


for NAME in `cat $4`
do
USER=`getent passwd $NAME`
if [ $1 "$USER" ]
then
$2 $NAME
[ "$2" = "useradd" ]&&(echo westos |passwd --stdin $NAME)
else
echo $NAME $3
fi
done
fi
}


if [ "$1" = "create" ]
then
ACTION -z "useradd" 'exist' $2
elif [ "$1" = "delete" ]
then
ACTION -n 'useradd -r' 'not exist' $2
else
echo "Usage: ctrl_user.sh <create|delete>    <userfile>"
fi






5.交互输入输出
(1)定死内容的
[[email protected] mnt]# expect /jiaohu.exp
spawn /mnt/jiaohu.sh
Who are you: lee
How old are you: 18
What's your class: linux
What's your feeling: happy
lee is 18's old study linux and feel happy
[[email protected] mnt]# cat /mnt/jiaohu.sh
#!/bin/bash
read -p "Who are you: " USER
read -p "How old are you: " AGE
read -p "What's your class: " CLASS
read -p "What's your feeling: " FEEL
echo $USER is ${AGE}\'s old study $CLASS and feel $FEEL
[[email protected] mnt]# cat /jiaohu.exp
#!/usr/bin/expect
spawn /mnt/jiaohu.sh
expect "Who"
send "lee\r"
expect "How"
send "18\r"
expect "class"
send "linux\r"
expect "feel"
send "happy\r"
expect eof


(2)内容可变
.sh文件不变
[[email protected] mnt]# vim /jiaohu.exp
[[email protected] mnt]# cat /jiaohu.exp
#!/usr/bin/expect
set NAME [ lindex $argv 0 ]
set AGE  [ lindex $argv 1 ]
set CLASS [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]
spawn /mnt/jiaohu.sh
expect "Who"
send "$NAME\r"
expect "How"
send "$AGE\r"
expect "class"
send "$CLASS\r"
expect "feel"
send "$FEEL\r"
expect eof
[[email protected] mnt]# expect /jiaohu.exp
spawn /mnt/jiaohu.sh
Who are you: 
How old are you: 
What's your class: 
What's your feeling: 
is 's old study and feel
[[email protected] mnt]# expect /jiaohu.exp lee 18 linux unhappy
spawn /mnt/jiaohu.sh
Who are you: lee
How old are you: 18
What's your class: linux
What's your feeling: unhappy
lee is 18's old study linux and feel unhappy
[[email protected] mnt]# expect /jiaohu.exp lee 18 linux 'not happy'
spawn /mnt/jiaohu.sh
Who are you: lee
How old are you: 18
What's your class: linux
What's your feeling: not happy
lee is 18's old study linux and feel not happy


5.交互连接一台主机
(1)连接时,有时有yes/no选项,有时没有,就造成了问题
[[email protected] ~]# expect /mnt/ping.exp 172.25.254.205
spawn ssh [email protected]
yes
[email protected]'s password: 
Last login: Thu Jun 22 15:59:57 2017 from 172.25.254.44
[[email protected] ~]# logout
Connection to 172.25.254.205 closed.
[[email protected] ~]# cat /mnt/ping.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
spawn ssh [email protected]$IP
expect "yes/no"
send "yes\r"
expect "password:"
send "redhat\r"
interact


(2)改为可变的
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
spawn ssh [email protected]
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "redhat\r"}
}
interact


(3)输入ip和密码连接的
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
set PASS [lindex $argv 1 ]
spawn ssh [email protected]
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$PASS\r"}
}
interact


(4)在文件中读取ip和密码并自动接入的(文件中可写多行信息)
[[email protected] mnt]# chmod +x /mnt/ping.exp 
[[email protected] mnt]#  /mnt/ping2.sh /mnt/host
spawn ssh [email protected]
yes
[email protected]'s password: 
Last login: Thu Jun 22 16:13:47 2017 from 172.25.254.205
[[email protected] ~]# logout
Connection to 172.25.254.205 closed.
[[email protected] mnt]# cat /mnt/ping2.sh 
#!/bin/bash
if
[ -n "$*" ]
then
MAX_LINE=`wc -l $* | awk '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
IP=`sed -n ${NUM}p $* | awk '{print $1}'`
PASS=`sed -n ${NUM}p $* | awk '{print $1}'`
/mnt/ping.exp $IP $PASS hostname
done
else
echo "Useage:check_host.sh <filename>"
fi
[[email protected] mnt]# cat /mnt/host
172.25.254.205 redhat
[[email protected] mnt]# cat /mnt/ping.exp 
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
spawn ssh [email protected]$IP
expect "yes/no"
send "yes\r"
expect "password:"
send "redhat\r"
interact


(5)登陆并输出此ip的hostname
#!/bin/bash
if
[ -n "$*" ]
then
MAX_LINE=`wc -l $* | awk '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
IP=`sed -n ${NUM}p $* | awk '{print $1}'`
PASS=`sed -n ${NUM}p $* | awk '{print $1}'`
echo "${IP}\'s hostname is `/mnt/ping.exp $IP $PASS hostname | tail -n 1`"
done
else
echo "Useage:check_host.sh <filename>"
fi


(6)查找此ip中的user
#!/bin/bash
if
[ -n "$*" ]
then
MAX_LINE=`wc -l $* | awk '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
IP=`sed -n ${NUM}p $* | awk '{print $1}'`
PASS=`sed -n ${NUM}p $* | awk '{print $1}'`
echo $IP's user :
  /mnt/ping.exp $IP $PASS 'getent passwd' |grep ^root
done
else
echo "Useage:check_host.sh <filename>"
fi