1

the bash script

for i in $script_name
do
  echo -en "running the script - $i\t - "
  exec 3>&1 4>&2
  var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1)  # Captures time only
  exec 3>&- 4>&-

  echo "$var"
done

print the following:

running the script - Verify_disk.bash -       1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash -   2.93
running the script - Verify_mem_size.bash -       0.71
running the script - Verify_disk_size.bash -      2.41
running the script - Verify_wdisk.bash -        1.63
running the script - Verify_cpu.bash - 0.74

since $var is the variable that print all these output we want to alignment the output

so it will be like this

running the script - Verify_disk.bash        -   1.42
running the script - Verify_yum_list.bash    -   10.49
running the script - Verify_size.bash        -   2.93
running the script - Verify_mem_size.bash    -   0.71
running the script - Verify_disk_size.bash   -   2.41
running the script - Verify_wdisk.bash       -   1.63
running the script - Verify_cpu.bash         -   0.74

what are the additional changes that should be with $var in order to alignment the last fields

0

2 Answers 2

1

Do a pre-loop to calculate the longest filename then use that as a spacing parameter:

longest=0
for file in *.bash
do
  [ "${#file}" -gt "$longest" ] && longest=${#file}
done

# ... for your execution loop
printf "running the script - %${longest}s\t- "
printf "%s\n" "$var"

I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.

1

I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?

I also took the liberty (personal preference) to right-align the output of the time values.

Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:

cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2f\n",$1, $2, $3}' 
running the script  -  Verify_disk.bash              -   1.42
running the script  -  Verify_yum_list.bash          -  10.49
running the script  -  Verify_size.bash              -   2.93
running the script  -  Verify_mem_size.bash          -   0.71
running the script  -  Verify_disk_size.bash         -   2.41
running the script  -  Verify_wdisk.bash             -   1.63
running the script  -  Verify_cpu.bash               -   0.74

So if you were to pipe the output of your script through my awk ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .