-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patheasycd.sh
4801 lines (3852 loc) · 124 KB
/
easycd.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
#!/bin/ksh
## last exit code defined is 95
## -----------------------------------------------------------------------------
##
## easycd.sh - process CDs, DVDs, or ISO images
##
## Author: Bernd Schemmer (Bernd.Schemmer@gmx.de)
##
## Version: see variable ${__SCRIPT_VERSION} below
## (see variable ${__SCRIPT_TEMPLATE_VERSION} for the template
## version used)
##
## Supported OS: Solaris 8 and newer
##
##
## Description
##
## shell script to process CDs, DVDs, or ISO images
##
## History
##
## 05/01/03 /bs initial release
## 05/02/01 /bs minor corrections
## 05/02/15 /bs minor corrections
## 05/04/07 /bs added verify commands & switches
## 06/04/01 /bs added parameter "-x createconfig"
## 06/02/14 /bs added add. parameter for mkisofs
## (parameter writecdrom writecd writedvd createISOimg)
## 06/07/14 /bs corrected a bug in the color definition for COLOR_OFF
##
##
## Predefined return codes:
##
## 1 - show usage and exit
## 2 - invalid parameter found
##
## 498 - Script not executed by root
## 499 - Script is already running
##
## 500 - User break
## 501 - QUIT signal received
## 502 - CTRL-C signal received
## 503 - TERM signal received
## 504 - external signal received
##
# -----------------------------------------------------------------------------
## __SHORT_DESC - short description (for help texts, etc)
## Change to your need
##
typeset -r __SHORT_DESC=" wrapper script for cdrecord & co"
## __SCRIPT_VERSION - the version of your script
##
typeset -r __SCRIPT_VERSION="0.06"
##
## --- defined read only variables
##
## __SCRIPT_TEMPLATE_VERSION - version of the template
##
typeset -r __SCRIPT_TEMPLATE_VERSION="1.15.1 14/12/2004"
## __TRUE - true (0)
## __FALSE - false (1)
##
typeset -r __TRUE=0
typeset -r __FALSE=1
##
## --- defined variables that may be changed
##
## __MUST_BE_ROOT (def.: false)
## set to ${__TRUE} for scripts that must be executed by root only
##
__MUST_BE_ROOT=${__TRUE}
## __ONLY_ONCE (def.: false)
## set to ${__TRUE} for scripts that can not run more than one
## instance at the same time
##
__ONLY_ONCE=${__FALSE}
## __VERBOSE_MODE - print verbose messages (def. false)
## use the parameter -v to set this variable to true
##
__VERBOSE_MODE=${__FALSE}
## __VERBOSE_LEVEL - count of -v parameter (def. 0)
##
typeset -i __VERBOSE_LEVEL=0
## __QUIET_MODE - print no messages (def.: false)
## use the parameter -q to set this variable to true
##
__QUIET_MODE=${__FALSE}
## __USER_BREAK_ALLOWED - CTRL-C aborts program or not (def. true)
## (no parameter to change this variable)
##
__USER_BREAK_ALLOWED=${__TRUE}
## __OVERWRITE mode - overwrite existing files or not (def. false
## use the parameter -O to change this variable
__OVERWRITE_MODE=${__FALSE}
## __DEBUG_MODE - use single step mode for main (def. false)
## use the parameter -D to set this variable
##
__DEBUG_MODE=${__FALSE}
__SCRIPT_ARRAY[0]=0
# internal variables for the single-step routine
typeset -i __BREAKPOINT_LINE=0
typeset -i __STEP_COUNT=0
typeset -i __TRACE_COUNT=0
# -----------------------------------------------------------------------------
#
# aliase
alias traceon="set -x"
alias traceoff="set +x"
alias invers="echo \${__COLOR_REVERSE}\c"
alias normal="echo \${__COLOR_OFF}"
alias red_on_white="echo \${__COLOR_FG_RED}\${__COLOR_BG_WHITE}\c"
# -----------------------------------------------------------------------------
# init the global variables
#
##
## --- defined variables that should not be changed
##
__THISRC=0
__USER_RESPONSE_IS=""
## __SCRIPTNAME - name of the script without the path
##
__SCRIPTNAME="$( basename $0 )"
## __SCRIPTDIR - path of the script (as entered by the user!)
##
__SCRIPTDIR="$( dirname $0 )"
## __REAL_SCRIPTDIR - path of the script (real path, maybe a link)
##
__REAL_SCRIPTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )
## __CONFIG_FILE - name of the config file
##
__CONFIG_FILE="${__SCRIPTNAME%.*}.conf"
__BASE_CONFIG_FILE="easycd.conf"
## __HOSTNAME - hostname
##
__HOSTNAME="$( uname -n )"
## __NODENAME - nodename
##
__NODENAME=${__HOSTNAME}
[ -f /etc/nodename ] && __NODENAME="$( cat /etc/nodename )"
## __OS - Operating system (e.g. SunOS)
##
__OS="$( uname -s )"
## __OS_VERSION - Operating system version (e.g 5.8)
##
__OS_VERSION="$( uname -r )"
## __OS_RELEASE - Operating system release (e.g. Generic_112233-08)
##
__OS_RELEASE="$( uname -v )"
## __MACHINE_CLASS - Machine class (e.g sun4u)
##
__MACHINE_CLASS="$( uname -m )"
## __MACHINE_TYPE - machine type (e.g. SUNW,Ultra-4)
##
__MACHINE_TYPE="$( uname -i )"
## __MACHINE_ARC - machine architecture (e.g. sparc)
##
__MACHINE_ARC="$( uname -p )"
## __START_DIR - working directory when starting the script
##
__START_DIR="$( pwd )"
## __LOGFILE - fully qualified name of the logfile used
##
__DEF_LOGFILE="/var/tmp/${__SCRIPTNAME}.$$.LOG"
__LOGFILE=${__DEF_LOGFILE}
##
## temporary files:
##
## __TEMPFILE1, __TEMPFILE2
__TEMPFILE1="/tmp/${__SCRIPTNAME}.$$.TEMP1"
__TEMPFILE2="/tmp/${__SCRIPTNAME}.$$.TEMP2"
__LIST_OF_TEMPFILES="${__TEMPFILE1} ${__TEMPFILE2}"
# lock file (used if $__ONLY_ONCE is true)
__LOCKFILE="/tmp/${__SCRIPTNAME}.lock"
__LOCKFILE_CREATED=${__FALSE}
##
## __EXITROUTINES - list of routines that should be executed before the
## script ends
##
__EXITROUTINES=""
# reboot necessary ?
## __REBOOT_REQUIRED - set to true to reboot automatically at
## script end (def. false)
##
__REBOOT_REQUIRED=${__FALSE}
## __REBOOT_PARAMETER - parameter for the reboot command (def.: none)
##
__REBOOT_PARAMETER=""
## __PRINT_LIST_OF_WARNINGS_MSGS
## print the list of warning messages at program end (def. false)
##
__PRINT_LIST_OF_WARNINGS_MSGS=${__FALSE}
## No of warnings found
##
typeset -i __NO_OF_WARNINGS=0
## __PRINT_LIST_OF_ERROR_MSGS
## print the list of error messages at program end (def. false)
##
__PRINT_LIST_OF_ERROR_MSGS=${__FALSE}
## List of warning messages
##
__LIST_OF_WARNINGS=""
## No of errors found
##
typeset -i __NO_OF_ERRORS=0
## List of error messages
##
__LIST_OF_ERRORS=""
## __LOGON_USERID - ID of the user opening the session
##
typeset REST=
who am i | read __LOGIN_USERID __REST
## __USERID - ID of the user executing this script (e.g. xtrnaw7)
##
__USERID=${__LOGIN_USERID}
[ -x /usr/ucb/whoami ] && /usr/ucb/whoami | read __USERID __REST
## __RUNLEVEL - current runlevel
who -r | read _DUMMY1 __DUMMY2 __RUNLEVEL __REST
# -----------------------------------------------------------------------------
# color variables
## __USE_COLORS - use colors (def. false) use the parameter -a to
## set this variable
__USE_COLORS=${__TRUE}
##
## Colorattributes:
## __COLOR_OFF, __COLOR_BOLD, __COLOR_NORMAL, - normal, __COLOR_UNDERLINE
## __COLOR_BLINK, __COLOR_REVERSE, __COLOR_INVISIBLE
##
__COLOR_OFF="\033[0m"
__COLOR_BOLD="\033[1m"
__COLOR_NORMAL="\033[2m"
__COLOR_UNDERLINE="\033[4m"
__COLOR_BLINK="\033[5m"
__COLOR_REVERSE="\033[7m"
__COLOR_INVISIBLE="\033[8m"
##
## Foreground Color variables:
## __COLOR_FG_BLACK, __COLOR_FG_RED, __COLOR_FG_GREEN, __COLOR_FG_YELLOW
## __COLOR_FG_BLUE, __COLOR_FG_MAGENTA, __COLOR_FG_CYAN, __COLOR_FG_WHITE
##
## Background Color variables:
## __COLOR_BG_BLACK, __COLOR_BG_RED, __COLOR_BG_GREEN, __COLOR_BG_YELLOW
## __COLOR_BG_BLUE, __COLOR_BG_MAGENTA, __COLOR_BG_CYAN, __COLOR_BG_WHITE
##
__COLOR_FG_BLACK="\033[30m"
__COLOR_FG_RED="\033[31m"
__COLOR_FG_GREEN="\033[32m"
__COLOR_FG_YELLOW="\033[33m"
__COLOR_FG_BLUE="\033[34m"
__COLOR_FG_MAGENTA="\033[35m"
__COLOR_FG_CYAN="\033[36m"
__COLOR_FG_WHITE="\033[37m"
__COLOR_BG_BLACK="\033[40m"
__COLOR_BG_RED="\033[41m"
__COLOR_BG_GREEN="\033[42m"
__COLOR_BG_YELLOW="\033[43m"
__COLOR_BG_BLUE="\033[44m"
__COLOR_BG_MAGENTA="\033[45m"
__COLOR_BG_CYAN="\033[46m"
__COLOR_BG_WHITE="\033[47m"
# position cursor: ESC[row,colH or ESC[row;colf (1,1 = upper left corner)
# Clear rest of line: ESC[K
# Clear screen: ESC[2J
# Save Cursor Pos ESC[s
# Restore Cursor Pos ESC[u
# Cursor Up # lines ESC{colsA
# Cursor down # lines ESC{colsB
# Cursor right # columns ESC{colsC
# Cursor left # columns ESC{colsD
# Get Cursor Pos ESC[6n
#
# -----------------------------------------------------------------------------
##
## --- defined sub routines
##
## --------------------------------------
## File
##
## read the config file
##
## usage: ReadConfigFile [configfile]
##
## where: configfile - name of the config file
## default: search scriptname.conf in the current directory and
## in the home directory (in this order)
##
## returns: ${__TRUE} - ok config read
## ${__FALSE} - error config file not found or not readable
##
ReadConfigFile() {
typeset THIS_CONFIG_FILE="$1"
typeset THISRC=${__FALSE}
if [ "${THIS_CONFIG_FILE}"x = ""x ] ; then
THIS_CONFIG_FILE="./${__CONFIG_FILE}"
if [ ! -f "${THIS_CONFIG_FILE}" ] ; then
THIS_CONFIG_FILE="${HOME}/${__CONFIG_FILE}"
fi
else
[ ! -f "${THIS_CONFIG_FILE}" ] && THIS_CONFIG_FILE="${HOME}/${THIS_CONFIG_FILE}"
fi
[ "$( basename "${THIS_CONFIG_FILE}" )"x = "${THIS_CONFIG_FILE}"x ] && THIS_CONFIG_FILE="./${THIS_CONFIG_FILE}"
if [ -f "${THIS_CONFIG_FILE}" ] ; then
LogMsg "Reading the config file \"${THIS_CONFIG_FILE}\" ..."
. "${THIS_CONFIG_FILE}"
THISRC=${__TRUE}
else
LogInfo "Config file \"${THIS_CONFIG_FILE}\" NOT found"
fi
return ${THISRC}
}
## --------------------------------------
## WriteConfigFile
##
## write the config file
##
## usage: WriteConfigFile [configfile]
##
## where: configfile - name of the config file
## default: write scriptname.conf in the current directory
##
## returns: ${__TRUE} - ok config written
## ${__FALSE} - error writing the config file
##
WriteConfigFile() {
typeset THIS_CONFIG_FILE="$1"
typeset THISRC=${__FALSE}
[ "${THIS_CONFIG_FILE}"x = ""x ] && THIS_CONFIG_FILE="./${__CONFIG_FILE}"
LogMsg "Writing the config file \"${THIS_CONFIG_FILE}\" ..."
cat <<EOT >"${THIS_CONFIG_FILE}"
# config file for $0
${CONFIG_PARAMETER}
EOT
[ $? -eq 0 ] && THISRC=${__TRUE}
return ${THISRC}
}
## --------------------------------------
## CreateConfigFile
##
## create the configuration file
##
## usage: CreateConfigFile [configfile]
##
## returns: ${__TRUE} - ok config written
## ${__FALSE} - error writing the config file
##
CreateConfigFile() {
typeset THIS_CONFIG="$1"
[ "${THIS_CONFIG}"x = ""x ] && THIS_CONFIG="${HOME}/${__CONFIG_FILE}"
typeset THISRC=${__TRUE}
if [ -f "${THIS_CONFIG}" -a ${__OVERWRITE_MODE} = ${__FALSE} ] ; then
LogWarning "The config file \"${THIS_CONFIG}\" already exist"
else
WriteConfigFile ${THIS_CONFIG}
THISRC=$?
fi
}
## --------------------------------------
## InstallScript
##
## create the symbolic links and the configuration file
##
## usage: InstallScript [configfile]
##
## returns: ${__TRUE} - ok config written
## ${__FALSE} - error writing the config file
##
InstallScript() {
typeset THIS_CONFIG="$1"
[ "${THIS_CONFIG}"x = ""x ] && THIS_CONFIG="${HOME}/${__CONFIG_FILE}"
typeset THISRC=${__TRUE}
if [ "${__SCRIPTNAME}"x = "easycd.sh"x ] ; then
LogMsg "Creating the symbolic links in the current directory ..."
for CURLINK in ${POSSIBLE_SCRIPT_NAMES} ; do
LogMsg "Processing \"${CURLINK}\" ..."
if [ -a ${CURLINK} ] ; then
if [ ${__OVERWRITE_MODE} = ${__TRUE} ] ; then
LogInfo "Removing the existing link \"${CURLINK}\" ..."
rm ${CURLINK}
else
LogWarning "\"${CURLINK}\" already exists."
continue
fi
fi
LogMsg " Creating the link ${CURLINK} -> $0"
ln -s $0 ${CURLINK}
done
fi
CreateConfigFile "${THIS_CONFIG}"
THISRC=$?
return ${THISRC}
}
## --------------------------------------
## UnInstallScript
##
## remove the symbolic links
##
## usage: UnInstallScript
##
##
## returns: ${__TRUE} - ok config written
## ${__FALSE} - error writing the config file
##
UnInstallScript() {
LogMsg "Deleting the symbolic links in the current directory ..."
for CURLINK in ${POSSIBLE_SCRIPT_NAMES} ; do
LogMsg "Processing \"${CURLINK}\" ..."
if [ -a ${CURLINK} ] ; then
LogMsg " Removing the link \"${CURLINK}\" ..."
rm ${CURLINK}
else
LogWarning "\"${CURLINK}\" does not exist."
continue
fi
done
[ -f "${HOME}/${__CONFIG_FILE}" ] && LogWarning "Config file \"${HOME}/${__CONFIG_FILE}\" NOT deleted"
return ${__TRUE}
}
## --------------------------------------
## SetWindowTitle
##
## change the title of an X window
##
## usage: SetWindowTitle "newtitle"
##
## returns: -
##
## Note
##
## Original Author of this function (xtitle) is:
## William Seppeler
## Email:seppeler@yahoo.com
##
SetWindowTitle(){
typeset NEW_TITLE="$*"
typeset -r ESC='\033'
typeset -r BEL='\007'
case $TERM in
xterm*|dtterm|aixterm|rxvt)
print "${ESC}]0;${NEW_TITLE}${BEL}\c";;
sun-cmd)
print "${ESC}]l${NEW_TITLE}${ESC}\\\\\c";;
#print "${ESC}]L${NEW_TITLE}${ESC}\\\\\c";;
hpterm)
print "${ESC}&f0k${#${NEW_TITLE}}D$*\c";;
#print "${ESC}&f-1k${#${NEW_TITLE}}D$*\c";;
iris-ansi)
print "${ESC}P1.y${NEW_TITLE}${ESC}\\\\\c";;
#print "${ESC}P3.y${NEW_TITLE}${ESC
esac
}
## --------------------------------------
## SetDISPLAY
##
## try to set the variable DISPLAY
##
## usage: SetDISPLAY
##
## returns: DISPLAY is set or not
##
SetDISPLAY(){
#
# If the DISPLAY variable is not set, then assume that the person
# is not at the console, so try to figure out where they are logged
# in from and send the display back to them.
#
if [ -z "$DISPLAY" ] ; then
tty=`tty | sed "s@/dev/@@"`
if [ "$tty" != "not a tty" ] ; then
machine=`who | grep $tty | sed -e 's/^.*(//' -e 's/)//'`
DISPLAY="${machine}:0" ; export DISPLAY
fi
fi
}
## --------------------------------------
## CheckYNParameter
##
## check if a parameter is y, n, 0, or 1
##
## usage: CheckYNParameter parameter
##
## returns: ${__TRUE} - the parameter is equal to yes
## ${__FALSE} - the parameter is equal to no
## 255 - the parameter is neither y nor no
##
CheckYNParameter(){
typeset THISRC=255
case $1 in
"y" | "Y" | "yes" | "YES" | 0 ) THISRC=${__TRUE} ;;
"n" | "N" | "no" | "NO" | 1 ) THISRC=${__FALSE} ;;
* ) THISRC=255 ;;
esac
return ${THISRC}
}
## --------------------------------------
## ConvertToYesNo
##
## convert the value of a variable to y or n
##
## usage: ConvertToYesNo parameter
##
## returns: -
## prints y, n or ? to STDOUT
##
ConvertToYesNo(){
case $1 in
"y" | "Y" | "yes" | "YES" | 0 ) echo "y" ;;
"n" | "N" | "no" | "NO" | 1 ) echo "n" ;;
* ) echo "?" ;;
esac
}
## ---------------------------------------
## SwitchOption
##
## switch an option from true to false or vice versa
##
## usage: SwitchOption optionname
##
## returns: the new value (either ${__TRUE} or ${__FALSE} )
##
SwitchOption() {
typeset THISRC=0
typeset THIS_SWITCH=$1
eval "[ \${${THIS_SWITCH}} = \${__TRUE} ] && ${THIS_SWITCH}=\${__FALSE} || ${THIS_SWITCH}=\${__TRUE}"
eval THISRC=\${${THIS_SWITCH}}
}
## --------------------------------------
## CheckInputDevice
##
## check if the input device is a terminal
##
## usage: CheckInputDevice
##
## returns: 0 - the input device is a terminal (interactive)
## 1 - the input device is NOT a terminal
##
CheckInputDevice(){
tty -s
return $?
}
## --------------------------------------
## GetProgramDirectory
##
## get the directory where a program resides
##
## usage: GetProgramDirectory [programpath/]programname [resultvar]
##
## returns:
## the variable PRGDIR contains <the directory with the program>
## if the parameter resultvar is missing
##
GetProgramDirectory() {
typeset PRG=""
typeset RESULTVAR=$2
if [ ! -L $1 ] ; then
PRG=$( cd -P -- "$(dirname -- "$(command -v -- "$1")")" && pwd -P )
else
# resolv links - $1 may be a softlink
PRG="$1"
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
PRG="$(dirname $PRG)"
fi
if [ "${RESULTVAR}"x != ""x ] ; then
eval ${RESULTVAR}=$PRG
else
PRGDIR=$PRG
fi
}
## --------------------------------------
## substr
##
## get a substring of a string
##
## usage: variable=` substr sourceStr pos length`
## or substr sourceStr pos length resultStr
##
## returns: 1 - parameter missing
## 0 - parameter okay
##
substr() {
typeset resultstr=""
typeset THISRC=1
if [ "$1"x != ""x ] ; then
typeset s=$1
typeset p=$2
typeset l=$3
[ "$l"x = ""x ] && l=${#s}
[ "$p"x = ""x ] && p=1
resultstr="$( echo $s | cut -c${p}-$((${p}+${l}-1)) )"
THISRC=0
else
THISRC=1
resultstr="$1"
fi
if [ "$4"x != ""x ] ; then
eval $4=\"${resultstr}\"
else
echo ${resultstr}
fi
return ${THISRC}
}
## --------------------------------------
## replacestr
##
## replace a substring with another substring
##
## usage: variable=` replacestr sourceStr oldsubStr newsubStr`
## or replacestr sourceStr oldsubStr newsubStr resultvariable
##
## returns: 0 - substring replaced
## 1 - substring not found
## 3 - error, parameter missing
##
## writes the substr to STDOUT if resultvariable is missing
##
replacestr() {
typeset THISRC=3
typeset sourcestring=$1
typeset oldsubStr=$2
typeset newsubStr=$3
if [ "${sourcestring}"x != ""x -a "${oldsubStr}"x != ""x ] ; then
if [[ "${sourcestring}" == *${oldsubStr}* ]] ; then
sourcestring="${sourcestring%%${oldsubStr}*}${newsubStr}${sourcestring#*${oldsubStr}}"
THISRC=0
else
THISRC=1
fi
fi
if [ "$4"x != ""x ] ; then
eval $4=\"${sourcestring}\"
else
echo $sourcestring
fi
return ${THISRC}
}
## --------------------------------------
## pos
##
## check if a string is a substring of another string
##
## usage: pos searchstring sourcestring
##
## returns: 0 - searchstring is not part of sourcestring
## else the position of searchstring in sourcestring
##
pos() {
typeset searchstring=$1
typeset sourcestring=$2
if [[ "${sourcestring}" == *${searchstring}* ]] ; then
typeset f=${sourcestring%%${searchstring}*}
return $(( ${#f}+1 ))
else
return 0
fi
}
## --------------------------------------
## lastpos
##
## check if a string is a substring of another string
##
## usage: lastpos searchstring sourcestring
##
## returns: 0 - searchstring is not part of sourcestring
## else the position of searchstring in sourcestring
##
lastpos() {
typeset searchstring=$1
typeset sourcestring=$2
if [[ "${sourcestring}" == *${searchstring}* ]] ; then
typeset f=${sourcestring%${searchstring}*}
return $(( ${#f}+1 ))
else
return 0
fi
}
## --------------------------------------
## isNumber
##
## check if a value is an integer
##
## usage: isNumber testValue
##
## returns: 0 - testValue is a number else not
##
isNumber() {
typeset TESTVAR="$(echo $1 | sed 's/[0-9]*//g' )"
[ "${TESTVAR}"x = ""x ] && return ${__TRUE} || return ${__FALSE}
}
## --------------------------------------
## toUppercase
##
## convert a string to uppercase
##
## usage: toUppercase sourceString | read resultString
## or targetString=` toUppercase sourceString`
## or toUppercase sourceString resultString
##
## returns: writes the converted string to STDOUT if resultString is missing
##
toUppercase() {
typeset -u testvar=$1
if [ "$2"x != ""x ] ; then
eval $2=\"${testvar}\"
else
echo ${testvar}
fi
}
## --------------------------------------
## toLowercase
##
## convert a string to lowercase
##
## usage: toLowercase sourceString | read resultString
## or targetString=` toLowercase sourceString`
## or toLowercase sourceString resultString
##
## returns: writes the converted string to STDOUT if resultString is missing
##
toLowercase() {
typeset -l testvar=$1
if [ "$2"x != ""x ] ; then
eval $2=\"${testvar}\"
else
echo ${testvar}
fi
}
## --------------------------------------
## executeCommand
##
## execute a command
##
## usage: executeCommand command parameter
##
## returns: the RC of the executed command
##
executeCommand() {
set +e
eval "$@"
}
## --------------------------------------
## executeCommandAndLog
##
## execute a command and write STDERR and STDOUT to the logfile
##
## usage: executeCommandAndLog command parameter
##
## returns: the RC of the executed command
##
executeCommandAndLog() {
set +e
if [ "${__LOGFILE}"x != ""x -a -f ${__LOGFILE} ] ; then
"$@" 2>&1 | tee -a ${__LOGFILE}
return $?
else
"$@"
return $?
fi
}
## --------------------------------------
## UserIsRoot
##
## validate the user id
##
## usage: UserIsRoot
##
## returns: 0 - the user is root; else not
##
UserIsRoot() {
[ `id | /usr/bin/sed 's/uid=\([0-9]*\)(.*/\1/'` = 0 ] && return 0 || return 1
}
## --------------------------------------
## UserIs
##
## validate the user id
##
## usage: UserIs USERID
##
## where: USERID - userid (e.g oracle)
##
## returns: 0 - the user is this user
## 1 - the user is NOT this user
## 2 - the user does not exist on this machine
## 3 - missing parameter
##
UserIs() {
typeset THISRC=3
typeset USERID=""
if [ "$1"x != ""x ] ; then
THISRC=2
USERID=` grep "^$1:" /etc/passwd | cut -d: -f3`
if [ "${USERID}"x != ""x ] ; then
UID=`id | /usr/bin/sed 's/uid=\([0-9]*\)(.*/\1/'`
[ ${UID} = ${USERID} ] && THISRC=0 || THISRC=1
fi
fi
return ${THISRC}
}
## --------------------------------------
## GetCurrentUID
##
## get the UID of the current user
##
## usage: GetCurrentUID
##
## where: -
##
## returns: the UID
##
GetCurrentUID() {
return `id | /usr/bin/sed 's/uid=\([0-9]*\)(.*/\1/'`
}
## --------------------------------------
## GetUserName
##
## get the name of a user
##
## usage: GetUserName UID
##
## where: UID - userid (e.g 1686)
##
## returns: __USERNAME contains the user name or "" if
## the userid does not exist on this machine
##
GetUserName() {
[ "$1"x != ""x ] && __USERNAME=` grep ":$1:" /etc/passwd | cut -d: -f1` || __USERNAME=""
}
## --------------------------------------
## GetUID
##
## get the UID for a username
##
## usage: GetUID username
##
## where: username - user name (e.g nobody)
##
## returns: __USER_ID contains the UID or "" if
## the username does not exist on this machine
##
GetUID() {
[ "$1"x != ""x ] && __USER_ID=` grep "^$1:" /etc/passwd | cut -d: -f3` || __USER_ID=""
}
# ======================================
## --------------------------------------
## LogMsgInvers
##
## print a message to STDOUT and write it also to the logfile
##
## usage: LogMsgInvers message
##
## returns: -
##
LogMsgInvers() {