-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_looper2.sh
More file actions
executable file
·78 lines (70 loc) · 2.1 KB
/
bash_looper2.sh
File metadata and controls
executable file
·78 lines (70 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# bash_looper2.sh
# 21 May 2025 | cruhl@datainscience.com
#
# This file is the bare minimum for a readable and maintainable shell script
# The key elements are:
# 1) A usage/help function
# 2) Command line arguments - miniumon of "-h" for "help/usage"
# 3) Live/Dry-run flag
# - I like the default to be non-live so I can review the commnads
# 4) Check command for errors, print errors at the end
# Usage output
function usage(){
echo "bash_tempalte.sh - basic bash script boilerplate"
echo " Options: "
echo " -h : print this message"
echo " -l : 'live-run mode' - evaluate and run commands. "
echo " default is 'dry-run mode' comands are only printed"
echo " -c : command loop count - run the command this many times"
echo " "
exit
}
# Set our default values
live_mode=0
count=0
# Loop over input options
# getopts "hlc:" means:
# h - define "-h" flag with no options
# l - define "-l" flag with no options
# c: - ( note the ":" ) define "-c" flag with one option - and save in "OPTARG"
while getopts "hlc:" arg; do
case $arg in
h)
# Found -h, show usage()
usage
;;
l)
# Found a -l, use live mode"
live_mode=1
;;
c)
# Found -c, argument value is stored in 'OPTARG'
count=$OPTARG
;;
esac
done
echo "Live Mode: $live_mode "
echo "Count count: $count"
cmd_base="./test_script.py"
# Use the linux "sequence" command to loop $count times
# We'll use the "live mode" flag here - it's helpful when reviewing
# what a script will do when it runs live
for i in $(seq 1 $count); do
echo "++===============================++"
cmd="$cmd_base --arg1 some_text_$i --failpct 30"
echo "cmd to run: $cmd"
# If we're in live mode, actually run the command
# for "not live_mode" the command is just skipped
if [[ $live_mode == 1 ]]; then
# Run the command
$cmd
rc=$?
echo "RC: $rc"
if [[ $rc > 0 ]]; then
echo "FAILED $cmd"
fi
fi
echo "--===============================--"
echo
done