shell脚本 案例

1,写一个循环,ping整个子网的ip。
 
使用while循环
#!/bin/bash 
ip=223 
while [ "$ip" -ne "239" ]
do
        ping 121.201.0.$ip -c1 && echo "121.201.0.$ip yes" >> ip_list.txt || echo "121.201.0.$ip no" >> ip_list.txt
((ip+=1))
done
 
使用for循环(推荐)
#!/bin/bash
for ip in 192.168.1.{100..255} 
do
        ping $ip -c 1 && echo $ip yes >> ./check_ip_network.txt|| echo $ip no >> ./check_ip_network.txt
done
 

2, 9*9乘法表

#!/bin/bash

for a in {1..9}
do
        for b in {1..9}
        do
                [ $a -ge $b ] && echo -en "$a x $b = $(expr $a * $b) "
        done
echo ""
done
 
[root@shell shell]# sh 9x9.sh
1 x 1 = 1 
2 x 1 = 2 2 x 2 = 4 
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 
6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
 

3. shell跳板机(触发信号后屏蔽信号)

方法1:
1)首先做好ssh key验证,做ssh key免登陆验证
2)实现传统的远程连接菜单选择脚本
3)利用linux信号防止用户在跳板机上操作。
4)用户登陆后既调用脚本。
 
代码如下:
[root@shell practical_script]# cat tiaoban.sh 
#!/bin/bash

# function
function trapper ()
{
        trap ':' INT EXIT TSTP TERM HUP
}
while :
do
        trapper
                clear
                        cat <<menu
# This is a springboard machine, can only be through the springboard 
# machine landing the following system.


        Please select the server you want to log on:

                                1) link 126.201.20.132(TT3)
                                2) exit
menu

read -p "pleas select: " num
case "$num" in
        1)
                echo "126.201.20.132(TT3) landing ..."
                ssh -p 26333 xxllen@126.201.20.132
                ;;
        2|*)
                exit
                ;;

esac
done
 
方法2: root链接服务器,expect 每次重新建立ssh key
 
###########################################################################################
4. Shell编程之LAMP一键安装脚本实战
[root@shell shell]# cat autoLAMP.sh 
#!/bin/bash
#auto make install LAMP
#by auto ly 2015

# httpd define path varible
H_FILES=httpd-2.2.31.tar.bz2
H_FILES_DIR=httpd-2.2.31
H_URL=http://mirrors.cnnic.cn/apache/httpd/
H_PREFIX=/usr/local/apache2/

M_FILES=mysql-5.0.41.zip
M_FILES_DIR=mysql-5.0.41
M_URL=http://gyconfigfc.attogames.com/
M_PREFIX=/usr/local/mysql/

# PHP define path variable
P_FILES=php-5.3.28.tar.bz2
P_FILES_DIR=php-5.3.28
P_URL=http://mirrors.sohu.com/php/
P_PREFIX=/usr/local/php5/


if [ -z "$1" ];then
        echo -e "33[36mPlease Select Install Menu follow:33[0m"
        echo -e "33[32m1)Compile and install Apache server:33[1m"
        echo "2)Compile and install MySQL server"
        echo "3)Compile and install PHP server"
        echo "4)Configure index.php and start the LAMP service"
        echo -e "33[31mUsage: {$0 1 | 2 | 3 | 4 | help}33[0m"
        exit
fi

if [ $1 -eq 1 ];then
        wget -c $H_URL/$H_FILES && tar -jxvf $H_FILES && cd $H_FILES_DIR ;./configure --prefix=$H_PREFIX

        if [ $? -eq 0 ];then
                make && make install
                echo -e "33[32mThe $H_FILES_DIR Server Install Successfully!33[0m"
        else
                echo -e "33[31mThe $H_FILES_DIR Server Install Failed,Please check ...33[0m"
        fi
fi

# auto install Mysql
if [ $1 -eq 2 ];then
        wget -c $M_URL/$M_FILES && unzip $M_FILES && cd $M_FILES_DIR ;chmod +x configure && chmod +x install-sh;./configure --prefix=$M_PREFIX

        if [ $? -eq 0 ];then
                make && make install
                echo -e "33[32mThe $M_FILES_DIR Server Install Successfully!33[0m"
        else
                echo -e "33[31mThe $M_FILES_DIR Server Install Failed,Please check ...33[0m"
        fi
fi

# auto install PHP Server
if [ $1 -eq 3 ];then
        wget -c $P_URL/$P_FILES && tar -jxvf $P_FILES && cd $P_FILES_DIR ;./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=$M_PREFIX --with-apxs2=/usr/local/apache2/bin/apxs

        if [ $? -eq 0 ];then
                make && make install
                echo -e "33[32mThe $P_FILES_DIR Server Install Successfully!33[0m"
        else
                echo -e "33[31mThe $P_FILES_DIR Server Install Failed,Please check ...33[0m"
        fi
fi

if [ $1 -eq 4 ];then
        sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf
        $H_PREFIX/bin/apachectl restart
        echo "AddType   application/x-httpd-php .php" >> $H_PREFIX/conf/httpd.conf
        IP=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`
        echo "You can access http://$IP/"

cat >$H_PREFIX/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
fi
################################################################################################
 
原文地址:https://www.cnblogs.com/rayong/p/7267755.html