zoukankan      html  css  js  c++  java
  • Shell编程进阶篇(完结)

    for循环语句、

    在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行。

         它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变数。这使得for循环能够知道在迭代过程中的执行顺序。

    1.1.1 shell中的for循环

             shell中的for 循环与在c中不同,它包含三种形式:第一种结构是列表for 循环;第二种结构就是不带列表的for循环;第三种就类似于C语言。

    ①   列表for循环(常用)

    #!/bin/bash
    for i in 取值列表
    do
        循环主体/命令
    done事挑衅

    ②   不带列表for循环(示例)

    #!/bin/absh
    echo "刘军军的博客是:"  
    for i 
         do   
         echo "$i" 
    done 

       脚本执行结果

    [root@clsn for]# sh  for2.sh http://blog.znix.top
    刘军军的博客是:
    http://blog.znix.top
     

    ③   类似C语言的风格这种用法常在C语语言中使用)

    for((exp1;exp2;exp3))
        do
          指令...
    done   

             编写类似C语言风格脚本

    for((i=0;i<=3;i++))
        do
          echo $i
    done  

             脚本执行过程

    不同语言的For循环

    Shell中的两种样式

    # 样式一:
    for i in 1 2 3 
      do 
        echo $i
    done
    # 样式二:
    for i in 1 2 3;do  echo $i;done

      JAVA

    for(int i = 0; i < 5; i++){
        //循环语句;
    }

      PHP

    for ($i = 0; $i < 5; $i++) {
      # statements;
    }

      VB

    For i = 1 To 5
    ===PASCAL===
    for not i=1 do
    begin
       i=0;
       writeln('Go on!');
    end.
       
      '循环语句
    Next i

      swift

    var x = 0
    for i in 1...100{
        x += i
    }
    print(x)
    
    //5050
    for _ in 1...100{
        x += 1
    }
    print(x)
    // 100
    
    var box = [1,2,3,4,5]
    for i in box{
        print(i)
    }
    /*
    1 
    2 
    3 
    4 
    5
    */
    ---

     for循环相关练习题

    练习题1】批量生成随机字符文件名案例

    使用for循环在/clsn目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串clsn,名称示例如下:

    [root@ljj scripts]# vi make_file.sh

    #!/bin/bash
    #############################################################
    # File Name: make_file.sh
    # Version: V.
    # Author: clsn
    # Organization: http://blog.znix.top
    # Created Time : -- ::
    # Description:
    #############################################################

    [ -d /clsn ] || mkdir -p /clsn
    rpm -qa |grep pwgen &>/dev/null
    if [ $? -eq 1 ]
    then
    yum install pwgen -y &>/dev/null
    fi

    cd /clsn &&
    for i in {1..10}
    do
    File_Name=`uuidgen |tr "0-9-" "a-z"|cut -c 1-10`
    File_Name2=`pwgen -1A0 10`
    touch ${File_Name}_clsn.html
    done

    [root@ljj scripts]# ls -l /clsn/
    总用量 0
    -rw-r--r--. 1 root root 0 11月 15 17:36 acacafidkd_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 bacedaejkg_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 bfdghfafkj_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 bjbhdeadkb_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 cfehdjbdkf_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 dcbagadakb_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 dhafjdaekg_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 ecbbidhhkc_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 icacgcccka_clsn.html
    -rw-r--r--. 1 root root 0 11月 15 17:36 iccifecdka_clsn.html

  • 相关阅读:
    DIY 作品 及 维修 不定时更新
    置顶,博客中所有源码 github
    openwrt PandoraBox PBR-M1 极路由4 HC5962 更新固件
    使用 squid 共享 虚拟专用网至局域网
    第一次参加日语能力测试 N5
    libx264 libfdk_aac 编码 解码 详解
    开发RTSP 直播软件 H264 AAC 编码 live555 ffmpeg
    MFC Camera 摄像头预览 拍照
    http2 技术整理 nginx 搭建 http2 wireshark 抓包分析 server push 服务端推送
    plist 图集 php 批量提取 PS 一个个切
  • 原文地址:https://www.cnblogs.com/liujunjun/p/11867300.html
Copyright © 2011-2022 走看看