-
Notifications
You must be signed in to change notification settings - Fork 11
/
execute_scripts.sh
6385 lines (5322 loc) · 190 KB
/
execute_scripts.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/ksh
#
# Note: use "/usr/bin/ksh -i" if the signal handler do not work
#
# **** Note: The main code starts after the line containing "# main:" ****
# The main code for your script should start after "# main - your code"
# Function statt after the line containing "# functions: "
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006-2014 Bernd Schemmer All rights reserved.
# Use is subject to license terms.
#
# Notes:
#
# - use "execute_scripts.sh {-v} {-v} {-v} -h" to get the usage help
#
# - use "execute_scripts.sh -H 2>execute_scripts.sh.doc" to get the documentation
#
# - use "execute_scripts.sh -X 2>execute_scripts.sh.examples.doc" to get some usage examples
#
# - this is a Kornshell script - it may not function correctly in other shells
# - the script was written and tested with ksh88 but should also work in ksh93
# The script should also work in bash -- but that is NOT completly tested
#
# ??? Add usage examples here; the lines should start with ##EXAMPLE##
#
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # create the documentation for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -H 2>./${__SCRIPTNAME}.doc
##EXAMPLE##
##EXAMPLE## # get the verbose usage for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -v -v -h
##EXAMPLE##
##EXAMPLE## # write a config file for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -C
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # use another config file
##EXAMPLE##
##EXAMPLE## CONFIG_FILE=myconfig.conf ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE## # use no config file at all
##EXAMPLE##
##EXAMPLE## CONFIG_FILE=none ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # write all output (STDOUT and STDERR) to the file
##EXAMPLE## # /var/tmp/mylog.txt
##EXAMPLE##
##EXAMPLE## __TEE_OUTPUT_FILE=/var/tmp/mylog.txt ./${__SCRIPTNAME} -T
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # create a dump of the environment variables in the files
##EXAMPLE## # /tmp/${__SCRIPTNAME}.envvars.\$\$
##EXAMPLE## # /tmp/${__SCRIPTNAME}.exported_envvars.\$\$
##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE##
##EXAMPLE## __CREATE_DUMP=1 ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE## # create a dump of the environment variables in the files
##EXAMPLE## # /var/tmp/debug/${__SCRIPTNAME}.envvars.\$\$
##EXAMPLE## # /var/tmp/debug/${__SCRIPTNAME}.exported_envvars.\##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE##
##EXAMPLE## __CREATE_DUMP=/var/tmp/debug ./${__SCRIPTNAME}
##EXAMPLE##
#
# Note: The escape character in the command below is only for the usage of execute_scripts.sh with the "-X" parameter!
#
##EXAMPLE## # use logger instead of echo to print the messages
##EXAMPLE##
##EXAMPLE## LOGMSG_FUNCTION=\"logger -s -p user.info execute_scripts.sh\" ./execute_scripts.sh
##EXAMPLE##
# -----------------------------------------------------------------------------
####
#### execute_scripts.sh - execute scripts parallel or sequentiell
####../../../../
#### Author: Bernd Schemmer ([email protected])
####
#### Version: see variable ${__SCRIPT_VERSION} below
#### (see variable ${__SCRIPT_TEMPLATE_VERSION} for the template version used)
####
#### Supported OS: Solaris and others
####
####
#### Description
#### -----------
####
#### execute scripts either parallel or sequentiell
##C#
##C# Configuration file
##C# ------------------
##C#
##C# This script supports a configuration file called <scriptname>.conf.
##C# The configuration file is searched in the working directory,
##C# the home directory of the user executing this script and in /etc
##C# (in this order).
##C#../../../../
##C# The configuration file is read before the parameter are processed.
##C#
##C# To override the default config file search set the variable
##C# CONFIG_FILE to the name of the config file to use.
##C#
##C# e.g. CONFIG_FILE=/var/myconfigfile ./execute_scripts.sh
##C#
##C# To disable the use of a config file use
##C#
##C# CONFIG_FILE=none ./execute_scripts.sh
##C#
##C# See the variable __CONFIG_PARAMETER below for the possible entries
##C# in the config file.
##C#
####
#### Predefined parameter
#### --------------------
####
#### see the subroutines ShowShortUsage and ShowUsage
####
#### Note: The current version of the script template can be found here:
####
#### http://bnsmb.de/solaris/scriptt.html
####
####
##T# Troubleshooting support
##T# -----------------------
##T#
##T# Use
##T#
##T# __CREATE_DUMP=<anyvalue|directory> <yourscript>
##T#
##T# to create a dump of the environment variables on program exit.
##T#
##T# e.g
##T#
##T# __CREATE_DUMP=1 ./execute_scripts.sh
##T#
##T# will create a dump of the environment variables in the files
##T#
##T# /tmp/execute_scripts.sh.envvars.$$
##T# /tmp/execute_scripts.sh.exported_envvars.$$
##T#
##T# before the script ends
##T#
##T# __CREATE_DUMP=/var/tmp/debug ./execute_scripts.sh
##T#
##T# will create a dump of the environment variables in the files
##T#
##T# /var/tmp/debug/execute_scripts.sh.envvars.$$
##T# /var/tmp/debug/execute_scripts.sh.exported_envvars.$$
##T#
##T# before the script ends (the target directory must already exist).
##T#
##T# Note that the dump files will always be created in case of a syntax
##T# error. To set the directory for these files use either
##T#
##T# export __DUMPDIR=/var/tmp/debug
##T# ./execute_scripts.sh
##T#
##T# or define __DUMPDIR in the script.
##T#
##T# To suppress creating the dump file in case of a syntax error add
##T# the statement
##T#
##T# __DUMP_ALREADY_CREATED=0../../../../
##T#
##T# to your script
##T#
##T# Use
##T#
##T# CreateDump <uniqdirectory> [filename_add]
##T#
##T# to manually create the dump files from within the script.
##T#
##T# e.g.
##T#
##T# CreateDump /var/debug
##T#
##T# will create the files
##T#
##T# /var/debug/execute_scripts.sh.envvars.$$
##T# /var/debug/execute_scripts.sh.exported_envvars.$$
##T#
##T# CreateDump /var/debug pass2.
##T#
##T# will create the files
##T#
##T# /var/debug/execute_scripts.sh.envvars.pass2.$$
##T# /var/debug/execute_scripts.sh.exported_envvars.pass2.$$
##T#
#### Note:
#### The default action for the signal handler USR1 is
#### "Create an environment dump in /var/tmp"
#### The filenames for the dumps are
####
#### /var/tmp/<scriptname>.envvars.dump_no_<no>_<PID>
#### /var/tmp/<scriptname>.exported_envvars.dump_no_<no>_<PID>
####
#### where <no> is a sequential number, <PID> is the PID of the
#### process with the script, and <scriptname> is the name of the
#### script without the path.
####
####
#### User defined signal handler
#### ---------------------------
####
#### You can define various SIGNAL handlers to process signals received
#### by the script.
####
#### All SIGNAL handlers can use these variables:
####
#### __TRAP_SIGNAL -- the catched trap signal
#### INTERRUPTED_FUNCTION -- the function interrupted by the signal
####
####
#### To define one signal handler for all signals do:
####
#### __GENERAL_SIGNAL_FUNCTION=<signalhandler_name>
####
#### To define unique signal handler for the various signals do:
####
#### __SIGNAL_SIGUSR1_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGUSR2_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGHUP_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGINT_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGQUIT_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGTERM_FUNCTION=<signalhandler_name>
####
#### If both type of signal handler are defined the script first
#### executes the general signal handler.
#### If this handler returns 0 the handler for the catched signal will
#### also be executed (else not).
#### If the unique handler for the signal ends with 0 the default signal
#### handler for this signal will also be executed (else not)
####
####
#### Credits
#### -------
####
#### Hints regarding the code to create the lockfile
#### wpollock (http://wikis.sun.com/display/~wpollock)
####
#### Source for the function PrintWithTimeStamp (in version 2.x and newer):
#### http://unix.stackexchange.com/questions/26728/prepending-a-timestamp-to-each-line-of-output-from-a-command
####
#### Andreas Obermaier for a security issue in the lockfile routine
#### (see history section 1.22.45 07.06.2012)
####
#### The code used in executeCommandAndLog is from
#### http://www.unix.com/unix-dummies-questions-answers/13018-exit-status-command-pipe-line.html#post47559
####
####
#### History:
#### --------
#### 1.0.0 07.06.2014 /bs
#### initial release
####
#### 1.0.1 09.01.2016 /bs
#### die will now end the script even if the parameter -f is used
####
#### 1.0.2 01.01.2017 /bs
#### replaced "set -o noglob" with "set -f" and "set -o glob" with "set +f" to be compatible with shells
#### like mirksh that to not support the glob/noglob syntax
####
#### 1.0.3 11.01.2019 /bs
#### __ingoretraps does not work on all ksh versions -- deleted
#### __unsettraps rewritten to work on more ksh versions
#### die now calls __unsettraps
#### LogRuntimeInfo rewritten to work on more ksh versions
#### code using LINENO corrected
####
####
#### script template History
#### -----------------------
#### 1.22.0 08.06.2006 /bs (BigAdmin Version 1)
#### public release; starting history for the script template
####
#### 1.22.1 12.06.2006 /bs
#### added true/false to CheckYNParameter and ConvertToYesNo
####
#### 1.22.2. 21.06.2006 /bs
#### added the parameter -V
#### added the use of environment variables
#### added the variable __NO_TIME_STAMPS
#### added the variable __NO_HEADERS
#### corrected a bug in the function executeCommandAndLogSTDERR
#### added missing return commands
####
#### 1.22.3 24.06.2006 /bs
#### added the function StartStop_LogAll_to_logfile
#### added the variable __USE_TTY (used in AskUser)
#### corrected an spelling error (dev/nul instead of /dev/null)
####
#### 1.22.4 06.07.2006 /bs
#### corrected a bug in the parameter error handling routine
####
#### 1.22.5 27.07.2006 /bs
#### corrected some minor bugs
####
#### 1.22.6 09.08.2006 /bs
#### corrected some minor bugs
####.
#### 1.22.7 17.08.2006 /bs
#### add the CheckParameterCount function
#### added the parameter -T
#### added long parameter support (e.g --help)
####
#### 1.22.8 07.09.2006 /bs
#### added code to save the env variable LANG and set it temporary to C
####
#### 1.22.9 20.09.2006 /bs
#### corrected code to save the env variable LANG and set it temporary to C
####
#### 1.22.10 21.09.2006 /bs
#### cleanup comments
#### the number of temporary files created automatically is now variable
#### (see the variable __NO_OF_TEMPFILES)
#### added code to install the trap handler in all functions
####
#### 1.22.11 19.10.2006 /bs
#### corrected a minor bug in AskUser (/c was not interpreted by echo)
#### corrected a bug in the handling of the parameter -S (-S was ignored)
####
#### 1.22.12 31.10.2006 /bs
#### added the variable __REQUIRED_ZONE
####
#### 1.22.13 13.11.2006 /bs
#### the template now uses TMP or TEMP if set for the temporary files
####
#### 1.22.14 14.11.2006 /bs
#### corrected a bug in the function AskUser (the default was y not n)
####
#### 1.22.15 21.11.2006 /bs
#### added initial support for other Operating Systems
####
#### 1.22.16 05.07.2007 /bs
#### enhanced initial support for other Operating Systems
#### Support for other OS is still not fully tested!
####
#### 1.22.17 06.07.2007 /bs
#### added the global variable __TRAP_SIGNAL
####
#### 1.22.18 01.08.2007 /bs
#### __OS_VERSION and __OS_RELEASE were not set - corrected
####
#### 1.22.19 04.08.2007 /bs
#### wrong function used to print "__TRAP_SIGNAL is \"${__TRAP_SIGNAL}\"" - fixed
####
#### 1.22.20 12.09.2007 /bs
#### the script now checks the ksh version if running on Solaris
#### made some changes for compatibility with ksh93
####
#### 1.22.21 18.09.2007 /bs (BigAdmin Version 2)
#### added the variable __FINISHROUTINES
#### changed __REQUIRED_ZONE to __REQUIRED_ZONES
#### added the variable __KSH_VERSION
#### reworked the trap handling
####
#### 1.22.22 23.09.2007 /bs
#### added the signal handling for SIGUSR1 and SIGUSR2 (variables __SIGUSR1_FUNC and __SIGUSR2_FUNC)
#### added user defined function for the signals HUP, BREAK, TERM, QUIT, EXIT, USR1 and USR2
#### added the variables __WARNING_PREFIX, __ERROR_PREFIX, __INFO_PREFIX, and __RUNTIME_INFO_PREFIX
#### the parameter -T or --tee can now be on any position in the parameters
#### the default output file if called with -T or --tee is now
#### /var/tmp/${0##*/}.$$.tee.log
####
#### 1.22.23 25.09.2007 /bs
#### added the environment variables __INFO_PREFIX, __WARNING_PREFIX,
#### __ERROR_PREFIX, and __RUNTIME_INFO_PREFIX
#### added the environment variable __DEBUG_HISTFILE
#### reworked the function to print the usage help :
#### use "-h -v" to view the extented usage help and use "-h -v -v" to
#### view the environment variables used also
####
#### 1.22.24 05.10.2007 /bs
#### another minor fix for ksh93 compatibility
####
#### 1.22.25 08.10.2007 /bs
#### only spelling errors corrected
####
#### 1.22.26 19.11.2007 /bs
#### only spelling errors corrected
####
#### 1.22.27 29.12.2007 /bs
#### improved the code to create the lockfile (thanks to wpollock for the info; see credits above)
#### improved the code to create the temporary files (thanks to wpollock for the info; see credits above)
#### added the function rand (thanks to wpollock for the info; see credits above)
#### the script now uses the directory name saved in the variable $TMPDIR for temporary files
#### if it's defined
#### now the umask used for creating temporary files can be changed (via variable __TEMPFILE_UMASK)
####
#### 1.22.28 12.01.2008 /bs
#### corrected a syntax error in the show usage routine
#### added the function PrintWithTimestamp (see credits above)
####
#### 1.22.29 31.01.2008 /bs
#### there was a bug in the new code to remove the lockfile which prevented
#### the script from removing the lockfile at program end
#### if the lockfile already exist the script printed not the correct error
#### message
####
#### 1.22.30 28.02.2008 /bs
#### Info update: executeCommandAndLog does NOT return the RC of the executed
#### command if a logfile is defined
#### added inital support for CYGWIN
#### (tested with CYGWIN_NT-5.1 v..1.5.20(0.156/4/2)
#### Most of the internal functions are NOT tested yet in CYGWIN
#### GetCurrentUID now supports UIDs greater than 254; the function now prints the UID to STDOUT
#### Corrected bug in GetUserName (only a workaround, not the solution)
#### now using printf in the AskUserRoutine
####
#### 1.22.30 28.02.2008 /bs
#### The lockfile is now also deleted if the script crashes because of a syntax error or something like this
####
#### 1.22.31 18.03.2008 /bs
#### added the version number to the start and end messages
#### an existing config file is now removed (and not read) if the script is called with -C to create a config file
####
#### 1.22.32 04.04.2008 /bs
#### minor changes for zone support
####
#### 1.22.33 12.02.2009 /bs
#### disabled the usage of prtdiag due to the fact that prtdiag on newer Sun machines needs a long time to run
#### (-> __MACHINE_SUBTYPE is now always empty for Solaris machines)
#### added the variable __CONFIG_FILE_FOUND; this variable contains the name of the config file
#### read if a config file was found
#### added the variable __CONFIG_FILE_VERSION
####
#### 1.22.34 28.02.2009 /bs
#### added code to check for the max. line no for the debug handler
#### (an array in ksh88 can only handle up to 4096 entries)
#### added the variable __PIDFILE
####
#### 1.22.35 06.04.2009 /bs
#### added the variables
#### __NO_CLEANUP
#### __NO_EXIT_ROUTINES
#### __NO_TEMPFILES_DELETE
#### __NO_TEMPMOUNTS_UMOUNT
#### __NO_TEMPDIR_DELETE
#### __NO_FINISH_ROUTINES
#### __CLEANUP_ON_ERROR
#### CONFIG_FILE
####
#### 1.22.36 11.04.2009 /bs
#### corrected a cosmetic error in the messages (wrong: ${TEMPFILE#} correct: ${__TEMPFILE#})
####
#### 1.22.37 08.07.2011 /bs
#### corrected a minor error with the QUIET parameter
#### added code to dump the environment (env var __CREATE_DUMP, function CreateDump )
#### implemented work around for missing function whence in bash
#### added the function LogIfNotVerbose
####
#### 1.22.38 22.07.2011 /bs
#### added code to make the trap handling also work in bash
#### added a sample user defined trap handler (function USER_SIGNAL_HANDLER)
#### added the function SetHousekeeping to enabe or disable house keeping
#### scriptt.sh did not write all messages to the logfile if a relative filename was used - fixed
#### added more help text for "-v -v -v -h"
#### now user defined signal handler can have arguments
#### the RBAC feature (__USE_RBAC) did not work as expected - fixed
#### added new scriptt testsuite for testing the script template on other OS and/or shells
#### added the function SaveEnvironmentVariables
####
#### 1.22.39 24.07.2011 /bs
#### __INIT_FUNCTION now enabled for cygwin also
#### __SHELL did not work in all Unixes - fixed
#### __OS_FULLNAME is now also set in Solaris and Linux
####
#### 1.22.40 25.07.2011 /bs
#### added some code for ksh93 (functions: substr)
#### Note: set __USE_ONLY_KSH88_FEATURES to ${__TRUE} to suppress using the ksh93 features
#### The default action for the signal handler USR1 is now "Create an env dump in /var/tmp"
#### The filenames for the dumps are
####
#### /var/tmp/<scriptname>.envvars.dump_no_<no>_<PID>
#### /var/tmp/<scriptname>.exported_envvars.dump_no_<no>_<PID>
####
#### where <no> is a sequential number, <PID> is the PID of the process with the script,
#### and <scriptname> is the name of the script without the path.
####
#### 1.22.41 26.09.2011 /bs
#### added the parameter -X
#### disabled some ksh93 code because "ksh -x -n" using ksh88 does not like it
####
#### 1.22.42 05.10.2011 /bs
#### added the function PrintDotToSTDOUT
####
#### 1.22.43 15.10.2011 /bs
#### added support for disabling the config file feature with CONFIG_FILE=none ./scriptt.sh
#### corrected a minor bug in SaveEnvironmentVariables
#### corrected a bug in the function SaveEnvironmentVariables
#### corrected a bug in getting the value for the variable ${__ABSOLUTE_SCRIPTDIR}
####
#### 1.22.44 22.04.2012 /bs
#### The script now uses nawk only if available (if not awk is used)
#### variables are now supported in the usage examples (prefixed with ##EXAMPLE##)
#### add a line with the current date and time to variable dumps, e.g.
####
#### ### /var/tmp/scriptt.sh.exported_envvars.dump_no_0_20074 - exported environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
####
#### ### /var/tmp/scriptt.sh.envvars.dump_no_0_20074 - environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
####
#### added experimental interactive mode to the signal handler for USR2
#### replaced /usr/bin/echo with printf
#### added the variable LOGMSG_FUNCTION
####
#### 1.22.45 07.06.2012 /bs
#### added code to check if the symbolic link for the lockfile already exists before creating
#### the lock file
####
#### 1.22.46 27.04.2013 /bs
#### executeCommandAndLog rewritten using coprocesses (see also credits)
#### Info update: executeCommandAndLog does now return the RC of the executed
#### command even if a logfile is defined
####
#### -------------------------------------------------------------------
####
#### 2.0.0.0 17.05.2013 /bs
#### added the variable __GENERAL_SIGNAL_FUNCTION: This variable
#### contains the name of a function that is called for all SIGNALs
#### before the special SIGNAL handler is called
#### removed the Debug Handler for single step execution (due to the
#### length of the template it is not useful anymore; use the
#### version 1.x of scriptt.sh if you still need the Debug Handler)
#### function executeCommandAndLogSTDERR rewritten
#### removed the function CheckParameterCount
#### use lsb_release in Linux to retrieve OS infos if available
#### minor fixes for code and comments
#### replaced PrintWithTimeStamp with code that does not use awk
#### isNumber replaced with code that does not use sed
####
#### 2.0.0.1 06.08.2013 /bs
#### added the variable __MACHINE_SUB_CLASS. Possible values
#### for sun4v machines: either "GuestLDom" or "PrimaryLDom"
####
#### 2.0.0.2 01.09.2013 /bs
#### added the variables __SYSCMDS and __SYSCMDS_FILE
####
#### 2.0.0.3 16.12.2013 /bs
#### now the Log-* functions return ${__TRUE} if a message is printed
#### and ${__FALSE} if not
####
#### 2.0.0.4 01.01.2014 /bs
#### the alias __settrap is renamed to settraps (with leading s)
#### two new aliase are defined: __ingoretraps and __unsettraps
#### whence function for non-ksh compatible shells rewritten
#### without using ksh
#### the switch -D is now used to toggle debug switches
#### known debug switches:
#### help -- print the usage help for -D
#### msg -- log debug messages to /tmp/<scriptname>.<pid>.debug
#### trace -- activate tracing to the file /tmp/<scriptname>.<pid>.trace
#### AskUser now accepts also "yes" and "no"
#### function IsFunctionDefined rewritten
#### now __LOGON_USERID and __USERID are equal to $LOGNAME until I
#### find a working solution
#### (the code in the previous version did not work if STDIN is not a tty)
####
#### 2.0.0.5 08.01.2014 /bs
#### added the function executeFunctionIfDefined
####
#### 2.0.0.6 17.01.2014 /bs
#### added the function PrintLine
#### added the debug options fn_to_stderr and fn_to_tty
#### max. return value for a function is 255 and therefor
#### the function for the stack and the functions pos and lastpo
#### now abort the script if a value greater than 255 should be returned
#### added the variables __HASHTAG, __SCRIPT_SHELL, and __SCRIPT_SHELL_OPTIONS
####
#### ----------------
#### Version variables
####
#### __SCRIPT_VERSION - the version of your script
####
####
typeset -r __SCRIPT_VERSION="v1.0.3"
####
#### __SCRIPT_TEMPLATE_VERSION - version of the script template
####
typeset -r __SCRIPT_TEMPLATE_VERSION="2.0.0.6 17.01.2014"
####
#### ----------------
####
##R# Predefined return codes:
##R# ------------------------
##R#
##R# 1 - show usage and exit
##R# 2 - invalid parameter found
##R#
##R# 210 - 233 reserved for the runtime system
##R# 234 - The return value is greater than 255 in function x
##R# 235 - Invalid debug switch found
##R# 236 - syntax error
##R# 237 - Can not write to the debug log file
##R# 238 - unsupported Operating system
##R# 239 - script runs in a not supported zone
##R# 240 - internal error
##R# 241 - a command ended with an error (set -e is necessary to activate this trap)
##R# 242 - the current user is not allowed to execute this script
##R# 243 - invalid machine architecture
##R# 244 - invalid processor type
##R# 245 - invalid machine platform
##R# 246 - error writing the config file
##R# 247 - include script not found
##R# 248 - unsupported OS version
##R# 249 - Script not executed by root
##R# 250 - Script is already running
##R#
##R# 251 - QUIT signal received
##R# 252 - User break
##R# 253 - TERM signal received
##R# 254 - unknown external signal received
##R#
#### ----------------
#### Used environment variables
####
#
# The variable __USED_ENVIRONMENT_VARIABLES is used in the function ShowUsage
#
__USED_ENVIRONMENT_VARIABLES="
#### __DEBUG_CODE
#### __RT_VERBOSE_LEVEL
#### __QUIET_MODE
#### __VERBOSE_MODE
#### __VERBOSE_LEVEL
#### __OVERWRITE_MODE
#### __USER_BREAK_ALLOWED
#### __NO_TIME_STAMPS
#### __NO_HEADERS
#### __USE_COLORS
#### __USE_RBAC
#### __RBAC_BINARY
#### __TEE_OUTPUT_FILE
#### __INFO_PREFIX
#### __WARNING_PREFIX
#### __ERROR_PREFIX
#### __RUNTIME_INFO_PREFIX
#### __NO_CLEANUP
#### __NO_EXIT_ROUTINES
#### __NO_TEMPFILES_DELETE
#### __NO_TEMPMOUNTS_UMOUNT
#### __NO_TEMPDIR_DELETE
#### __NO_FINISH_ROUTINES
#### __CLEANUP_ON_ERROR
#### __CREATE_DUMP
#### __DUMP_ALREADY_CREATED
#### __DUMPDIR
#### __USE_ONLY_KSH88_FEATURES
#### CONFIG_FILE
"
####
#
# binaries and scripts used in this script:
#
# awk basename cat cp cpio cut date dd dirname egrep expr find grep id ln ls nawk pwd
# reboot rm sed sh tee touch tty umount uname who zonename
#
# /usr/bin/pfexec
# /usr/ucb/whoami or $( whence whoami )
# /usr/openwin/bin/resize or $( whence resize )
#
# AIX: oslevel
#
# Linux: lsb_release
#
# -----------------------------------------------------------------------------
# variables for the trap handler
__FUNCTION="main"
#
# Note: The usage of the variable LINENO is different in the various ksh versions
#
# alias to install the trap handler
#
alias __settraps="
trap 'GENERAL_SIGNAL_HANDLER SIGHUP \${LINENO} \${__FUNCTION}' 1
trap 'GENERAL_SIGNAL_HANDLER SIGINT \${LINENO} \${__FUNCTION}' 2
trap 'GENERAL_SIGNAL_HANDLER SIGQUIT \${LINENO} \${__FUNCTION}' 3
trap 'GENERAL_SIGNAL_HANDLER SIGTERM \${LINENO} \${__FUNCTION}' 15
trap 'GENERAL_SIGNAL_HANDLER SIGUSR1 \${LINENO} \${__FUNCTION}' USR1
trap 'GENERAL_SIGNAL_HANDLER SIGUSR2 \${LINENO} \${__FUNCTION}' USR2
"
# alias to reset all traps to the defaults
#
alias __unsettraps="
trap - 1
trap - 2
trap - 3
trap - 15
trap - USR1
trap - USR2
"
#### ----------------
#### ##### general hints
####
#### Do not use variable names beginning with __ (these are reserved for
#### internal use)
####
# save the language setting and switch the language temporary to C
#
__SAVE_LANG="${LANG}"
LANG=C
export LANG
# -----------------------------------------------------------------------------
#### ##### constants
####
#### __TRUE - true (0)
#### __FALSE - false (1)
####
####
typeset -r __TRUE=0
typeset -r __FALSE=1
# -----------------------------------------------------------------------------
#### __KSH_VERSION - ksh version (either 88 or 93)
#### If the script is not executed by ksh the shell is compatible to
### ksh version $__KSH_VERSION
####
__KSH_VERSION=88 ; f() { typeset __KSH_VERSION=93 ; } ; f ;
# use ksh93 features?
#
if [ "${__KSH_VERSION}"x = "93"x ] ; then
__USE_ONLY_KSH88_FEATURES=${__USE_ONLY_KSH88_FEATURES:=${__FALSE}}
else
__USE_ONLY_KSH88_FEATURES=${__USE_ONLY_KSH88_FEATURES:=${__TRUE}}
fi
#### __OS - Operating system (e.g. SunOS)
####
__OS="$( uname -s )"
# ----------------------------------------------------------------------
# read the hash tag of the script
#
#### __HASHTAG - hash tag of the script
#### __SCRIPT_SHELL - shell in the hash tag of the script
#### __SCRIPT_SHELL_OPTIONS - shell options in the hash tag of the script
####
####
__HASHTAG="$( head -1 $0 )"
__SCRIPT_SHELL="${__HASHTAG#*!}"
__SCRIPT_SHELL="${__SCRIPT_SHELL% *}"
__SCRIPT_SHELL_OPTIONS="${__HASHTAG#* }"
[ "${__SCRIPT_SHELL_OPTIONS}"x = "${__HASHTAG}"x ] && __SCRIPT_SHELL_OPTIONS=""
# -----------------------------------------------------------------------------
# specific settings for the various operating systems and shells
#
#
case ${__OS} in
CYGWIN* )
set +o noclobber
__SHELL_FIELD=9
;;
SunOS | AIX )
__SHELL_FIELD=9
;;
* )
__SHELL_FIELD=8
;;
esac
# -----------------------------------------------------------------------------
# specific settings for various shells
#
#### __SHELL - name of the current shell executing this script
####
__SHELL="$( ps -f -p $$ | grep -v PID | tr -s " " | cut -f${__SHELL_FIELD} -d " " )"
__SHELL=${__SHELL##*/}
: ${__SHELL:=ksh}
case "${__SHELL}" in
"bash" )
# set shell options for alias expanding if running in bash
shopt -s expand_aliases
;;
esac
# -----------------------------------------------------------------------------
# define whence if necessary
#
# old definition for whence:
#
# whence whence 2>/dev/null 1>/dev/null || function whence { ksh whence -p $* ; }
# new definition for whence:
#
whence whence 2>/dev/null 1>/dev/null || function whence {
if typeset -f $1 1>/dev/null ; then
echo $1
elif alias $1 2>/dev/null 1>/dev/null ; then
echo $1
else
which $1 2>/dev/null
fi
}
#### --------------------------------------------------------------------------
#### internal variables
# -----------------------------------------------------------------------------
####
#### __LOG_DEBUG_MESSAGES - log debug messages if set to true
#### This can be activated with the parameter -D msg
####
__LOG_DEBUG_MESSAGES=${__FALSE}
# -----------------------------------------------------------------------------
#### __ACTIVATE_TRACE - log trace messages (set -x) if set to true
#### This can be activated with the parameter -D trace
###
__ACTIVATE_TRACE=${__FALSE}
# -----------------------------------------------------------------------------
####
#### __TRAP_SIGNAL - current trap caught by the trap handler
#### This is a global variable that can be used in the exit routines
####
__TRAP_SIGNAL=""
# -----------------------------------------------------------------------------
#### __USE_RBAC - set this variable to ${__TRUE} to execute this script
#### with RBAC
#### default is ${__FALSE}
####
#### Note: You can also set this environment variable before starting the script
####
: ${__USE_RBAC:=${__FALSE}}
# -----------------------------------------------------------------------------
#### __RBAC_BINARY - pfexec binary
####
#### default is /usr/bin/pfexec
####
#### Note: You can also set this environment variable before starting the script
####
: ${__RBAC_BINARY:=/usr/bin/pfexec}
# -----------------------------------------------------------------------------
#
# user executing this script (works only if using a ssh session with specific
# ssh versions that export these variables!)
#
SCRIPT_USER="$( echo $SSH_ORIGINAL_USER | tr "=" " " | cut -f 5 -d " " )"
SCRIPT_USER_MSG="${SCRIPT_USER}"
# -----------------------------------------------------------------------------
#### __TEE_OUTPUT_FILE - name of the output file if called with the parameter -T
#### default: /var/tmp/$( basename $0 ).$$.tee.log
####
#### Note: You can also set this environment variable before starting the script
####
: ${__TEE_OUTPUT_FILE:=/var/tmp/${0##*/}.$$.tee.log}
# -----------------------------------------------------------------------------
# process the parameter -q or --quiet
#
if [[ \ $*\ == *\ -q* || \ $*\ == *\ --quiet\ * ]] ; then
__NO_HEADERS=${__TRUE}
__QUIET_MODE=${__TRUE}
fi
# -----------------------------------------------------------------------------
# config file found or not
#
__CONFIG_FILE_FOUND=""
# -----------------------------------------------------------------------------
# use the parameter -T or --tee to automatically call the script and pipe
# all output to tee
if [ "${__PPID}"x = ""x ] ; then
__PPID=$PPID ; export __PPID
if [[ \ $*\ == *\ -T* || \ $*\ == *\ --tee\ * ]] ; then
echo "Saving STDOUT and STDERR to \"${__TEE_OUTPUT_FILE}\" ..."
exec $0 $@ 2>&1 | tee -a "${__TEE_OUTPUT_FILE}"
__MAINRC=$?
echo "STDOUT and STDERR saved in \"${__TEE_OUTPUT_FILE}\"."
exit ${__MAINRC}
fi
fi
: ${__PPID:=$PPID} ; export __PPID
# -----------------------------------------------------------------------------
#
# Set the variable ${__USE_RBAC} to ${__TRUE} to activate RBAC support
#
# Allow the use of RBAC to control who can access this script. Useful for
# administrators without root permissions
#
if [ "${__USE_RBAC}" = "${__TRUE}" ] ; then
if [ "$_" != "${__RBAC_BINARY}" -a -x "${__RBAC_BINARY}" ]; then
__USE_RBAC=${__FALSE} "${__RBAC_BINARY}" $0 $*
exit $?
else
echo "${0%%*/} ERROR: \"${__RBAC_BINARY}\" not found or not executable!" >&2
exit 238
fi
fi
# -----------------------------------------------------------------------------
####
#### ##### defined variables that may be changed
####
#### __DEBUG_CODE - code executed at start of every sub routine
#### Note: Use always "__DEBUG_CODE="eval ..." if you want to use variables or aliases
#### Default debug code : none
####
#### sample debug code:
#### __DEBUG_CODE=" eval echo Entering the subroutine \${__FUNCTION} ... "
####
#### Note: Use an include script for more complicated debug code, e.g.
#### __DEBUG_CODE=" eval . /var/tmp/mydebugcode"
####
__DEBUG_CODE=""
#### __FUNCTION_INIT - code executed at start of every sub routine
#### (see the hints for __DEBUG_CODE)
#### Default init code : install the trap handlers
####
# __FUNCTION_INIT=" eval __settraps; echo \"Now in function \${__FUNCTION}\" "
__FUNCTION_INIT=" eval __settraps "
### variables for debugging
###
### __NO_CLEANUP - do not call the cleanup routine at all at script end if ${__TRUE}
###
: ${__NO_CLEANUP:=${__FALSE}}
#### __NO_EXIT_ROUTINES - do not execute the exit routines if ${__TRUE}
####
: ${__NO_EXIT_ROUTINES:=${__FALSE}}
#### __NO_TEMPFILES_DELETE - do not remove temporary files at script end if ${__TRUE}
####
: ${__NO_TEMPFILES_DELETE:=${__FALSE}}
#### __NO_TEMPMOUNTS_UMOUNT - do not umount temporary mount points at script end if ${__TRUE}
####
: ${__NO_TEMPMOUNTS_UMOUNT:=${__FALSE}}
#### __NO_TEMPDIR_DELETE - do not remove temporary directories at script end if ${__TRUE}
####
: ${__NO_TEMPDIR_DELETE:=${__FALSE}}
#### __NO_FINISH_ROUTINES - do not execute the finish routeins at script end if ${__TRUE}
####