#########################squid##############################################
####1,test条件判断
test命令可用于评估bash脚本中的表达式。它评估其参数所指定的表达式,如果表达式
为true,返回零退出状态,如果表达式为false,则返回非零退出状态。test具有替代语
法,使用方括号"[]"将表达式括起来,这样更易于阅读。
1)语法:test EXPRESSION 或[EXPRESSION]
非零或零长度字符串运算符:test -{n|z} STRING
如图一:
实例:
vim check_ip.sh
如图二
测试:
如图三
2)字符串比较运算符:=、!=
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" = "$b" ]&& echoyes||echo no
yes
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" = "$b" ]&& echo yes||echo no
no
[root@localhost mnt]# [ "$a" != "$b" ]&& echo yes||echo no
yes
[root@localhost mnt]# [ ! "$a" = "$b" ]&& echoyes|| echo no
yes
3)数字比较运算符:-eq、-ne、-lt、-le、-gt、-ge
[root@localhost mnt]# [ "$a"-eq "$b" ]&& echo yes|| echo no
no
[root@localhost mnt]# [ "$a"-le "$b" ]&& echo yes|| echo no
yes
[root@localhost mnt]# [ "$a"-lt "$b" ]&& echo yes|| echo no
yes
[root@localhost mnt]# [ "$a"-gt "$b" ]&& echo yes|| echo no
no
[root@localhost mnt]# [ "$a"-ge "$b" ]&& echo yes|| echo no
no
[root@localhost mnt]# [ "$a"-ne "$b" ]&& echo yes|| echo no
yes
实例:
vim check_num.sh
如图四
测试:
[root@localhost mnt]# /mnt/check_num.sh
please two number!!
[root@localhost mnt]# /mnt/check_num.sh 1 3
1+3 is smaller than 10
[root@localhost mnt]# /mnt/check_num.sh 5 7
5+7 is bigger than 10
4)文件状态运算符:test -{b|c|e|f|d|r|w|x|s|L}FILE/DIRECTORY
[root@localhost mnt]# [ -e "/hello" ]&& echo yes || echono
no
[root@localhost mnt]# [ -e "/mnt" ]&& echo yes || echono
yes
[root@localhost mnt]# [ -d "/mnt" ]&& echo yes || echono
yes
[root@localhost mnt]# [ -f "/mnt" ]&& echo yes || echono
no
[root@localhost mnt]# [ -x "/mnt" ]&& echo yes || echono
yes
[root@localhost mnt]# [ -c "/mnt" ]&& echo yes || echono
no
[root@localhost mnt]# [ -s "/mnt" ]&& echo yes || echono
yes
[root@localhost mnt]# [ -S "/var/lib/mysql/mysql.sock" ]&&echo yes || echo no
yes
[root@localhost mnt]# ln -s /mnt/file /mnt/file1
[root@localhost mnt]# [ -L "/mnt/file1" ]&& echo yes || echono
yes
vim check_file.sh
如图五
chmod +x /mnt/check_file.sh
测试:
[root@localhost mnt]# /mnt/check_file.sh
USE: /mnt/check_file.sh file
[root@localhost mnt]# /mnt/check_file.sh /mnt/file
FILE exists and is a regular file
[root@localhost mnt]# /mnt/check_file.sh /mnt/file1
FILE exists and is a symbolic link (same as -h)
[root@localhost mnt]# /mnt/check_file.sh /var/lib/mysql/mysql.sock
FILE exists and is a socket
[root@localhost mnt]# /mnt/check_file.sh /mmm
FILE not exists
[root@localhost mnt]# /mnt/check_file.sh /mnt
FILE exists and is a directory
5)二进制文件运算符:-ef、-nt、-ot
[root@server0 bin]# [ /bin/mount -ef /usr/bin/mount ]; echo $?
0
[root@server0 bin]# [ /bin/mount -nt /usr/bin/mount ]; echo $?
1
[root@server0 bin]# [ /bin/mount -ot /usr/bin/mount ]; echo $?
1
6)逻辑运算符:-o、-a、!、&&、||
[root@server0 bin]# [ 2 -gt 1 -a 1 -gt 2 ]; echo $?
1
[root@server0 bin]# [ 2 -gt 1 -o 1 -gt 2 ]; echo $?
0
[root@server0 bin]# [ ! 2 -gt 1 ]; echo $?
1
#####2,if语句
if命令检查if后面的命令或列表的退出值。如果第一个命令评估为true/零,则运行then
之后的命令列表,直至任一else。如果第一个命令评估为false/非零,则运行else与fi之
间的命令列表(反向平写if,标记if块的结束)。
语法:if command; then command; command2; else command3;fi
1)
vim create_user.sh
#!/bin/bash
if
[ -z "$1" ]
then
echo "please give me auserfile"
elif
[ ! -e "$1" ]
then
echo "$1 is notexist!!"
else
for NAME in `cat $1`
do
USER=`getent passwd $NAME`
if
[ -z "$USER" ]
then
useradd $NAME
echo westos |passwd --stdin$NAME
else
echo "$NAME isexist"
fi
done
fi
测试:
[root@localhost mnt]# /mnt/create_user.sh
please give me a userfile
[root@localhost mnt]# /mnt/create_user.sh /mnt/userfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Changing password for user user4.
passwd: all authentication tokens updated successfully.
Changing password for user user5.
passwd: all authentication tokens updated successfully.
2)
vim ctr_user.sh
#!/bin/bash
if
[ "$1" = "create" ]
then
if
[ -z "$2" ]
then
echo "please give mea userfile"
elif
[ ! -e "$2" ]
then
echo "$2 is notexist!!"
else
for NAME in `cat$2`
do
USER=`getent passwd$NAME`
if
[ -z "$USER"]
then
useradd $NAME
echo westos |passwd--stdin $NAME
else
echo "$NAME isexist"
fi
done
fi
elif
[ "$1" = "delete" ]
then
if
[ -z "$2" ]
then
echo "please give mea userfile"
elif
[ ! -e "$2" ]
then
echo "$2 is notexist!!"
else
for NAME in `cat$2`
do
USER=`getent passwd$NAME`
if
[ -n "$USER" ]
then
userdel -r $NAME
else
echo "$NAME is notexist!!"
fi
done
fi
else
echo "Use:ctr_user.sh<create|delete> <userfile>"
fi
测试:
[root@localhost mnt]# /mnt/ctr_user.sh
Use:ctr_user.sh <create|delete> <userfile>
[root@localhost mnt]# /mnt/ctr_user.sh create
please give me a userfile
[root@localhost mnt]# /mnt/ctr_user.sh create /mnt/userfile
user1 is exist
user2 is exist
user3 is exist
user4 is exist
user5 is exist
[root@localhost mnt]# /mnt/ctr_user.sh delete /mnt/userfile
[root@localhost mnt]# /mnt/ctr_user.sh delete /mnt/userfile
user1 is not exist!!
user2 is not exist!!
user3 is not exist!!
user4 is not exist!!
user5 is not exist!!
[root@localhost mnt]# /mnt/ctr_user.sh create /mnt/userfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Changing password for user user4.
passwd: all authentication tokens updated successfully.
Changing password for user user5.
passwd: all authentication tokens updated successfully.
3)
vim ctr_user.sh(函数)
#!/bin/bash
ACTION(){
if
[ -z "$4" ]
then
echo "please give me auserfile"
elif
[ ! -e "$4" ]
then
echo "$4 is notexist!!"
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 'userdel -r' 'notexist!!' $2
else
echo "Use:ctr_user.sh<create|delete> <userfile>"
fi
测试:
[root@localhost mnt]# /mnt/ctr_user1.sh create userfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Changing password for user user4.
passwd: all authentication tokens updated successfully.
Changing password for user user5.
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# /mnt/ctr_user1.sh delete userfile
[root@localhost mnt]# /mnt/ctr_user1.sh delete userfile
user1 not exist!!
user2 not exist!!
user3 not exist!!
user4 not exist!!
#############二,expect语句
在shell中利用expect实现自动应答脚本。
# cat talk
echo "who are you?"
read who
echo "hello, $who"
echo "are you
happy?"
read answer
echo "why?"
read answer
# cat auto
#!/usr/bin/expect
#set timeout 10
spawn ./talk
expect "who"
send "firefly\n"
expect "happy?"
send "Yes,I am happy.\n"
expect "why?"
send "enen!\n"
expect eof
exi
#!/usr/bin/expect
这一行告诉操作系统脚本里的代码使用那一个shell来执行。
set timeout 10
设置后面所有的expect命令的等待响应的超时时间,单位为秒。
spawn talk
spawn是expect的内部命令,作用是给后面的shell指令加个壳,用来传递交互指令。
expect "who"
判断上次输出结果里是否包含“who”的字符串,如果有则立即返回,否则等待超时时间后返回。
send "westos\n"
执行交互动作,相当于手工输入"westos"。
expect eof
作用是在输出中搜索文件结束符,如果没有这一行,脚本会立即退出,得不到正确结果。
interact
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。否则退出登录。
$argv 参数数组
expect脚本可以接受从bash传递过来的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第
一个,第二个,第三个....参数。
实例1:
vim autossh.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
spawn ssh root@$IP
expect {
"yes/no" {send"yes\r";exp_continue}
"password:" {send"westos\r"}
}
interact
测试:
[root@localhost mnt]# /mnt/autossh.exp 172.25.254.90
spawn ssh root@172.25.254.90
The authenticity of host '172.25.254.90 (172.25.254.90)' can't beestablished.
ECDSA key fingerprint is dc:ac:86:b2:77:db:aa:1f:d2:43:fe:98:a7:4b:ee:36.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.90' (ECDSA) to the list of knownhosts.
westos
实例2:(命令)
(1)
vim check_hostname.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 $2}'`
/mnt/autossh.exp $IP $PASShostname | tail -n 1
done
else
echo "Use:check_host.sh<filename>"
fi
(2)
vim autossh.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
set COMMAND [ lindex $argv 2 ]
spawn ssh root@$IP $COMMAND
expect {
"yes/no" {send"yes\r";exp_continue}
"password:" {send"$PASS\r"}
}
interact
#####三,环境变量
shell和脚本使用变量来存储数据 ,有些变量可以连同它们的内容传递给子进程,这些
变量我们称之为环境变量。
[root@localhost mnt]# export LINUX
[root@localhost mnt]# bash
[root@localhost mnt]# echo $LINUX
redhat
[root@localhost mnt]# exit
exit
使用env命令显示所有环境变量
使用set命令现实所有本地定义的shell变量
Bash启动脚本
在用户登录的时候,会运行全局变量文件/etc/profile,和用户自定义变量文件
~/.bash_profile去初始化它们的环境变量。
/etc/profile
\_ /etc/profile.d/*.sh
~/.bash_profile
\_ ~/.bashrc
\_ /etc/bashrc
实例:
vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export PATH=$PATH:/mnt
source .bash_profile
测试:
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/mnt
使用别名
alias命令可以用来自定义属于自己的系统命令,写入~/.bashrc文件永久生效。
查看别名:
# alias
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
设置别名:
# alias mycom='echo hello;hostname'
# mycomm
hello
server0.example.com
删除别名: unalias mycomm
实例:
临时设置别名
[root@localhost ~]# alias xie='vim'
[root@localhost ~]# xie /etc/passwd
永久设置别名
vim /etc/bashrc
# /etc/bashrc
alias xie='vim' ------->设置别名
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
# are we an interactive shell?
source .bash_profile
查看别名:
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot--show-tilde'
alias xie='vim'
删除别名:
[root@localhost ~]# unalias xie
[root@localhost ~]# alias ------->再次查看,‘xie’别名已经删除
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot--show-tilde'
使用函数
pathmunge () {
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
}
...
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
3.12squid
最新推荐文章于 2024-05-05 02:25:09 发布