timefunc: a function for creating a line-by-line execution time profile for bash code with very minimal overhead
CODE IS HOSTED ON GITHUB HERE
`timefunc` produces a cumulative line-by-line execution time profile for bash noscripts and functions. It is "cumulative" in the sense that if a given line is run multiple times (e.g., because it is in a loop) it will show to total cumulative time spent running that particular line, as well as the command that was run and how many times said command was run.
`timefunc` works by cleverly using the DEBUG trap to keep track of the cumulative time taken per line (as given by `$LINENO`) as the function runs, and then once it is finished running it takes this info and produces a human-usable time-profile report with it. Times are determined by recording start/stop time via the bash builtin `$EPOCHREALTIME` variable, and then the difference is computed and added to a per-line running total.
Note: `line-by-line` refers to lines that you would see after sourcing the function (call if `ff`) and running `declare -f ff`, not to lines in the original function definition. e.g., lines like `echo 1; echo 2` will be broken up into separate lines.
DEPENDENCIES: Bash 5+ (due to use of $EPOCHREALTIME), various GNU coreutils (cat, sed, grep, sort, cut, head, tail, ...)
Note: One could, without much effort, tweak this to use
USAGE
# First, source `timefunc`
source <(curl 'https://raw.githubusercontent.com/jkool702/timeprofile/main/timefunc.bash')
# run timefunc on already-sourced function gg, optionally with arguments for gg
timefunc gg [args]
# run timefunc on not-already-sourced function gg, optionally with arguments for gg, and specify where to source function gg from
timefunc -s "$gg_src" gg [args]
# run timefunc on noscript hh, optionally with arguments for hh
timefunc -S hh [args]
# note: anything passed via STDIN to timefunc will be redirected to the STDIN of the function/noscript being time profiled. e.g.,
echo 'stuff on STDIN' | timefunc gg
See the help text at the top of `timefunc` for more details
EXAMPLE
This is one of the functions I used for testing
gg() {
trap 'echo goodbye' EXIT
trap 'echo -n' DEBUG
echo 'start test'
printf '%s\n' 'loop 1 test'
for kk in $(sleep 1; seq 1 10); do
echo 'hi'
sleep 0.1s
echo $(echo bye; sleep 0.4s)
done
echo 'loop 1 test done'; echo 'loop 2 test'
for kk in {1..10}; do
if (( kk == ( ( kk / 2 ) 2 ) )); then
echo even; sleep 0.2s
elif (( kk == ( ( kk / 3 ) 3 ) )); then echo 'odd3'; sleep 0.3s
fi
echo
done
echo 'loop 2 test done'; echo 'loop 3 test (nested loops in subshell)'
(
for hh in {1..10}; do
for ii in $(seq 1 $gg); do
for jj in $(seq 1 $(( hh + ii ))); do
echo nope >/dev/null
sleep 0.01s
done
done
done
)
echo 'loop 3 test (nested loops in subshell) done';
echo 'loop 4 test (nested functions)'
ff() {
for kk in {1..10}; do
for ll in {1..5}; do
echo $(( $kk $ll ))
sleep 0.02s
done
done
}
ff
echo 'loop 4 test (nested functions) done';
echo 'loop test 5 (nested function in subshell)';
(
ff
)
echo 'loop test 5 (nested function in subshell) done';
echo 'test complete'
}
Running
CODE IS HOSTED ON GITHUB HERE
`timefunc` produces a cumulative line-by-line execution time profile for bash noscripts and functions. It is "cumulative" in the sense that if a given line is run multiple times (e.g., because it is in a loop) it will show to total cumulative time spent running that particular line, as well as the command that was run and how many times said command was run.
`timefunc` works by cleverly using the DEBUG trap to keep track of the cumulative time taken per line (as given by `$LINENO`) as the function runs, and then once it is finished running it takes this info and produces a human-usable time-profile report with it. Times are determined by recording start/stop time via the bash builtin `$EPOCHREALTIME` variable, and then the difference is computed and added to a per-line running total.
Note: `line-by-line` refers to lines that you would see after sourcing the function (call if `ff`) and running `declare -f ff`, not to lines in the original function definition. e.g., lines like `echo 1; echo 2` will be broken up into separate lines.
DEPENDENCIES: Bash 5+ (due to use of $EPOCHREALTIME), various GNU coreutils (cat, sed, grep, sort, cut, head, tail, ...)
Note: One could, without much effort, tweak this to use
date (and not $EPOCHREALTIME) to generate the timestamps and make it compatible with earlier bash versions, but this would come at the cost of significantly increasing the overhead from generating the time profile.USAGE
# First, source `timefunc`
source <(curl 'https://raw.githubusercontent.com/jkool702/timeprofile/main/timefunc.bash')
# run timefunc on already-sourced function gg, optionally with arguments for gg
timefunc gg [args]
# run timefunc on not-already-sourced function gg, optionally with arguments for gg, and specify where to source function gg from
timefunc -s "$gg_src" gg [args]
# run timefunc on noscript hh, optionally with arguments for hh
timefunc -S hh [args]
# note: anything passed via STDIN to timefunc will be redirected to the STDIN of the function/noscript being time profiled. e.g.,
echo 'stuff on STDIN' | timefunc gg
See the help text at the top of `timefunc` for more details
EXAMPLE
This is one of the functions I used for testing
timefunc that contains loops and nested loops, uses subshells and command substitutions, defines functions and calls said functions locally, and defines its own DEBUG and EXIT traps. Many sleep calls are included to check that the execution times and the commands are matching up properly.gg() {
trap 'echo goodbye' EXIT
trap 'echo -n' DEBUG
echo 'start test'
printf '%s\n' 'loop 1 test'
for kk in $(sleep 1; seq 1 10); do
echo 'hi'
sleep 0.1s
echo $(echo bye; sleep 0.4s)
done
echo 'loop 1 test done'; echo 'loop 2 test'
for kk in {1..10}; do
if (( kk == ( ( kk / 2 ) 2 ) )); then
echo even; sleep 0.2s
elif (( kk == ( ( kk / 3 ) 3 ) )); then echo 'odd3'; sleep 0.3s
fi
echo
done
echo 'loop 2 test done'; echo 'loop 3 test (nested loops in subshell)'
(
for hh in {1..10}; do
for ii in $(seq 1 $gg); do
for jj in $(seq 1 $(( hh + ii ))); do
echo nope >/dev/null
sleep 0.01s
done
done
done
)
echo 'loop 3 test (nested loops in subshell) done';
echo 'loop 4 test (nested functions)'
ff() {
for kk in {1..10}; do
for ll in {1..5}; do
echo $(( $kk $ll ))
sleep 0.02s
done
done
}
ff
echo 'loop 4 test (nested functions) done';
echo 'loop test 5 (nested function in subshell)';
(
ff
)
echo 'loop test 5 (nested function in subshell) done';
echo 'test complete'
}
Running
gg (without time profiling) takes just overGitHub
timeprofile/timefunc.bash at main · jkool702/timeprofile
tool for measuring the execution time of all commands called by a shell noscript or shell function - jkool702/timeprofile
16.5 seconds
time gg
real 0m16.575s
user 0m0.355s
sys 0m0.200s
Running
timefunc gg >/dev/null
1.1: 0.001325 sec { (2x) "${@}"; (1x) ${noscriptFlag}; (2x) :; (1x) ff; }
1.3: 0.002744 sec { (1x) echo 'start test'; (10x) for kk in {1..10}; }
1.4: 0.002519 sec { (10x) :; (1x) printf '%s\n' 'loop 1 test'; }
1.6: 1.030974 sec { (50x) for ll in {1..5}; }
1.7: 0.015178 sec { (50x) echo $(( $kk $ll )); (10x) for kk in $(sleep 1; seq 1 10); }
1.8: 1.117169 sec { (10x) echo 'hi'; (50x) sleep 0.02s; }
1.9: 1.022775 sec { (10x) sleep 0.1s; }
1.10: 4.180431 sec { (10x) echo $(echo bye; sleep 0.4s); }
1.12: 0.000239 sec { (1x) echo 'loop 1 test done'; }
1.13: 0.000216 sec { (1x) echo 'loop 2 test (nested loops)'; }
1.16: 0.001946 sec { (10x) for kk in {1..10}; }
1.19: 0.010337 sec { (50x) for jj in {1..5}; }
1.20: 0.010285 sec { (50x) (( ( kk + jj ) == ( ( ( kk + jj )/ 2 ) 2 ) )); }
1.21: 0.007913 sec { (25x) echo even; }
1.22: 5.058182 sec { (25x) sleep 0.2s; }
1.24: 0.005089 sec { (25x) (( ( kk + jj ) == ( ( ( kk + jj ) / 3 ) 3 ) )); }
1.25: 0.001776 sec { (8x) echo 'odd3'; }
1.26: 2.418957 sec { (8x) sleep 0.3s; }
1.29: 0.012242 sec { (50x) echo; }
1.32: 0.000239 sec { (1x) echo 'loop 2 test (nested loops) done'; }
1.33: 0.000222 sec { (1x) echo 'loop 3 test (nested loops in subshell)'; }
1.48: 0.000253 sec { (1x) echo 'loop 3 test (nested loops in subshell) done'; }
1.49: 0.000234 sec { (1x) echo 'loop 4 test (nested functions)'; }
1.50: 0.000203 sec { (1x) ff; }
1.51: 0.000246 sec { (1x) echo 'loop 4 test (nested functions) done'; }
1.52: 0.000220 sec { (1x) echo 'loop test 5 (nested function in subshell)'; }
1.55: 0.000252 sec { (1x) echo 'loop test 5 (nested function in subshell) done'; }
1.56: 0.000326 sec { (1x) echo 'test complete'; }
TOTAL TIME TAKEN: 17.335308 seconds
SUBSHELL COMMANDS
2.1: 0.002224 sec { (1x) :); (10x) echo $(echo bye; sleep 0.4s)); (1x) ff; }
2.3: 0.006185 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); (9x) for kk in {1..10}; (1x) for kk in {1..10}); }
2.6: 1.015416 sec { (49x) for ll in {1..5}; (1x) for ll in {1..5}); (1x) seq 1 10); (1x) sleep 1; }
2.7: 0.014421 sec { (49x) echo $(( $kk $ll )); (1x) echo $(( $kk $ll ))); }
2.8: 1.119910 sec { (49x) sleep 0.02s; (1x) sleep 0.02s); }
2.9: 4.026090 sec { (10x) echo bye; (10x) sleep 0.4s); }
2.10: 0.001833 sec { (10x) echo $(echo bye; sleep 0.4s)); }
2.36: 0.002011 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); }
2.39: 0.002030 sec { (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); }
2.42: 0.013214 sec { (64x) for jj in $(seq 1 $(( hh + ii ))); (1x) for jj in $(seq 1 $(( hh + ii )))); }
2.43: 0.015589 sec { (64x) echo nope > /dev/null; (1x) echo nope > /dev/null); }
2.44: 0.789913 sec { (64x) sleep 0.01s; (1x) sleep 0.01s); }
2.54: 0.000217 sec { (1x) ff); }
time profile for gg has been saved to /root/timeprofile.gg
The overall execution time increased by ~760 ms to 17.335 seconds, an increase of ~4.6%. Its worth noting that this does not include the ~1 second needed at the end after the function has finished running to actually generate the time profile from the raw timing data. Its also, however, worth noting that the "total execution time" in the time profile report includes the time taken by the DEBUG trap to keep track of the cumulative time taken for each line, but the per-line totals do not (the DEBUG trap basically stops the timer by setting tStop, then figures out the time difference, then starts a
time gg
real 0m16.575s
user 0m0.355s
sys 0m0.200s
Running
timefunc gg gives the following output:timefunc gg >/dev/null
1.1: 0.001325 sec { (2x) "${@}"; (1x) ${noscriptFlag}; (2x) :; (1x) ff; }
1.3: 0.002744 sec { (1x) echo 'start test'; (10x) for kk in {1..10}; }
1.4: 0.002519 sec { (10x) :; (1x) printf '%s\n' 'loop 1 test'; }
1.6: 1.030974 sec { (50x) for ll in {1..5}; }
1.7: 0.015178 sec { (50x) echo $(( $kk $ll )); (10x) for kk in $(sleep 1; seq 1 10); }
1.8: 1.117169 sec { (10x) echo 'hi'; (50x) sleep 0.02s; }
1.9: 1.022775 sec { (10x) sleep 0.1s; }
1.10: 4.180431 sec { (10x) echo $(echo bye; sleep 0.4s); }
1.12: 0.000239 sec { (1x) echo 'loop 1 test done'; }
1.13: 0.000216 sec { (1x) echo 'loop 2 test (nested loops)'; }
1.16: 0.001946 sec { (10x) for kk in {1..10}; }
1.19: 0.010337 sec { (50x) for jj in {1..5}; }
1.20: 0.010285 sec { (50x) (( ( kk + jj ) == ( ( ( kk + jj )/ 2 ) 2 ) )); }
1.21: 0.007913 sec { (25x) echo even; }
1.22: 5.058182 sec { (25x) sleep 0.2s; }
1.24: 0.005089 sec { (25x) (( ( kk + jj ) == ( ( ( kk + jj ) / 3 ) 3 ) )); }
1.25: 0.001776 sec { (8x) echo 'odd3'; }
1.26: 2.418957 sec { (8x) sleep 0.3s; }
1.29: 0.012242 sec { (50x) echo; }
1.32: 0.000239 sec { (1x) echo 'loop 2 test (nested loops) done'; }
1.33: 0.000222 sec { (1x) echo 'loop 3 test (nested loops in subshell)'; }
1.48: 0.000253 sec { (1x) echo 'loop 3 test (nested loops in subshell) done'; }
1.49: 0.000234 sec { (1x) echo 'loop 4 test (nested functions)'; }
1.50: 0.000203 sec { (1x) ff; }
1.51: 0.000246 sec { (1x) echo 'loop 4 test (nested functions) done'; }
1.52: 0.000220 sec { (1x) echo 'loop test 5 (nested function in subshell)'; }
1.55: 0.000252 sec { (1x) echo 'loop test 5 (nested function in subshell) done'; }
1.56: 0.000326 sec { (1x) echo 'test complete'; }
TOTAL TIME TAKEN: 17.335308 seconds
SUBSHELL COMMANDS
2.1: 0.002224 sec { (1x) :); (10x) echo $(echo bye; sleep 0.4s)); (1x) ff; }
2.3: 0.006185 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); (9x) for kk in {1..10}; (1x) for kk in {1..10}); }
2.6: 1.015416 sec { (49x) for ll in {1..5}; (1x) for ll in {1..5}); (1x) seq 1 10); (1x) sleep 1; }
2.7: 0.014421 sec { (49x) echo $(( $kk $ll )); (1x) echo $(( $kk $ll ))); }
2.8: 1.119910 sec { (49x) sleep 0.02s; (1x) sleep 0.02s); }
2.9: 4.026090 sec { (10x) echo bye; (10x) sleep 0.4s); }
2.10: 0.001833 sec { (10x) echo $(echo bye; sleep 0.4s)); }
2.36: 0.002011 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); }
2.39: 0.002030 sec { (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); }
2.42: 0.013214 sec { (64x) for jj in $(seq 1 $(( hh + ii ))); (1x) for jj in $(seq 1 $(( hh + ii )))); }
2.43: 0.015589 sec { (64x) echo nope > /dev/null; (1x) echo nope > /dev/null); }
2.44: 0.789913 sec { (64x) sleep 0.01s; (1x) sleep 0.01s); }
2.54: 0.000217 sec { (1x) ff); }
time profile for gg has been saved to /root/timeprofile.gg
The overall execution time increased by ~760 ms to 17.335 seconds, an increase of ~4.6%. Its worth noting that this does not include the ~1 second needed at the end after the function has finished running to actually generate the time profile from the raw timing data. Its also, however, worth noting that the "total execution time" in the time profile report includes the time taken by the DEBUG trap to keep track of the cumulative time taken for each line, but the per-line totals do not (the DEBUG trap basically stops the timer by setting tStop, then figures out the time difference, then starts a
timefunc: a function for creating a line-by-line execution time profile for bash code with very minimal overhead
CODE IS HOSTED ON GITHUB [HERE](https://github.com/jkool702/timeprofile/blob/main/timefunc.bash)
***
`timefunc` produces a cumulative line-by-line execution time profile for bash noscripts and functions. It is "cumulative" in the sense that if a given line is run multiple times (e.g., because it is in a loop) it will show to total cumulative time spent running that particular line, as well as the command that was run and how many times said command was run.
`timefunc` works by cleverly using the DEBUG trap to keep track of the cumulative time taken per line (as given by `$LINENO`) as the function runs, and then once it is finished running it takes this info and produces a human-usable time-profile report with it. Times are determined by recording start/stop time via the bash builtin `$EPOCHREALTIME` variable, and then the difference is computed and added to a per-line running total.
Note: `line-by-line` refers to lines that you would see after sourcing the function (call if `ff`) and running `declare -f ff`, not to lines in the original function definition. e.g., lines like `echo 1; echo 2` will be broken up into separate lines.
***
DEPENDENCIES: Bash 5+ (due to use of $EPOCHREALTIME), various GNU coreutils (cat, sed, grep, sort, cut, head, tail, ...)
Note: One could, without much effort, tweak this to use `date` (and not `$EPOCHREALTIME`) to generate the timestamps and make it compatible with earlier bash versions, but this would come at the cost of significantly increasing the overhead from generating the time profile.
***
USAGE
# First, source `timefunc`
source <(curl 'https://raw.githubusercontent.com/jkool702/timeprofile/main/timefunc.bash')
# run timefunc on already-sourced function gg, optionally with arguments for gg
timefunc gg [args]
# run timefunc on not-already-sourced function gg, optionally with arguments for gg, and specify where to source function gg from
timefunc -s "$gg_src" gg [args]
# run timefunc on noscript hh, optionally with arguments for hh
timefunc -S hh [args]
# note: anything passed via STDIN to timefunc will be redirected to the STDIN of the function/noscript being time profiled. e.g.,
echo 'stuff on STDIN' | timefunc gg
See the help text at the top of `timefunc` for more details
***
EXAMPLE
This is one of the functions I used for testing `timefunc` that contains loops and nested loops, uses subshells and command substitutions, defines functions and calls said functions locally, and defines its own DEBUG and EXIT traps. Many sleep calls are included to check that the execution times and the commands are matching up properly.
gg() {
trap 'echo goodbye' EXIT
trap 'echo -n' DEBUG
echo 'start test'
printf '%s\n' 'loop 1 test'
for kk in $(sleep 1; seq 1 10); do
echo 'hi'
sleep 0.1s
echo $(echo bye; sleep 0.4s)
done
echo 'loop 1 test done'; echo 'loop 2 test'
for kk in {1..10}; do
if (( kk == ( ( kk / 2 ) * 2 ) )); then
echo even; sleep 0.2s
elif (( kk == ( ( kk / 3 ) * 3 ) )); then echo 'odd3'; sleep 0.3s
fi
echo
done
echo 'loop 2 test done'; echo 'loop 3 test (nested loops in subshell)'
(
for hh in {1..10}; do
for ii in $(seq 1 $gg); do
for jj in $(seq 1 $(( hh + ii ))); do
echo nope >/dev/null
sleep 0.01s
done
done
done
)
echo 'loop 3 test (nested loops in subshell) done';
echo 'loop 4 test (nested functions)'
ff() {
for kk in {1..10}; do
for ll in {1..5}; do
echo $(( $kk ** $ll ))
sleep 0.02s
done
done
}
ff
echo 'loop 4 test (nested functions) done';
echo 'loop test 5 (nested function in subshell)';
(
ff
)
echo 'loop test 5 (nested function in subshell) done';
echo 'test complete'
}
Running `gg` (without time profiling) takes just over
CODE IS HOSTED ON GITHUB [HERE](https://github.com/jkool702/timeprofile/blob/main/timefunc.bash)
***
`timefunc` produces a cumulative line-by-line execution time profile for bash noscripts and functions. It is "cumulative" in the sense that if a given line is run multiple times (e.g., because it is in a loop) it will show to total cumulative time spent running that particular line, as well as the command that was run and how many times said command was run.
`timefunc` works by cleverly using the DEBUG trap to keep track of the cumulative time taken per line (as given by `$LINENO`) as the function runs, and then once it is finished running it takes this info and produces a human-usable time-profile report with it. Times are determined by recording start/stop time via the bash builtin `$EPOCHREALTIME` variable, and then the difference is computed and added to a per-line running total.
Note: `line-by-line` refers to lines that you would see after sourcing the function (call if `ff`) and running `declare -f ff`, not to lines in the original function definition. e.g., lines like `echo 1; echo 2` will be broken up into separate lines.
***
DEPENDENCIES: Bash 5+ (due to use of $EPOCHREALTIME), various GNU coreutils (cat, sed, grep, sort, cut, head, tail, ...)
Note: One could, without much effort, tweak this to use `date` (and not `$EPOCHREALTIME`) to generate the timestamps and make it compatible with earlier bash versions, but this would come at the cost of significantly increasing the overhead from generating the time profile.
***
USAGE
# First, source `timefunc`
source <(curl 'https://raw.githubusercontent.com/jkool702/timeprofile/main/timefunc.bash')
# run timefunc on already-sourced function gg, optionally with arguments for gg
timefunc gg [args]
# run timefunc on not-already-sourced function gg, optionally with arguments for gg, and specify where to source function gg from
timefunc -s "$gg_src" gg [args]
# run timefunc on noscript hh, optionally with arguments for hh
timefunc -S hh [args]
# note: anything passed via STDIN to timefunc will be redirected to the STDIN of the function/noscript being time profiled. e.g.,
echo 'stuff on STDIN' | timefunc gg
See the help text at the top of `timefunc` for more details
***
EXAMPLE
This is one of the functions I used for testing `timefunc` that contains loops and nested loops, uses subshells and command substitutions, defines functions and calls said functions locally, and defines its own DEBUG and EXIT traps. Many sleep calls are included to check that the execution times and the commands are matching up properly.
gg() {
trap 'echo goodbye' EXIT
trap 'echo -n' DEBUG
echo 'start test'
printf '%s\n' 'loop 1 test'
for kk in $(sleep 1; seq 1 10); do
echo 'hi'
sleep 0.1s
echo $(echo bye; sleep 0.4s)
done
echo 'loop 1 test done'; echo 'loop 2 test'
for kk in {1..10}; do
if (( kk == ( ( kk / 2 ) * 2 ) )); then
echo even; sleep 0.2s
elif (( kk == ( ( kk / 3 ) * 3 ) )); then echo 'odd3'; sleep 0.3s
fi
echo
done
echo 'loop 2 test done'; echo 'loop 3 test (nested loops in subshell)'
(
for hh in {1..10}; do
for ii in $(seq 1 $gg); do
for jj in $(seq 1 $(( hh + ii ))); do
echo nope >/dev/null
sleep 0.01s
done
done
done
)
echo 'loop 3 test (nested loops in subshell) done';
echo 'loop 4 test (nested functions)'
ff() {
for kk in {1..10}; do
for ll in {1..5}; do
echo $(( $kk ** $ll ))
sleep 0.02s
done
done
}
ff
echo 'loop 4 test (nested functions) done';
echo 'loop test 5 (nested function in subshell)';
(
ff
)
echo 'loop test 5 (nested function in subshell) done';
echo 'test complete'
}
Running `gg` (without time profiling) takes just over
GitHub
timeprofile/timefunc.bash at main · jkool702/timeprofile
tool for measuring the execution time of all commands called by a shell noscript or shell function - jkool702/timeprofile
16.5 seconds
time gg
real 0m16.575s
user 0m0.355s
sys 0m0.200s
Running `timefunc gg` gives the following output:
timefunc gg >/dev/null
1.1: 0.001325 sec { (2x) "${@}"; (1x) ${noscriptFlag}; (2x) :; (1x) ff; }
1.3: 0.002744 sec { (1x) echo 'start test'; (10x) for kk in {1..10}; }
1.4: 0.002519 sec { (10x) :; (1x) printf '%s\n' 'loop 1 test'; }
1.6: 1.030974 sec { (50x) for ll in {1..5}; }
1.7: 0.015178 sec { (50x) echo $(( $kk ** $ll )); (10x) for kk in $(sleep 1; seq 1 10); }
1.8: 1.117169 sec { (10x) echo 'hi'; (50x) sleep 0.02s; }
1.9: 1.022775 sec { (10x) sleep 0.1s; }
1.10: 4.180431 sec { (10x) echo $(echo bye; sleep 0.4s); }
1.12: 0.000239 sec { (1x) echo 'loop 1 test done'; }
1.13: 0.000216 sec { (1x) echo 'loop 2 test (nested loops)'; }
1.16: 0.001946 sec { (10x) for kk in {1..10}; }
1.19: 0.010337 sec { (50x) for jj in {1..5}; }
1.20: 0.010285 sec { (50x) (( ( kk + jj ) == ( ( ( kk + jj )/ 2 ) * 2 ) )); }
1.21: 0.007913 sec { (25x) echo even; }
1.22: 5.058182 sec { (25x) sleep 0.2s; }
1.24: 0.005089 sec { (25x) (( ( kk + jj ) == ( ( ( kk + jj ) / 3 ) * 3 ) )); }
1.25: 0.001776 sec { (8x) echo 'odd3'; }
1.26: 2.418957 sec { (8x) sleep 0.3s; }
1.29: 0.012242 sec { (50x) echo; }
1.32: 0.000239 sec { (1x) echo 'loop 2 test (nested loops) done'; }
1.33: 0.000222 sec { (1x) echo 'loop 3 test (nested loops in subshell)'; }
1.48: 0.000253 sec { (1x) echo 'loop 3 test (nested loops in subshell) done'; }
1.49: 0.000234 sec { (1x) echo 'loop 4 test (nested functions)'; }
1.50: 0.000203 sec { (1x) ff; }
1.51: 0.000246 sec { (1x) echo 'loop 4 test (nested functions) done'; }
1.52: 0.000220 sec { (1x) echo 'loop test 5 (nested function in subshell)'; }
1.55: 0.000252 sec { (1x) echo 'loop test 5 (nested function in subshell) done'; }
1.56: 0.000326 sec { (1x) echo 'test complete'; }
TOTAL TIME TAKEN: 17.335308 seconds
SUBSHELL COMMANDS
2.1: 0.002224 sec { (1x) :); (10x) echo $(echo bye; sleep 0.4s)); (1x) ff; }
2.3: 0.006185 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); (9x) for kk in {1..10}; (1x) for kk in {1..10}); }
2.6: 1.015416 sec { (49x) for ll in {1..5}; (1x) for ll in {1..5}); (1x) seq 1 10); (1x) sleep 1; }
2.7: 0.014421 sec { (49x) echo $(( $kk ** $ll )); (1x) echo $(( $kk ** $ll ))); }
2.8: 1.119910 sec { (49x) sleep 0.02s; (1x) sleep 0.02s); }
2.9: 4.026090 sec { (10x) echo bye; (10x) sleep 0.4s); }
2.10: 0.001833 sec { (10x) echo $(echo bye; sleep 0.4s)); }
2.36: 0.002011 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); }
2.39: 0.002030 sec { (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); }
2.42: 0.013214 sec { (64x) for jj in $(seq 1 $(( hh + ii ))); (1x) for jj in $(seq 1 $(( hh + ii )))); }
2.43: 0.015589 sec { (64x) echo nope > /dev/null; (1x) echo nope > /dev/null); }
2.44: 0.789913 sec { (64x) sleep 0.01s; (1x) sleep 0.01s); }
2.54: 0.000217 sec { (1x) ff); }
time profile for gg has been saved to /root/timeprofile.gg
The overall execution time increased by ~760 ms to 17.335 seconds, an increase of ~4.6%. Its worth noting that this does not include the ~1 second needed at the end after the function has finished running to actually generate the time profile from the raw timing data. Its also, however, worth noting that the "total execution time" in the time profile report includes the time taken by the DEBUG trap to keep track of the cumulative time taken for each line, but the per-line totals do not (the DEBUG trap basically stops the timer by setting tStop, then figures out the time difference, then starts a
time gg
real 0m16.575s
user 0m0.355s
sys 0m0.200s
Running `timefunc gg` gives the following output:
timefunc gg >/dev/null
1.1: 0.001325 sec { (2x) "${@}"; (1x) ${noscriptFlag}; (2x) :; (1x) ff; }
1.3: 0.002744 sec { (1x) echo 'start test'; (10x) for kk in {1..10}; }
1.4: 0.002519 sec { (10x) :; (1x) printf '%s\n' 'loop 1 test'; }
1.6: 1.030974 sec { (50x) for ll in {1..5}; }
1.7: 0.015178 sec { (50x) echo $(( $kk ** $ll )); (10x) for kk in $(sleep 1; seq 1 10); }
1.8: 1.117169 sec { (10x) echo 'hi'; (50x) sleep 0.02s; }
1.9: 1.022775 sec { (10x) sleep 0.1s; }
1.10: 4.180431 sec { (10x) echo $(echo bye; sleep 0.4s); }
1.12: 0.000239 sec { (1x) echo 'loop 1 test done'; }
1.13: 0.000216 sec { (1x) echo 'loop 2 test (nested loops)'; }
1.16: 0.001946 sec { (10x) for kk in {1..10}; }
1.19: 0.010337 sec { (50x) for jj in {1..5}; }
1.20: 0.010285 sec { (50x) (( ( kk + jj ) == ( ( ( kk + jj )/ 2 ) * 2 ) )); }
1.21: 0.007913 sec { (25x) echo even; }
1.22: 5.058182 sec { (25x) sleep 0.2s; }
1.24: 0.005089 sec { (25x) (( ( kk + jj ) == ( ( ( kk + jj ) / 3 ) * 3 ) )); }
1.25: 0.001776 sec { (8x) echo 'odd3'; }
1.26: 2.418957 sec { (8x) sleep 0.3s; }
1.29: 0.012242 sec { (50x) echo; }
1.32: 0.000239 sec { (1x) echo 'loop 2 test (nested loops) done'; }
1.33: 0.000222 sec { (1x) echo 'loop 3 test (nested loops in subshell)'; }
1.48: 0.000253 sec { (1x) echo 'loop 3 test (nested loops in subshell) done'; }
1.49: 0.000234 sec { (1x) echo 'loop 4 test (nested functions)'; }
1.50: 0.000203 sec { (1x) ff; }
1.51: 0.000246 sec { (1x) echo 'loop 4 test (nested functions) done'; }
1.52: 0.000220 sec { (1x) echo 'loop test 5 (nested function in subshell)'; }
1.55: 0.000252 sec { (1x) echo 'loop test 5 (nested function in subshell) done'; }
1.56: 0.000326 sec { (1x) echo 'test complete'; }
TOTAL TIME TAKEN: 17.335308 seconds
SUBSHELL COMMANDS
2.1: 0.002224 sec { (1x) :); (10x) echo $(echo bye; sleep 0.4s)); (1x) ff; }
2.3: 0.006185 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); (9x) for kk in {1..10}; (1x) for kk in {1..10}); }
2.6: 1.015416 sec { (49x) for ll in {1..5}; (1x) for ll in {1..5}); (1x) seq 1 10); (1x) sleep 1; }
2.7: 0.014421 sec { (49x) echo $(( $kk ** $ll )); (1x) echo $(( $kk ** $ll ))); }
2.8: 1.119910 sec { (49x) sleep 0.02s; (1x) sleep 0.02s); }
2.9: 4.026090 sec { (10x) echo bye; (10x) sleep 0.4s); }
2.10: 0.001833 sec { (10x) echo $(echo bye; sleep 0.4s)); }
2.36: 0.002011 sec { (9x) for hh in {1..10}; (1x) for hh in {1..10}); }
2.39: 0.002030 sec { (9x) for ii in $(seq 1 $gg); (1x) for ii in $(seq 1 $gg)); }
2.42: 0.013214 sec { (64x) for jj in $(seq 1 $(( hh + ii ))); (1x) for jj in $(seq 1 $(( hh + ii )))); }
2.43: 0.015589 sec { (64x) echo nope > /dev/null; (1x) echo nope > /dev/null); }
2.44: 0.789913 sec { (64x) sleep 0.01s; (1x) sleep 0.01s); }
2.54: 0.000217 sec { (1x) ff); }
time profile for gg has been saved to /root/timeprofile.gg
The overall execution time increased by ~760 ms to 17.335 seconds, an increase of ~4.6%. Its worth noting that this does not include the ~1 second needed at the end after the function has finished running to actually generate the time profile from the raw timing data. Its also, however, worth noting that the "total execution time" in the time profile report includes the time taken by the DEBUG trap to keep track of the cumulative time taken for each line, but the per-line totals do not (the DEBUG trap basically stops the timer by setting tStop, then figures out the time difference, then starts a
new timer by setting tStart). This means that the per-line execution times shown should contain, at most, 1-2% overhead. i.e., they are *very* close to what the actual execution times are when running the function without `timefunc`.
***
KNOWN ISSUES
To have a command record its time it must trigger the DEBUG trap, which doesn't always happen (e.g., for commands run in a subshell via `( ... )`). Commands run in subshells are grouped based on what `${BASH_SUBSHELL}.${LINENO}` evaluates to when the exit trap for said subshell is called, which doesn't always separate out commands as logically as one might like. Thus, the time profile may not be as "line-by-line" separated for stuff run in subshells, and stuff run in subshells may be missing entirely from the main shell's time profile (lines starting with 1.x) and/or have its times listed in both the main shell and subshell time profiles (e.g., as with `echo $(sleep 1, echo hi)`.
That all said, I believe that all commands are accounted for somewhere in the time profile, and that for a given line in the time profile the listed time is *almost* always accurate for the set of commands listed. Its just that the cumulative time taken running a particular line (that was run in a subshell) may be merged with other subshell lines.
If anyone has ideas on how I might be able to better separate these let me know, but when I tried I could basically either do what I did or have every single subshell command on its own separate line and not combine any of them, making the generated time profile much longer and less useful (IMO).
https://redd.it/1641o96
@r_bash
***
KNOWN ISSUES
To have a command record its time it must trigger the DEBUG trap, which doesn't always happen (e.g., for commands run in a subshell via `( ... )`). Commands run in subshells are grouped based on what `${BASH_SUBSHELL}.${LINENO}` evaluates to when the exit trap for said subshell is called, which doesn't always separate out commands as logically as one might like. Thus, the time profile may not be as "line-by-line" separated for stuff run in subshells, and stuff run in subshells may be missing entirely from the main shell's time profile (lines starting with 1.x) and/or have its times listed in both the main shell and subshell time profiles (e.g., as with `echo $(sleep 1, echo hi)`.
That all said, I believe that all commands are accounted for somewhere in the time profile, and that for a given line in the time profile the listed time is *almost* always accurate for the set of commands listed. Its just that the cumulative time taken running a particular line (that was run in a subshell) may be merged with other subshell lines.
If anyone has ideas on how I might be able to better separate these let me know, but when I tried I could basically either do what I did or have every single subshell command on its own separate line and not combine any of them, making the generated time profile much longer and less useful (IMO).
https://redd.it/1641o96
@r_bash
Reddit
From the bash community on Reddit: timefunc: a function for creating a line-by-line execution time profile for bash code with very…
Explore this post and more from the bash community
Hijacking the new-tab command in gnome-terminal
I'm interesting in souping up my gnome-terminal (or any other terminal emulator that works with vte) so that, for example, if I'm inside a docker container and I use the new-tab command, the new tab opens up in the same docker container at the same working directory. I have control of the docker container, so I can put whatever I want into its bashrc to support that. As a starting point, I've been looking into how gnome-terminal knows to open new tabs at the same working directory when I'm _not_ in a container. I think I have a decent handle on that.
PROMPT_COMMAND gets set to a function __vte_prompt_command, which calls a function __vte_osc7. This second function uses escape characters to send a message to the terminal telling it the current working directory. At least, that seems to be the case.
The good news is that, in principle, this means one could send a working directory even from within a container, and it would still go back to the terminal (I have confirmed that this works). The problem, of course, is that when I try to open up a new tab, it would try to open it at that working directory on the host machine, not in the container. Assuming that location doesn't exist on the host machine, it will default to the home directory, and all of that will happen before it reaches my host machine's bashrc, where I could tell it to enter the docker container at a specified working directory.
So I'm wondering if there's any way to hijack the command that gets run when the user requests a new tab for gnome-terminal, so that I could access the working directory inside the docker container and use that information before it gets lost.
That's a rather long-winded question, I realize. Anyone have any thoughts? Thanks.
https://redd.it/16457zh
@r_bash
I'm interesting in souping up my gnome-terminal (or any other terminal emulator that works with vte) so that, for example, if I'm inside a docker container and I use the new-tab command, the new tab opens up in the same docker container at the same working directory. I have control of the docker container, so I can put whatever I want into its bashrc to support that. As a starting point, I've been looking into how gnome-terminal knows to open new tabs at the same working directory when I'm _not_ in a container. I think I have a decent handle on that.
PROMPT_COMMAND gets set to a function __vte_prompt_command, which calls a function __vte_osc7. This second function uses escape characters to send a message to the terminal telling it the current working directory. At least, that seems to be the case.
The good news is that, in principle, this means one could send a working directory even from within a container, and it would still go back to the terminal (I have confirmed that this works). The problem, of course, is that when I try to open up a new tab, it would try to open it at that working directory on the host machine, not in the container. Assuming that location doesn't exist on the host machine, it will default to the home directory, and all of that will happen before it reaches my host machine's bashrc, where I could tell it to enter the docker container at a specified working directory.
So I'm wondering if there's any way to hijack the command that gets run when the user requests a new tab for gnome-terminal, so that I could access the working directory inside the docker container and use that information before it gets lost.
That's a rather long-winded question, I realize. Anyone have any thoughts? Thanks.
https://redd.it/16457zh
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
What does ${foo:+--bar} do
Basically noscript. I am working on a noscript made by a bash wizard that is no longer in my team. I don't even know how to Google this.
https://redd.it/164m9v6
@r_bash
Basically noscript. I am working on a noscript made by a bash wizard that is no longer in my team. I don't even know how to Google this.
https://redd.it/164m9v6
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
how to show progress bar of currently running bash noscript
I have a noscript (wrapper) that calls for another noscript (main), when I launch it, it does not really show anything, I would like for the wrapper noscript to show a progress bar of the main noscript and when main reaches to the end of the file, wrapper shows a complete progress bar :
​
wrapper.sh
#!/bin/bash
main.sh | tee | aha > generated.html
main.sh is noscript that has multiple commands to run, about 335 line
How can I achieve this?
https://redd.it/164lamu
@r_bash
I have a noscript (wrapper) that calls for another noscript (main), when I launch it, it does not really show anything, I would like for the wrapper noscript to show a progress bar of the main noscript and when main reaches to the end of the file, wrapper shows a complete progress bar :
​
wrapper.sh
#!/bin/bash
main.sh | tee | aha > generated.html
main.sh is noscript that has multiple commands to run, about 335 line
How can I achieve this?
https://redd.it/164lamu
@r_bash
Why does ls output everything on one line but if we write ls to a file, there are newlines?
$ ls
gives us
--------------------------------
$ls > newfile
$cat newfile
gives us
Why doesn't the native ls command output the files with newlines?
echo -e $(ls) doesn't output the newlines either
https://redd.it/1656186
@r_bash
$ ls
gives us
file1 file2 file3 ... etc--------------------------------
$ls > newfile
$cat newfile
gives us
file1file2file3Why doesn't the native ls command output the files with newlines?
echo -e $(ls) doesn't output the newlines either
https://redd.it/1656186
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Script runs perfectly when run manually but gives an error when run from crontab
- I am trying to do something very simple
- Send an email via AWS SES from within an EC2 instance
- I have an IAM instance role configured on EC2 which has the appropriate permissions to send an email using SES
Here is my test.sh noscript which is stored at /home/ec2-user
When I run this noscript manually using
It sends me the email successfully and I can see this output (checked my inbox too and it works)
But when I created a crontab -e as follows
I see an error inside the tmp/cron-test.log file
I did not create an AWS credentials because I am assuming the EC2 instance with the IAM role is automatically going to create temporary credentials for me? Why is this not working? I dont want to store access key id and secret on AWS ec2 as it goes against best practices. How do I fix this?
https://redd.it/165ev1q
@r_bash
- I am trying to do something very simple
- Send an email via AWS SES from within an EC2 instance
- I have an IAM instance role configured on EC2 which has the appropriate permissions to send an email using SES
Here is my test.sh noscript which is stored at /home/ec2-user
#!/bin/bash
from="cron@example.com"
to="example@gmail.com"
subject="Test email sent at $(date)"
text="Test email with test data sent at test date $(date)"
aws ses send-email --from "${from}" --subject "${subject}" --text "${text}" --to "${to}"
When I run this noscript manually using
./test.sh
It sends me the email successfully and I can see this output (checked my inbox too and it works)
{
"MessageId": "0200028a46979f63-3b413e00-4c88-4504-b677-d908007e032d-000000"
}
But when I created a crontab -e as follows
*/15 * * * * bash /home/ec2-user/test.sh >> /tmp/cron-test.log 2>&1
I see an error inside the tmp/cron-test.log file
You must specify a region. You can also configure your region by running "aws configure".
I did not create an AWS credentials because I am assuming the EC2 instance with the IAM role is automatically going to create temporary credentials for me? Why is this not working? I dont want to store access key id and secret on AWS ec2 as it goes against best practices. How do I fix this?
https://redd.it/165ev1q
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
One command line to open sh app$ and execute command 🤔
Hi i have docker on Vps and i want to check some state of my nodes in container. I think the best way to do that is make bash noscript, which loop command over all containers and read status.
I want to do that remotely with paramiko (Python library).
I think i should do it in one line, and here is my problem idk how to do it in one line.
docker exec - it d1 bin/sh <- bash
cd /root/.folder & & ./app.sh <-shell
COMMAND - - status <- app$
Is it possible to do it in one line? 🤔
https://redd.it/165jow5
@r_bash
Hi i have docker on Vps and i want to check some state of my nodes in container. I think the best way to do that is make bash noscript, which loop command over all containers and read status.
I want to do that remotely with paramiko (Python library).
I think i should do it in one line, and here is my problem idk how to do it in one line.
docker exec - it d1 bin/sh <- bash
cd /root/.folder & & ./app.sh <-shell
COMMAND - - status <- app$
Is it possible to do it in one line? 🤔
https://redd.it/165jow5
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Error: Value too great for base
Hey everyone, I wrote a bash noscript that is suppose to automate the creation of an AWS resource (specifically the noscript helps to create EBS snapshots).
Volume_map takes the volume IDs, which is a required credential to create the snapshot.
But when i run the noscript I get an error
https://redd.it/165l5ut
@r_bash
Hey everyone, I wrote a bash noscript that is suppose to automate the creation of an AWS resource (specifically the noscript helps to create EBS snapshots).
Volume_map takes the volume IDs, which is a required credential to create the snapshot.
volume_map["vol-0b235e5983aae1aef"]="Folder1" volume_map["vol-050ec02d0a7d2288b"]="Folder2"But when i run the noscript I get an error
./create_snapshot.sh: line 5: vol-050ec02d0a7d2288b: value too great for base (error token is "050ec02d0a7d2288b")https://redd.it/165l5ut
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
can only read out last thing added to array
function arraystest {
declare -a itemtype
declare -a itemname
declare -a seasonnumber
declare -a episodenumber
echo "num: ${num}"
echo -e
read -p -r "what type of item? " itemtype${num}
echo -e
read -p "name of item? " itemname[${num}]
echo -e
read -p "season number? " seasonnumber${num}
echo -e
read -p "episode number? " episodenumber[${num}]
echo -e
(( num++ ))
if (($num > 2))
then clearprintout
else arraystest
fi
}
function clearprintout {
clear
printout
}
function dump {
echo "string number 0 : ${itemtype0} ${itemname[0]} ${seasonnumber0} ${episodenumber[0]}"
echo "string number 1 : ${itemtype1} ${itemname[1]} ${seasonnumber1} ${episodenumber[1]}"
echo "string number 2 : ${itemtype2} ${itemname[2]} ${seasonnumber2} ${episodenumber[2]}"
echo -e
echo "itemtype: ${itemtype[*]}"
}
function printout {
echo "printnum: ${printnum}"
echo -e
echo "string number ${printnum} : ${itemtype[${printnum}]} ${itemname${printnum}} ${seasonnumber[${printnum}]} ${episodenumber${printnum}}"
(( printnum++ ))
if (($printnum > 2))
then dump
else printout
fi
}
# main
num=0
printnum=0
arraystest
This code is meant to ask a set of four questions, with the answer to each question going in an array. It's meant to do this three times, then print them out.
As far as I can tell, it's throwing away previous answers and keeping only the last one.
Where am I going wrong?
https://redd.it/165jifv
@r_bash
function arraystest {
declare -a itemtype
declare -a itemname
declare -a seasonnumber
declare -a episodenumber
echo "num: ${num}"
echo -e
read -p -r "what type of item? " itemtype${num}
echo -e
read -p "name of item? " itemname[${num}]
echo -e
read -p "season number? " seasonnumber${num}
echo -e
read -p "episode number? " episodenumber[${num}]
echo -e
(( num++ ))
if (($num > 2))
then clearprintout
else arraystest
fi
}
function clearprintout {
clear
printout
}
function dump {
echo "string number 0 : ${itemtype0} ${itemname[0]} ${seasonnumber0} ${episodenumber[0]}"
echo "string number 1 : ${itemtype1} ${itemname[1]} ${seasonnumber1} ${episodenumber[1]}"
echo "string number 2 : ${itemtype2} ${itemname[2]} ${seasonnumber2} ${episodenumber[2]}"
echo -e
echo "itemtype: ${itemtype[*]}"
}
function printout {
echo "printnum: ${printnum}"
echo -e
echo "string number ${printnum} : ${itemtype[${printnum}]} ${itemname${printnum}} ${seasonnumber[${printnum}]} ${episodenumber${printnum}}"
(( printnum++ ))
if (($printnum > 2))
then dump
else printout
fi
}
# main
num=0
printnum=0
arraystest
This code is meant to ask a set of four questions, with the answer to each question going in an array. It's meant to do this three times, then print them out.
As far as I can tell, it's throwing away previous answers and keeping only the last one.
Where am I going wrong?
https://redd.it/165jifv
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Can someone explain these wildcards with my shell expansions?
So then I tried
I'm not sure why these wildcards are making my search results more restrictive than not having them in the first place.
https://redd.it/165x09z
@r_bash
locate /bin/zip - This command works (and outputs files like usr/bin/zipgrep, etc) but I thought there was an implicit wildcard like this locate */bin/zipand that's why it locates all the directories that house /bin/zipSo then I tried
locate "*/bin/zip". No output... if usr/bin/zipgrep matched with command locate /bin/zip, why wouldn't anything appear in the output for locate "*/bin/zip"?locate "/bin/*zip*" - This command also does not work but I'm thinking it should because the wildcards loosen up the search parameter.I'm not sure why these wildcards are making my search results more restrictive than not having them in the first place.
https://redd.it/165x09z
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Eases that make writing Bash code easier?
It's been a while since I write bash noscripts (around 1 year) and the environment is always the same: the so well known and always used - for me \- in its most basic essence VIM. VIM is so settled in me that it took a really long process to write C code on VSCode instead of VIM. However, I do not intend to leave VIM and I suppose that many of you guys write your bash noscripts through this editor.
Therefore, I'd like to know from you things that you have plugged in (and consider indispensable nowadays) on the editor so that writing bash code turned easier. It can be anything like, for example, autocomplete. If it's not a pain for you, it'd be nice to know how to implement this feature on someone's else machine, so feel free to throw links too!
https://redd.it/166oo3d
@r_bash
It's been a while since I write bash noscripts (around 1 year) and the environment is always the same: the so well known and always used - for me \- in its most basic essence VIM. VIM is so settled in me that it took a really long process to write C code on VSCode instead of VIM. However, I do not intend to leave VIM and I suppose that many of you guys write your bash noscripts through this editor.
Therefore, I'd like to know from you things that you have plugged in (and consider indispensable nowadays) on the editor so that writing bash code turned easier. It can be anything like, for example, autocomplete. If it's not a pain for you, it'd be nice to know how to implement this feature on someone's else machine, so feel free to throw links too!
https://redd.it/166oo3d
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Beginner needs help with a short AWK function
Hi all. I'm trying to create a short bash noscript to assist me at work. I want to take the output of a command (which displays the install history of a server). Here is an example of what typical output of the history command looks like:
(install command)<machine name>
(machine name) | (install date) | (install time) | (install log id number) | (install status)
​
The (install status) field can be either "Successful" or "Failed".
I want to change the color and also make the text bold for both the first and last fields. But I want to specifically only change the font color in the last field to green if the last field contains the word "Successful" and if it contains the word "Failed" I want the font color for the last field to be red.
Then I want to display the entire output of the install command with the modified first and last fields pasted in.
So the goal output would look like:
(machine name in blue font color + bold) | (install date) | (install time) | (install log id number) | (install status ("Successful" <-- Green font or "Failed" <-- Red font) |
I have gotten this to work but it is a horrible way of doing it, it's about 100 lines of code. I think the command I really need to be using is awk(match) but I cannot find good guides on how to use it. I was able to get it to work to replace a certain word with another word, but I don't know how to use awk + match to change the font formatting like color and bold. Thank you to anyone who can help!!
https://redd.it/166sdag
@r_bash
Hi all. I'm trying to create a short bash noscript to assist me at work. I want to take the output of a command (which displays the install history of a server). Here is an example of what typical output of the history command looks like:
(install command)<machine name>
(machine name) | (install date) | (install time) | (install log id number) | (install status)
​
The (install status) field can be either "Successful" or "Failed".
I want to change the color and also make the text bold for both the first and last fields. But I want to specifically only change the font color in the last field to green if the last field contains the word "Successful" and if it contains the word "Failed" I want the font color for the last field to be red.
Then I want to display the entire output of the install command with the modified first and last fields pasted in.
So the goal output would look like:
(machine name in blue font color + bold) | (install date) | (install time) | (install log id number) | (install status ("Successful" <-- Green font or "Failed" <-- Red font) |
I have gotten this to work but it is a horrible way of doing it, it's about 100 lines of code. I think the command I really need to be using is awk(match) but I cannot find good guides on how to use it. I was able to get it to work to replace a certain word with another word, but I don't know how to use awk + match to change the font formatting like color and bold. Thank you to anyone who can help!!
https://redd.it/166sdag
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Difference between \( and "(
There's this example in the book I'm reading
https://redd.it/166urz2
@r_bash
There's this example in the book I'm reading
find /etc \( -type d -not -perm 0777 \). So I understand the backslashes are escaping the parenthesis creating the group but then I wanted to try find /etc "( -type d -not -perm 0777 )" but it gives different results. Don't the quotes also escape the parenthesis?https://redd.it/166urz2
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Efficiently replace a substring within a string
I want to replace a substring within a string. Is there a way, maybe with awk, to just replace the, let's say, second substring with another string? Something like replace $2 of variable1 with $substring (or "substring"). This would be very convenient, this way I could skip the step to grep the actual substring in the variable and then replace this string for the new string, e.g. with sed.
https://redd.it/167cck1
@r_bash
I want to replace a substring within a string. Is there a way, maybe with awk, to just replace the, let's say, second substring with another string? Something like replace $2 of variable1 with $substring (or "substring"). This would be very convenient, this way I could skip the step to grep the actual substring in the variable and then replace this string for the new string, e.g. with sed.
https://redd.it/167cck1
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Bash Scripting
Any resources to keep noscripts syntax skills sharp or advance them?
I've done a few courses on pluralsight but I don't often find myself noscripting much at work (I am in INFO SEC but most of our tools are already written) and with almost every coding I've tried to learn I will undoubtedly forget everything I've learned if I don't reinforce the skills for awhile.
Thank you.
https://redd.it/167h9z9
@r_bash
Any resources to keep noscripts syntax skills sharp or advance them?
I've done a few courses on pluralsight but I don't often find myself noscripting much at work (I am in INFO SEC but most of our tools are already written) and with almost every coding I've tried to learn I will undoubtedly forget everything I've learned if I don't reinforce the skills for awhile.
Thank you.
https://redd.it/167h9z9
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
is it possible to count the number of rows printed to in the terminal?
I'm working on a program that takes the size of the terminal into account. So far, I've got it adapting all the text output to the width, no problem at all.
The design of the program prints a bunch of text to the top of the screen, has a space, then prints some more at the bottom. (That space will be replaced later on with a table of data)
The blank space should be the bit that scales to fit vertically. I could cheat, and set something up with a fixed variable for the top and bottom half. But I was hoping for something robust that could handle any changes I might add to the top.
So, what I want to know is, is there a way to cook up a function that will count the number of terminal rows (top to bottom) that have been written to at the point it's envoked?
Say my program runs:
clear
echo "first line"
echo "second line"
echo "third line"
magicnumber linecount
echo "fourth line"
magicnumber runs and makes $linecount=3. It doesn't make it 4 because the fourth hadn't happened when it was envoked.
possible?
https://redd.it/167jfs4
@r_bash
I'm working on a program that takes the size of the terminal into account. So far, I've got it adapting all the text output to the width, no problem at all.
The design of the program prints a bunch of text to the top of the screen, has a space, then prints some more at the bottom. (That space will be replaced later on with a table of data)
The blank space should be the bit that scales to fit vertically. I could cheat, and set something up with a fixed variable for the top and bottom half. But I was hoping for something robust that could handle any changes I might add to the top.
So, what I want to know is, is there a way to cook up a function that will count the number of terminal rows (top to bottom) that have been written to at the point it's envoked?
Say my program runs:
clear
echo "first line"
echo "second line"
echo "third line"
magicnumber linecount
echo "fourth line"
magicnumber runs and makes $linecount=3. It doesn't make it 4 because the fourth hadn't happened when it was envoked.
possible?
https://redd.it/167jfs4
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community