zoukankan      html  css  js  c++  java
  • while loop, for loop

    1. create a function to generate file with the filename start with 10 random characters

    
    
    #!/bin/bash
    WORK_DIR=/tmp/00
    #define the function
    create(){
        i=1
        while [ $i -le 5 ]
        do
           if [ ! -d $WORK_DIR ]
           then
              mkdir -p $WORK_DIR
              cd $WORK_DIR && touch `</dev/urandom tr -dc "a-z" | head -c 10`_test.txt
           else
              cd $WORK_DIR && touch `</dev/urandom tr -dc "a-z" | head -c 10`_test.txt
           fi
           i=$(( $i+1 )) 
         done
    }
    # invoke the function   
    create
    

    2. Using for loop to write a function to rename file in batch. 

    #!/bin/bash
    
    changeName(){
      DIR=/tmp/test01
      FILE=`ls $DIR`
      postfix=a.txt
      for i in $FILE
      do
        c=`echo $i |cut -c 1-10`
        mv $DIR/$i $DIR/$c$postfix
        echo "The file  $i was renamed to $c$postfix" 
      done
    
    }
    changeName
    

    3. using for loop print the odd number between 1 - 100

    for((i=1;i<=100;i++))
    do
      if ((i%2==1))
      then
        echo $i
        continue
      fi
    done
    

    4.  using while loop print the odd number between 1 - 100

    i=1
    while(($i<=100))
    do
        if(($i%2==1))
        then
            echo $i
        fi
        i=$(($i+1))
    done
    

    5.  Another way to print the odd number

    for i in {1..100..2}
    do
       echo $i
    done
    

    6. print the file name for a folder

    for f  in `ls ~`
    do 
              name=`echo "$f" | awk -F. '{print $1}'`           
              echo $name
    done
    

      

      

      

      

  • 相关阅读:
    Docker宿主机管理
    Docker常用命令
    Maven专题4——Maven测试
    Spring Boot 2.x 之 Logging
    spark高可用集群搭建立
    elastic插件安装
    单实例安装elastic和启动报错解决
    使用Turbine对集群进行监控
    Centos安装mysql5.6.33
    Centos6安装破解JIRA7.3.8
  • 原文地址:https://www.cnblogs.com/amy2012/p/11627860.html
Copyright © 2011-2022 走看看