for循环语法结构如下

[root@backup tmp]# cat .sh
#!/bin/bash
for i in `seq `
do
echo $i
done
[root@backup tmp]# sh .sh 3 批量修改文件名

[root@backup tmp]# ll *.txt
-rw-r--r-- 1 root root 0 May 11 21:34 1.txt
-rw-r--r-- 1 root root 0 May 11 21:34 2.txt
-rw-r--r-- 1 root root 0 May 11 21:34 3.txt
-rw-r--r-- 1 root root 0 May 11 21:34 4.txt
-rw-r--r-- 1 root root 0 May 11 21:34 5.txt
[root@backup tmp]#
[root@backup tmp]# cat 2.sh
#!/bin/bash
cd /tmp
for filename in `ls|grep "txt$"`
do
mv $filename `echo $filename |cut -d . -f1`.gif


done
[root@backup tmp]# sh 2.sh
[root@backup tmp]# ll
total 8
-rw-r--r-- 1 root root 0 May 11 21:34 1.gif
-rw-r--r-- 1 root root 49 May 11 21:33 1.sh
-rw-r--r-- 1 root root 0 May 11 21:34 2.gif
-rw-r--r-- 1 root root 115 May 11 21:39 2.sh
-rw-r--r-- 1 root root 0 May 11 21:34 3.gif
-rw-r--r-- 1 root root 0 May 11 21:34 4.gif
-rw-r--r-- 1 root root 0 May 11 21:34 5.gif

create table t1 (id int(4),name varchar(10));

insert into t1(id,name) values (1,'martin');

分库备份mysql数据库

[root@lnmp01 ~]# cat 1.sh
#!/bin/bash
PATH="application/mysql/bin:$PATH"
DBPATH=/server/backup
MYSQLROOT=root
MYPASS=hello123
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYSQLROOT -p$MYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYSQLROOT -p$MYPASS -S $SOCKET"

[ ! -d $DBPATH ]&& mkdir -p $DBPATH

for dbname in `$MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|schema"` #获取数据库名
do
$MYDUMP $dbname |gzip >$DBPATH/${dbname}_$(date +%F).sql.gz
done
[root@lnmp01 ~]#
[root@lnmp01 ~]# sh 1.sh
[root@lnmp01 ~]# ll /server/backup/
total 12
-rw-r--r-- 1 root root 680 Sep 20 12:16 discusz_2017-09-20.sql.gz
-rw-r--r-- 1 root root 7693 Sep 20 12:16 wordpress_2017-09-20.sql.gz

shell for 循环实践的更多相关文章

  1. Linux 之Shell for循环

    @代表所有参数所以如果后面跟上echo $v你会发现他会一次显示user userdebug eng $poo -le ${#prodlist[@]} 这句话是说 $poo小于等于prodlist中的 ...

  2. Shell脚本最佳实践

    Shell脚本最佳实践 0. 编码.缩进.文件命名和权限设置等 使用utf-8编码: 统一使用tab缩进或空格缩进,不要混用: 文件名以.sh结尾,并且统一风格: 添加可执行权限: chmod +x ...

  3. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

  4. shell for循环+case的脚本(监控程序状态)

    分享一个shell for循环+case的脚本(监控程序状态) 分享一个for循环+case的脚本(监控程序状态并执行相关操作) ,供大家学习参考. 复制代码代码如下: #/bin/bash set ...

  5. shell 各种循环判断

    shell支持的循环有 Shell if else Shell case esac Shell for循环 Shell while循环 Shell until循环

  6. shell脚本--循环结构

    shell的循环结构有while和for两种 for循环 #!/bin/bash #文件名:test.sh i=4 for i in 2 4 6 8 10 do echo $i done echo $ ...

  7. Linux Shell系列教程之(十一)Shell while循环

    本文是Linux Shell系列教程的第(十一)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上一篇Linux Shell系列教程之(十)Shell for循环中,我们已经 ...

  8. Linux Shell系列教程之(十)Shell for循环

    本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shel ...

  9. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

随机推荐

  1. Bootstrap历练实例:响应式布局

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Spring Boot 2.x零基础入门到高级实战教程

    一.零基础快速入门SpringBoot2.0 1.SpringBoot2.x课程全套介绍和高手系列知识点 简介:介绍SpringBoot2.x课程大纲章节 java基础,jdk环境,maven基础 2 ...

  3. @property 关键字的区别详解

    @property 关键字的区别详解:assign 与weak. __block 与 __weak.strong 与copy的区别 字数911 阅读269 评论2 喜欢8 在开发中经常使用属性@pro ...

  4. macOS如何正确驱动集成显卡HDMI(包括视频和音频)

    聊聊如何正确驱动集成显卡HDMI(包括视频和音频)必备条件:1.必须使用AppleHDA驱动声卡(仿冒.clover.applealc都可以的),使用voodoo驱动声卡应该不行的.2.dsdt或者s ...

  5. JavaScript 字符串分行、Return 语句使用注意事项

    JavaScript 字符串分行 JavaScript 允许我们在字符串中使用断行语句: var x ="Hello World!"; 但是,在字符串中直接使用回车换行是会报错的: ...

  6. Linux命令之---cat

    命令简介 cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 命令格式 cat [选项] [文 ...

  7. hdu1950Bridging signals(求最长上升自序列nlogn算法)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Redis实现之RDB持久化(二)

    RDB文件结构 在Redis实现之RDB持久化(一)这一章中,我们介绍了Redis服务器保存和载入RDB文件的方法,在这一节,我们将对RDB文件本身进行介绍,并详细说明文件各个部分的结构和意义.图1- ...

  9. pycharm中某些方法被标黄的原因及解决办法

    在编辑python文件时,会遇到上图所示,函数方法被标黄的问题,但是不影响使用. 引起原因:,如果不报错说明,这是因为你配置的python解释器中有该方法,但是pycharm没有找到这个方法,即加载失 ...

  10. laravel5.2总结--路由

      1 基本路由   1.1 定义路由的文件 app/Http/routes.php   1.2 最基本的路由: Route::get(''index", function () { ret ...