Subtract two variables in Bash

后端 未结 8 1946
时光取名叫无心
时光取名叫无心 2020-12-02 06:33

I have the script below to subtract the counts of files between two directories but the COUNT= expression does not work. What is the correct syntax?

<         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 07:15

    Use Python:

    #!/bin/bash
    # home/victoria/test.sh
    
    START=$(date +"%s")                                     ## seconds since Epoch
    for i in $(seq 1 10)
    do
      sleep 1.5
      END=$(date +"%s")                                     ## integer
      TIME=$((END - START))                                 ## integer
      AVG_TIME=$(python -c "print(float($TIME/$i))")        ## int to float
      printf 'i: %i | elapsed time: %0.1f sec | avg. time: %0.3f\n' $i $TIME $AVG_TIME
      ((i++))                                               ## increment $i
    done
    
    

    Output

    $ ./test.sh 
    i: 1 | elapsed time: 1.0 sec | avg. time: 1.000
    i: 2 | elapsed time: 3.0 sec | avg. time: 1.500
    i: 3 | elapsed time: 5.0 sec | avg. time: 1.667
    i: 4 | elapsed time: 6.0 sec | avg. time: 1.500
    i: 5 | elapsed time: 8.0 sec | avg. time: 1.600
    i: 6 | elapsed time: 9.0 sec | avg. time: 1.500
    i: 7 | elapsed time: 11.0 sec | avg. time: 1.571
    i: 8 | elapsed time: 12.0 sec | avg. time: 1.500
    i: 9 | elapsed time: 14.0 sec | avg. time: 1.556
    i: 10 | elapsed time: 15.0 sec | avg. time: 1.500
    $
    
    • Get current time in seconds since the Epoch on Linux, Bash

提交回复
热议问题