网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
done
[root@shell day05]# sh for1.sh
[root@shell day05]# ll file/
total 0
-rw-r–r-- 1 root root 0 Jul 20 16:31 06d7415c18_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 22b6007222_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 232d120f98_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 24eb9ca27b_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 2dfb5d3996_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 6e92c961d6_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 9d73e8c661_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 9ee2ff767a_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 a94de61b66_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 ba09e7529c_oldchang.html
-rw-r–r-- 1 root root 0 Jul 20 16:31 dbd4cadf0c_oldchang.html
2.使用for循环,将上面文件名中的oldchang改为oldduan,并将扩展名全部大写
[root@shell day05]# cat for2.sh #!/bin/bash for file in `ls file/`;do mv $file ${file/oldchang.html/oldduan.HTML} &>/dev/null done [root@shell day05]# ll file/ total 0 -rw-r--r-- 1 root root 0 Jul 20 16:38 0cc1a6f122_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 0fcfd82665_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 2926da6325_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 35983a7e16_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 570c22fd5c_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 5b9c3e9607_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 5f101577d3_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 93c3445bff_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 c2bc523968_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 e148e11a25_oldchang.html -rw-r--r-- 1 root root 0 Jul 20 16:38 fa9c74b056_oldchang.html
其他方法思路
3、使用for循环和if,筛选出以下单词中字符数大于等于6的单词:I am teacher oldchang and I like eating and sleeping
[root@shell day05]# cat for-ip.sh #!/bin/bash for ((i=1;i<255;i++));do ping -c 1 -w 1 10.0.0.$i &>/dev/null if [ $? -eq 0 ];then echo "10.0.0.$i 可以ping通!" else echo "10.0.0.$i 不可以ping通!" fi done wait [root@shell day05]# sh for-ip.sh 10.0.0.1 可以ping通! 10.0.0.21 可以ping通! ^Z [4]+ Stopped sh for-ip.sh
4、for循环打印99乘法表
[root@shell lianxi]# cat for99-1.sh #!/bin/bash #倒三角排序 for ((i=9 ;i>=1;i--));do for ((j=1;j<=i;j++));do echo -en "${j}x$i=$(($i\*$j))\t" #第一种方法计算 done echo done #正三角排序 for ((i=1;i<=9;i++));do for ((j=1;j<=i;j++));do echo -en "${j}x$i=`expr $i\*$j|bc`\t" #第二种方法计算 done echo done [root@shell lianxi]# sh for99-1.sh 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x4=4 2x4=8 3x4=12 4x4=16 1x3=3 2x3=6 3x3=9 1x2=2 2x2=4 1x1=1 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
5.给出一个网段,如10.0.0.0/24,如何判断网段内的所有ip是否能联通
[root@shell day05]# cat for-ip.sh #!/bin/bash ############################################################## # File Name: for\_ip.sh # Version: V1.0 # Author: lcx # Organization: www.oldboyedu.com ############################################################## #for i in 10.0.0.{1..254} #do # ping -c 2 -w 2 $i &>/dev/null # if [ $? -eq 0 ];then # echo $i 可以ping通! # else # echo $i 不可以ping通 &>/dev/null # fi #done for ((i=1;i<255;i++));do ping -c 1 -w 1 10.0.0.$i &>/dev/null if [ $? -eq 0 ];then echo "10.0.0.$i 可以ping通!" else echo "10.0.0.$i 不可以ping通!" fi done wait [root@shell day05]# sh for-ip.sh 10.0.0.1 可以ping通! 10.0.0.21 可以ping通! ^Z [4]+ Stopped sh for-ip.sh
2.循环的流程控制
break、continue、exit、return
2.1 break
[root@shell day05]# vim break.sh
1 #!/bin/bash
2 ##############################################################
3 # File Name: break.sh
4 # Version: V1.0
5 # Author: lcx
6 # Organization: www.oldboyedu.com
7 ##############################################################
8 for ((i=0;i<10;i++));do
9 echo $i
10 if [ $i -eq 3 ];then
11 break
12 fi
13 done
[root@shell day05]# sh break.sh
0
1
2
3
2.2 contunue
[root@shell day05]# cat break.sh
#!/bin/bash
##############################################################
# File Name: break.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
for ((i=0;i<10;i++));do
echo $i
if [ $i -eq 3 ];then
continue
fi
done
echo "hello"
[root@shell day05]# sh break.sh
0
1
2
3
4
5
6
7
8
9
hello
流程控制的测试脚本
[root@shell day05]# vim for-bcer.sh
#!/bin/bash
##############################################################
# File Name: for-bcer.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
#############################################################
#循环流程控制break、continue、exit、return的区别和对比
if [ $# -ne 1 ];then
# 如果传参个数部位1,则打印下面的使用提示,提示四个命令作为参数
echo $"usage:$0 {break|continue|exit|return}"
exit 1 #退出脚本
fi
function test(){
for((i=0;i<5;i++));do
if [ $i -eq 3 ];then
$\*; #"$\*" 就是接收函数以外的参数,是{break|continue|exit|return}其中一个
fi
echo $i
done
echo "I am in func." #函数内循环外的输出提示
}
test $\* # 这里的$\*为参数的传参
func_ret=$? # 接收并测试函数返回值
if [ `echo $\*|grep return|wc -l` -eq 1 ];then # 如果传参中含有return
echo "return exit status: $func\_ret" # 则提示return退出的状态码
fi
echo "OK"
执行结果
[root@shell day05]# sh for-bcer.sh
usage:for-bcer.sh {break|continue|exit|return}
[root@shell day05]# sh for-bcer.sh break
0
1
2
I am in func.
OK
[root@shell day05]# sh for-bcer.sh continue
0
1
2
4
I am in func.
OK
[root@shell day05]# sh for-bcer.sh exit
0
1
2
[root@shell day05]# sh for-bcer.sh return
0
1
2
return exit status: 0
OK
最全的Linux教程,Linux从入门到精通
======================
-
linux从入门到精通(第2版)
-
Linux系统移植
-
Linux驱动开发入门与实战
-
LINUX 系统移植 第2版
-
Linux开源网络全栈详解 从DPDK到OpenFlow
第一份《Linux从入门到精通》466页
====================
内容简介
====
本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。
本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。
需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!