6

I got some code above which was into a pdf document, wherever I try to post it indentation is terrible, I would like to get it to a proper formatting for debbuging. Here is my code:

    #!/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/0-
0041/iio:device1/in_power0_input" 
  "VDD_SYS_GPU /sys/bus/i2c/drivers/ina3221x/0-
0040/iio:device0/in_power0_input" 
  "VDD_SYS_CPU /sys/bus/i2c/drivers/ina3221x/0-
0041/iio:device1/in_power1_input" 
  "VDD_SYS_SOC /sys/bus/i2c/drivers/ina3221x/0-
0040/iio:device0/in_power1_input" 
  "VDD_SYS_DDR /sys/bus/i2c/drivers/ina3221x/0-
0041/iio:device1/in_power2_input" 
 "VDD_4V0_WIFI /sys/bus/i2c/drivers/ina3221x/0-
0040/iio:device0/in_power2_input"
)
for
((
i 
=
 0
;
 i 
<
 ${#RAILS[@]}
;
 i++
));
do
Optimizin
g Jetson TX2 for Power and Performance
Jetson TX2
TDG
-08413-
001_v1.0  
|     28 
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'm doign it manually but I wonder if it could be done automatically.

2

What IDE are you using? In VIM, gg=G works reasonably well. If you're using Atom, then see this: https://atom.io/packages/format-shell .

4
  • True, but OP has a lot of lines that need to be joined together before =. I think it's worth mentioning you can select a range of lines with visual line mode (shift + V, j and k to move up and down), then join them with J.
    – m0dular
    May 31 '18 at 23:27
  • 1
    how do I use gg=G in vim?
    – vfbsilva
    Jun 1 '18 at 0:03
  • 1
    How to use gg=G: stackoverflow.com/a/12582414/487992
    – JohnMudd
    Sep 28 '18 at 19:04
  • 2
    gg=G does not work on my complex Bash script at all :) Apr 18 '20 at 0:46
0

format-shell uses shfmt (https://github.com/mvdan/sh) under the hood. This tool could be plugged to CI or as pre-commit hook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.