shell for 循环实践
Posted 山外云
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell for 循环实践相关的知识,希望对你有一定的参考价值。
for循环语法结构如下
[root@backup tmp]# cat 1.sh #!/bin/bash for i in `seq 3` do echo $i done [root@backup tmp]# sh 1.sh 1 2 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 循环实践的主要内容,如果未能解决你的问题,请参考以下文章
shell编程 10 --- while 和until 循环的应用实践