-1

I'm having the following error:

bash power.sh  date: extra operand ā€˜%s’ Try 'date --help' for more
information. power.sh: line 16: [1578180864: command not found
RAIL,POWER_AVG power.sh: line 27: 0 /0: division by 0 (error token is
"0")

or sh power.sh: 5: power.sh: Syntax error: "(" unexpected

On the follwoing script:

#!/bin/bash   
# to measure average power consumed in 30sec with 1sec sampling interval   
duration=30   
interval=1   
RAILS=("VDD_IN /sys/bus/i2c/drivers/ina3221x/6-0040/in_power0_input"
"VDD_SYS_GPU /sys/bus/i2c/drivers/ina3221x/6-0040/iio:device/in_power0_input"
"VDD_SYS_CPU /sys/bus/i2c/drivers/ina3221x/6-0040/iio:device/in_power1_input"
"VDD_SYS_SOC /sys/bus/i2c/drivers/ina3221x/6-0040/iio:device/in_power1_input"
"VDD_SYS_DDR /sys/bus/i2c/drivers/ina3221x/6-0040/iio:device/in_power2_input"
"VDD_4V0_WIFI /sys/bus/i2c/drivers/ina3221x/0-0040/iio:device/in_power2_input")

for ((i = 0; i < ${#RAILS[@]}; i++)); 
 do read name[$i] node[$i] pwr_sum[$i] pwr_count[$i]<<<$(echo "${RAILS[$i]} 0 0")  
 done  
 end_time=$(($(date + '%s')+duration))  
 while [$(date +'%s') -le $end_time]; 
  do  
    for ((i =0; i < ${#RAILS[@]};i++)); do  
   pwr_sum[$i]=$((${pwr_sum[$i]} + $(cat ${node[$i]}))) && 
   pwr_count[$i]=$((${pwr_count[$i]} +1))  
  done  
 sleep $interval  

done  
  echo "RAIL,POWER_AVG";   
for ((i =0; i< ${#RAILS[@]}; i++)); do  
  pwr_avg=$((${pwr_sum[$i]} /${pwr_count[$i]}))  
  echo "${name[$i]},$pwr_avg"   
done

I've used it with sucess before, what did change? I've tried to call it both with bash and sh but dunno what is wrong.

vfbsilva
  • 135
  • 8

1 Answers1

0

This line:

 end_time=$(($(date + '%s')+duration)) 

shoud be

 end_time=$(($(date +'%s')+duration)) 

(one space more)

Also this:

 while [$(date +'%s') -le $end_time]; 

need to be

 while [ $(date +'%s') -le $end_time ]; 

(here you need space after and before the brackets)

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31