首页 > 代码库 > shell

shell

###Bash Scripts###

###bash脚本基础
#ipshow.sh
只显示当前主机ip地址
#!/bin/bash
ifconfig eth0 | grep inet | grep inet6 -v | cut -d " " -f 10

[root@server102 mnt]# echo #hello#

[root@server102 mnt]# echo "#hello#"
#hello#
[root@server102 mnt]# echo ‘#hello#‘
#helo#
[root@server102 mnt]# echo \#hello\#
#hello#
""双引号不可以引用 ` \ ! & $
[root@server102 mnt]# echo ‘$hello‘
$hello
[root@server102 mnt]# echo ‘`hello`‘
`hello`
[root@server102 mnt]# echo ‘"hello"‘
"hello"
[root@server102 mnt]# echo ""hello""
hello
[root@server102 mnt]# echo "\`hello\`"
`hello`
[root@serv
er102 mnt]# echo "\"hello\""
"hello"

#show_time.sh
显示时间格式为$$$$$ the time now is " 23:51:07 " $$$$$
[root@server102 mnt]# vim ipshow.sh
[root@server102 mnt]# cat ipshow.sh
#!/bin/bash
echo ‘$$$$$ the time now is "‘\ ` date +%T `\ ‘" $$$$$‘

#export
临时设定
[root@server102 ~]# export a=3
[root@server102 ~]# echo $a
3
用户设定
[root@server102 ~]# cd ~
[root@server102 ~]# vim .bash_profile
 13 export a=3
[root@server102 ~]# source .bash_profile
系统设定
[root@server102 ~]# vim /etc/profile
 77 export a=3

*注:用户设定和系统设定冲突时,读取用户设定(先读取系统设定,后读取用户设定,系统设定被覆盖,不存在优先级问题)

#写一个10s倒计时脚本
[root@server102 mnt]# vim dao10s.sh
[root@server102 mnt]# cat dao10s.sh
#!/bin/bash
for ((SEC=10;SEC>0;SEC--))
do
echo -ne "After ${SEC}s is end "
echo -ne "\r    \r"
sleep 1
done

#写一个1min10s倒计时脚本
[root@server102 mnt]# vim dao1M10s.sh
[root@server102 mnt]# cat dao1M10s.sh
#!/bin/bash
MIN=1
for ((SEC=10;SEC>=0;SEC--))
do
echo -ne "After ${MIN}:${SEC}s is end "
sleep 1
echo -ne "\r    \r"
    while
    [ "$SEC" -lt "0" -a "$MIN" -gt "0" ]
    do
    echo -ne "After ${MIN}:${SEC}s is end "
    echo -ne "\r    \r"
    ((MIN--))
    SEC=60
    done

done

#检测教室内30主机的开关状态
[root@server102 mnt]# vim ping.sh
[root@server102 mnt]# cat ping.sh
#!/bin/bash
for NUM in {1..33}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down
done
检测
[root@server102 mnt]# sh ping.sh
172.25.254.1 is down
172.25.254.2 is up
172.25.254.3 is down
172.25.254.4 is down
172.25.254.5 is down
172.25.254.6 is down
172.25.254.7 is down
172.25.254.8 is down
172.25.254.9 is down
172.25.254.10 is down
172.25.254.11 is down
172.25.254.12 is down
172.25.254.13 is up
172.25.254.14 is up
172.25.254.15 is up
172.25.254.16 is down
172.25.254.17 is down
172.25.254.18 is down
172.25.254.19 is down
172.25.254.20 is up
172.25.254.21 is up
172.25.254.22 is down
172.25.254.23 is down
172.25.254.24 is down
172.25.254.25 is up
172.25.254.26 is up
172.25.254.27 is up
172.25.254.28 is up
172.25.254.29 is down
172.25.254.30 is down
172.25.254.31 is down
172.25.254.32 is down
172.25.254.33 is down

#数据库备份脚本
[root@server102 mnt]# yum install mariadb-server -y
[root@server102 mnt]# vim /etc/my.cnf
 skip-networking=1
[root@server102 mnt]# systemctl start mariadb
[root@server102 mnt]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we‘ll need the current
password for the root user.  If you‘ve just installed MariaDB, and
you haven‘t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]
New password:
Re-enter new password:
Sorry, passwords do not match.

New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from ‘localhost‘.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named ‘test‘ that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you‘ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

[root@server102 mnt]# mysql -uroot -pwestos -e "create database westos;"
[root@server102 mnt]# mysql -uroot -pwestos -e "create table westos.linux (username varchar(50) not null,password varchar(50) not null);"
[root@server102 mnt]# mysql -uroot -pwestos -e "insert into westos.linux values (‘zzz‘,‘123‘);"
[root@server102 mnt]# mysql -uroot -pwestos -e "insert into westos.linux values (‘yyy‘,‘123‘);"
[root@server102 mnt]# mysql -uroot -pwestos -e "select * from westos.linux"
+----------+----------+
| username | password |
+----------+----------+
| zzz      | 123      |
| yyy      | 123      |
+----------+----------+
[root@server102 mnt]# mysql -uroot -pwestos -e "show databases;" -EN | grep -E "^\*|schema$" -v
mysql
westos
[root@server102 mnt]# vim mysql.sh
[root@server102 mnt]# cat mysql.sh
#!/bin/bash
for x in $(mysql -uroot -pwestos -e"show databases;" -EN | grep -E "^\*|schema$" -v)
do
    mysqldump -uroot -pwestos $x > /mnt/$x-`date`.dump
done
[root@server102 mnt]# sh mysql.sh
[root@server102 mnt]# ls
mysql.sh    westos-Tue Dec 13 03:22:15 EST 2016.dump   mysql-Tue Dec 13 03:22:15 EST 2016.dump    


###脚本传参

#text.sh
[root@server102 mnt]# vim text.sh
[root@server102 mnt]# cat text.sh
#!/bin/bash
echo $1                    ##脚本后面跟的第一串字符
echo $2                    ##脚本后面跟的第二串字符
echo $3                    ##脚本后面跟的第三串字符
echo $*                    ##脚本后面跟的所有字符
echo $@                    ##脚本后面跟的所有字符
echo $#                    ##脚本后面跟的字符串数
[root@server102 mnt]# sh text.sh red
red


red
red
1
[root@server102 mnt]# sh text.sh red hat
red
hat

red hat
red hat
2
[root@server102 mnt]# sh text.sh red hat linux
red
hat
linux
red hat linux
red hat linux
3

#ping1.sh
写一个脚本后面跟IP显示up或down
[root@server102 mnt]# vim ping1.sh
[root@server102 mnt]# cat ping1.sh
#!/bin/bash
ping -c1 -w1 $@ &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down
测试
[root@server102 mnt]# sh ping1.sh 172.25.254.1
172.25.254. is down
[root@server102 mnt]# sh ping1.sh 172.25.254.2
172.25.254. is up

#create.sh
脚本后面跟的第一串字符为用户,第二串为密码
[root@server102 mnt]# vim create.sh
[root@server102 mnt]# cat create.sh
#!/bin/bash
echo "`useradd $1`"
echo "`echo $2 | passwd --stdin $2`"

#read_ip.sh
执行脚本输入主机号码显示ip
[root@server102 mnt]# vim read_ip.sh
[root@server102 mnt]# cat read_ip.sh
#!/bin/bash
read -p "Please input a interface: " ETH
ifconfig $ETH | grep inet | grep inet6 -v | cut -d " " -f 10
测试
[root@server102 mnt]# sh read_ip.sh
[root@server102 mnt]# sh read_ip.sh
Please input a interface: eth0
172.25.254.102


#create_user.sh
建立用户设置密码
[root@server102 mnt]# vim create_user.sh
[root@server102 mnt]# cat create_user.sh
#!/bin/bash
read -p "please input you want create username: " NAME
read -p "please input ${NAME}‘s password: " -s PASS
useradd $NAME
echo $PASS | passwd --stdin $NAME &> /dev/null && (
    echo  "created $NAME and seted it password" )||
    echo error

测试
[root@server102 mnt]# sh create_user.sh
please input you want create username: westos
please input westos‘s password: created westos and seted it password


#ping.sh
输出ipaddr显示网络是否正常,不输入任何结果报错
[root@server102 mnt]# vim ping.sh
[root@server102 mnt]# cat ping.sh
#!/bin/bash
[ -n "$*" ] && (
ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down
)||(
echo please give me a ip address
)
测试
[root@server102 mnt]# sh ping.sh 172.25.254.1
172.25.254.1 is down
[root@server102 mnt]# sh ping.sh 172.25.254.2
172.25.254.2 is up
[root@server102 mnt]# sh ping.sh
please give me a ip address

#number.sh
输入1-10输出yes,其他为no
[root@server102 mnt]# vim number.sh
[root@server102 mnt]# cat number.sh
#!/bin/bash
read -p "Please input a number: " NUM
if
[ "$NUM" -ge "0" -a "$NUM" -le "10" ]
then
echo yes
else
echo no
fi
测试
[root@server102 mnt]# sh number.sh
Please input a number: 1
yes
[root@server102 mnt]# sh number.sh
Please input a number: 10
yes
[root@server102 mnt]# sh number.sh
Please input a number: 11
no

#check_file.sh
执行check_file.sh file 显示文件类型,无此文件,请输入一个文件名称
[root@server102 mnt]# vim check_file.sh
[root@server102 mnt]# cat check_file.sh
#!/bin/bash
if
[ -e "$1" ]
then
[ -f "$1" -a ! -L "$1" ] && echo $1 is a file
[ -b "$1" ] && echo $1 is a block
[ -c "$1" ] && echo $1 is a count
[ -d "$1" ] && echo $1 is a directory
[ -L "$1" ] && echo $1 is a lik
else
[ -n "$1"] && echo "Please input give me a file" || echo $1 is not exist
fi

### -ef -nt -ot
[root@server102 mnt]# ln -s /mnt/file /mnt/file1
[root@server102 mnt]# [ file -ef file1 ] && echo yes        ##file与file1节点相同
yes
[root@server102 mnt]# touch westos                ##westos建立时间比file晚
[root@server102 mnt]# [ file -ot westos ] && echo yes        ##file比westos建立时间早
yes
[root@server102 mnt]# [ westos -nt file ] && echo yes        ##westos比file建立时间晚
yes

#check_food.sh
$1=apple输出banana $1=banana输出apple 其他输出error
[root@server102 mnt]# vim check_food.sh
[root@server102 mnt]# cat check_food.sh
#!/bin/bash
case $1 in
    apple)
    echo banana
    ;;
    banana)
    echo apple
    ;;
    *)
    echo error
esac
测试
[root@server102 mnt]# sh check_food.sh
error
[root@server102 mnt]# sh check_food.sh apple
banana
[root@server102 mnt]# sh check_food.sh banana
apple

#ssh.exp
输入IP,密码自动ssh远程连接
[root@server102 mnt]# yum install expect -y
[root@server102 mnt]# vim ssh.exp
[root@server102 mnt]# cat ssh.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn ssh root@$IP
expect {
    "yes/no"
    {send "yes\r";exp_continue}
    "password:"
    {send "$PASS\r"}
    }
interact
[root@server102 mnt]# chmod +x ssh.exp
测试
[root@server102 mnt]# /mnt/ssh.exp 172.25.254.2 redhat
spawn ssh root@172.25.254.2
root@172.25.254.2‘s password:
Last failed login: Wed Dec 14 15:12:10 CST 2016 from www.mazha.com on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Tue Dec 13 09:22:12 2016 from foundation0.ilt.example.com
ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1481592132
[root@foundation2 ~]# ifconfig eth0
eth0: error fetching interface information: Device not found
[root@foundation2 ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.2  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::e8cd:faff:fe47:d981  prefixlen 64  scopeid 0x20<link>
        ether 00:21:cc:65:e7:b5  txqueuelen 0  (Ethernet)
        RX packets 115113  bytes 103508996 (98.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 74588  bytes 5730582 (5.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#pinghost.sh
显示教室内所有主机的主机名称
[root@server102 mnt]# cat ssh.exp
#!/usr/bin/expect
set timeout 2
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
set COMM [ lindex $argv 2 ]
spawn ssh root@$IP $COMM
expect {
    "yes/no"
    {send "yes\r";exp_continue}
    "password:"\              
    {send "$PASS\r"}
    {send "$COMM\r"}
    }
expect eof
[root@server102 mnt]# cat pinghost.sh
#!/bin/bash
for NUM in {1..33}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
    /mnt/ssh.exp 172.25.254.$NUM redhat hostname | grep -E "spawn|root" -v )
done

测试
[root@server102 mnt]# sh pinghost.sh
foundation2.ilt.example.com
foundation7.ilt.example.com
foundation25.ilt.example.com
foundation27.ilt.example.com
foundation28.ilt.example.com

###alias
[root@server102 mnt]# alias xie=vim
[root@server102 mnt]# xie file
当前用户更改
[root@server102 ~]# vim .bashrc
 alias xie=‘vim‘
[root@server102 ~]# source .bashrc
所有用户更改
[root@server102 ~]# vim /etc/bashrc
 alias xie=‘vim‘
[root@server102 ~]# source /etc/bashrc
取消更改
删掉/etc/bashrc文件中添加内容
[root@server102 ~]# unalias xie


#test.sh
执行test.sh 显示文件类型,无此文件,请输入一个文件名称
[root@server102 mnt]# vim test.sh
[root@server102 mnt]# cat test.sh
#!/bin/bash
IF () {
    if
    [ "$1"    "$2" ]
    then
    $3
    fi
}
IF -L $1 "echo $1 is a lik"
IF -f $1 "echo $1 is a file"
IF -d $1 "echo $1 is a directory"
IF -c $1 "echo $1 is a count"
IF -b $1 "echo $1 is a blockdevice"

本文出自 “12115084” 博客,请务必保留此出处http://12125084.blog.51cto.com/12115084/1883127

shell