Python 官方文档:入门教程 => 点击学习
(1)打印乘法口诀表#!/bin/sh for i in {1..9} #取1-9的数字 do for n in {1..9} #取1-9的数字 do [ $i -ge $n ] && echo -n "${i}x${
(1)打印乘法口诀表
#!/bin/sh
for i in {1..9} #取1-9的数字
do
for n in {1..9} #取1-9的数字
do
[ $i -ge $n ] && echo -n "${i}x${n}=$((i*n)) " #如果$i大于等于$n,则输出。
done
echo " "
done
效果:
[root@shell day3]# sh for9X9.sh
1x1=1
2x1=2 2x2=4
3x1=3 3x2=6 3x3=9
4x1=4 4x2=8 4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
(2)批量添加用户及随机密码
#!/bin/sh
for i in `seq -w 10`
do
user=oldboy-$i #定义用户
pass=`echo $RANDOM|md5sum |cut -c 1-8 ` #定义密码,取8位数。这里的密码即为随机的字母和数字的组合。可搜索$RANDOM环境变量及md5sum命令详解。
echo "user:$user passWord:$pass" >>/tmp/user.txt #将账号和密码输入到一个文件内,以便查询。
useradd $user && echo "$pass" |passwd --stdin $user #无交互的添加密码。
done
效果:
[root@shell day3]# cat /tmp/user.txt
user:oldboy-01 password:d4198542
user:oldboy-02 password:6e025742
user:oldboy-03 password:1a94ac35
user:oldboy-04 password:594bb055
user:oldboy-05 password:fcecc05c
user:oldboy-06 password:3957ee7a
user:oldboy-07 password:1454a6e7
.............................................................
(3)Mysql分库分表备份
分库备份:
#!/bin/sh
MYUSER=root #mysql的用户
MYPASS=123456 #mysql root用户的密码
MYCMD="mysql -u$MYUSER -p$MYPASS" #定义了mysql命令
MYDUMP="mysqldump -u$MYUSER -p$MYPASS" #定义了mysqldump命令
DATABASE=`$MYCMD -e "show databases;"|sed '1d'|egrep -v "mysql|perfORMance_schema|information_schema"` #获取到数据库名
for database in $DATABASE
do
$MYDUMP -B $database|gzip >/server/backup/${database}_$(date +%F).sql.gz #for循环分库备份
done
分表备份:
#!/bin/sh
MYUSER=root
MYPASS=123456
MYCMD="mysql -u$MYUSER -p$MYPASS"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS"
DATABASE=`$MYCMD -e "show databases"|sed '1d'|egrep -v "mysql|performance_schema|information_schema"`
for database in $DATABASE
do
[ ! -d /server/backup/$database ] && mkdir -p /server/backup/$database #创建以数据库名命名的目录
TABLE=`$MYCMD -e "show tables from $database;"|sed '1d'` #获取表名
for table in $TABLE
do
$MYDUMP $database $table |gzip >/server/backup/$database/${database}_${table}_$(date +%F).sql.gz #for循环分表备份
done
done
分表备份效果:
[root@shell backup]# ll
总用量 64
drwxr-xr-x. 2 root root 24576 6月 6 16:00 bbs
drwxr-xr-x. 2 root root 4096 6月 6 16:00 blog
drwxr-xr-x. 2 root root 4096 6月 6 16:00 lee_default
drwxr-xr-x. 2 root root 4096 6月 6 16:00 lee_gbk
drwxr-xr-x. 2 root root 4096 6月 6 16:00 lee_utf8
drwxr-xr-x. 2 root root 4096 6月 6 16:00 liyiliang
drwxr-xr-x. 2 root root 4096 6月 6 16:00 test
drwxr-xr-x. 2 root root 4096 6月 6 16:00 test1
drwxr-xr-x. 2 root root 12288 6月 6 16:00 www
[root@shell backup]# tree #部分数据
................................................
x tqq bbs_pre_ucenter_pm_messages_8_2014-06-06.sql.gz
x tqq bbs_pre_ucenter_pm_messages_9_2014-06-06.sql.gz
x tqq bbs_pre_ucenter_protectedmembers_2014-06-06.sql.gz
x tqq bbs_pre_ucenter_settings_2014-06-06.sql.gz
x tqq bbs_pre_ucenter_sqlcache_2014-06-06.sql.gz
x tqq bbs_pre_ucenter_tags_2014-06-06.sql.gz
x MQq bbs_pre_ucenter_vars_2014-06-06.sql.gz
tqq blog
x tqq blog_wp_commentmeta_2014-06-06.sql.gz
x tqq blog_wp_comments_2014-06-06.sql.gz
x tqq blog_wp_links_2014-06-06.sql.gz
x tqq blog_wp_options_2014-06-06.sql.gz
x tqq blog_wp_postmeta_2014-06-06.sql.gz
x tqq blog_wp_posts_2014-06-06.sql.gz
x tqq blog_wp_term_relationships_2014-06-06.sql.gz
x tqq blog_wp_terms_2014-06-06.sql.gz
x tqq blog_wp_term_taxonomy_2014-06-06.sql.gz
x tqq blog_wp_usermeta_2014-06-06.sql.gz
x mqq blog_wp_users_2014-06-06.sql.gz
tqq lee_default
tqq lee_gbk
tqq lee_utf8
x tqq lee_utf8_leesir_2014-06-06.sql.gz
x mqq lee_utf8_test_2014-06-06.sql.gz
tqq liyiliang
.................................................
--结束END--
本文标题: shell之for循环的3个简单脚本
本文链接: https://lsjlt.com/news/189582.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0