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 [email protected]
[email protected]'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"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章