day 17

例题26

[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 26.sh
#检测MySQL服务是否正常(比如,可以正常进入mysql执行show processlist),
#并检测一下当前的MySQL服务是主还是从,如果是从,请判断它的主从服务是否异常。如果是主,则不需要做什么
#!/bin/bash
mysql="/usr/local/mysql/bin/mysql -uroot -p123456"
if ! $mysql -e "show processlist" >/dev/null 2>/dev/null 
then
    echo "MySQL service is down."
    exit
else
    $mysql -e "show slave status\G" 2>/dev/null >/tmp/slave.stat
    n=`wc -l /tmp/slave.stat|awk '{print $1}'`
    if [ $n -eq 0 ]
    then
        echo "This is master."
    else
        echo "This is slave."
        egrep 'Slave_IO_Running:|Slave_SQL_Running:'/tmp/slave.stat|awk -F ': ' '{print $2}' > /tmp/SQL.tmp
        if grep -qw "No" /tmp/SQL.tmp 
        then
            echo "The slave is down."
        fi
    fi
fi

例题27

[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 27.sh
#只支持三个选项:'--del','--add','--help',输入其他选项报错。
#使用'--add'时,需要验证用户名是否存在,存在则反馈存在,且不添加。 不存在则创建该用户,需要设置与该用户名相同的密码。
#使用'--del'时,需要验证用户名是否存在,存在则删除用户及其家目录。不存在则反馈该用户不存在。 
#--help选项反馈出使用方法。
#能用echo $?检测脚本执行情况,成功删除或添加用户为0,不成功为非0正整数。
#能以英文逗号分割,一次性添加或者删除多个用户。例如 adddel.sh --add user1,user2,user3
#!/bin/baash
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
    echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
    exit
fi
ex_user()
{
    if ! id $1 2>/dev/null >/dev/null
    then
        useradd $1 && echo "$1 add successful."
    else
        echo $1 exist.
    fi
}
notex_user()
{
    if id $1 2>/dev/null >/dev/null
    then
        userdel $1 && echo "$1 delete successful."
    else
        echo $1 not exist.
    fi
}
case $1 in
    --add)
        if [ $# -eq 1 ]
        then
            echo "Wrong, use bash $0 --add user or bash $0 --add user1,user2,user3..."
            exit
        else
            n=`echo $2| awk -F ',' '{print NF}'`
            if [ $n -gt 1 ]
            then
                for i in `seq 1 $n`
                do
                    username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`
                    ex_user $username
                done
            else
                ex_user $2
            fi
        fi
        ;;
    --del)
        if  [ $# -eq 1 ]
        then
            echo "Wrong, use bash $0 --del user or bash $0 --del user1,user2,user3..."
            exit
        else
            n=`echo $2| awk -F ',' '{print NF}'`
            if [ $n -gt 1 ]
            then
                for i in `seq 1 $n`
                do
                    username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`
                    notex_user $username
                done
            else
                notex_user $2
            fi
        fi
        ;;
    --help)
        if  [ $# -ne 1 ]
        then
            echo "Wrong, use bash $0 --help"
            exit
        else

        echo "Use bash $0 --add username or bash $0 --add user1,user2,user3... add user."
        echo "    bash $0 --del username -r bash $0 --del user1,user2,user3... delete user."
        echo "    bash $0 --help print this info."
        fi
    ;;
    *)
        echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
    ;;
esac

例题28

[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 28.sh
#计算100以内所有能被3整除的正整数的和
#!/bin/bash
sum=0
for i in `seq 1 100`
do
    j=$[$i%3]
    if [ $j -eq 0 ]
    then
        sum=$[$sum+$i]
    fi
done
echo $sum

~           

例题29

[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 29.sh
#脚本需判断提供的两个数字必须为整数
#当做减法或者除法时,需要判断哪个数字大,减法时需要用大的数字减小的数字,除法时#需要用大的数字除以小的数字,并且结果需要保留两个小数点。
#!/bin/bash
is_nu()
{
    n=`echo $1 |sed 's/[0-9]//g'`
    if [ -n "$n" ]
    then
        echo "给出的参数必须是正整数"
        exit
    fi
}
if [ $# -ne 2 ]
then
    echo "必须要输入两个参数"
    exit
else
    is_nu $1
    is_nu $2
fi

big()
{
    if [ $1 -gt $2 ]
    then
        echo $1
    else
        echo $2
    fi
}

small()
{
    if [ $1 -lt $2 ]
    then
        echo $1
    else
        echo $2
    fi
}

add()
{
    sum=$[$1+$2]
    echo "$1+$2=$sum"
}

jian()
{
   b=`big $1 $2`
   s=`small $1 $2`
   cha=$[$b-$s]
   echo "$b-$s=$cha"
}

cheng()
{
    ji=$[$1*$2]
    echo "$1x$2=$ji"
}
chu()
{
   b=`big $1 $2`
   s=`small $1 $2`
   v=`echo "scale=2;$b/$s"|bc`
   echo "$b/$s=$v"
}

add $1 $2
jian $1 $2
cheng $1 $2
chu $1 $2

例题30

[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 30.sh
#要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。
#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "请输入一个纯数字."
        continue
    fi
    if echo $n |grep -qi 'end'
    then
        exit
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
        echo "请输入一个纯数字."
        continue
    else
        echo "你输入的数字是: $n"
        continue
    fi
done

 

转载于:https://www.cnblogs.com/huzhendong/p/10247374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Day17 中,我们可以通过 Flask 框架快速搭建一个 BBS 论坛。具体步骤如下: 1. 创建 Flask 应用 ```python from flask import Flask app = Flask(__name__) ``` 2. 创建数据库 ```python from flask_sqlalchemy import SQLAlchemy app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bbs.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) ``` 3. 创建数据库模型 ```python class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(20), nullable=False) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) ``` 4. 创建路由和视图函数 ```python @app.route('/') def index(): posts = Post.query.all() return render_template('index.html', posts=posts) @app.route('/post/<int:post_id>') def post(post_id): post = Post.query.get(post_id) return render_template('post.html', post=post) @app.route('/new_post', methods=['GET', 'POST']) def new_post(): if request.method == 'POST': title = request.form['title'] content = request.form['content'] user_id = 1 # 假设当前用户为 id 为 1 的用户 post = Post(title=title, content=content, user_id=user_id) db.session.add(post) db.session.commit() return redirect('/') return render_template('new_post.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User.query.filter_by(username=username, password=password).first() if user: session['user_id'] = user.id return redirect('/') else: flash('用户名或密码错误') return render_template('login.html') @app.route('/logout') def logout(): session.pop('user_id', None) return redirect('/') ``` 5. 创建 HTML 模板 创建 index.html、post.html、new_post.html、login.html 四个模板文件,并且使用 jinja2 模板引擎渲染数据。 6. 运行应用 ```python if __name__ == '__main__': app.run() ``` 以上就是快速搭建 BBS 论坛的主要步骤,当然在实际应用中还需要考虑更多细节问题,比如用户认证、数据校验、页面美化等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值