File: sh_07.sh

package info (click to toggle)
vim 2%3A9.1.2103-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 93,456 kB
  • sloc: ansic: 433,730; cpp: 6,399; makefile: 4,597; sh: 2,397; java: 2,312; xml: 2,099; python: 1,595; perl: 1,419; awk: 730; lisp: 501; cs: 458; objc: 369; sed: 8; csh: 6; haskell: 1
file content (93 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download | duplicates (2)
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
#!/bin/dash
# Test file to test 'for do done' loops.
# You can start this script like: $0 {-ne -gt -le ...} (all numeric operators 
# are allowed!

# All this works and should be OK

################################################################################
#
# For loop without 'in list'. Uses $1 $2 ... This is a special case!
# This 'for Var, do, done' is a very handy solution AND no real replacement 
# available!
#
Function1 () {

echo "Function1: for loop inside a function:\t\c"
[ "$*" ] || echo "none\c"

for Var
do
    [ 1 $Var 2 ] && echo "OK \c" || echo "no \c"
done
echo

} # End of Function1

################################################################################
#
# For loop with 'in list' $*
#
Function2 () {

echo "Function2: for loop inside a function:\t\c"
for Var in $*
do
    [ 1 $Var 2 ] && echo "OK \c" || echo "no \c"
done ; echo

} # End of Function2

################################################################################
#
# For loop with 'in list' $@. Works the same way as $*
#
Function3 () {

echo "Function3: for loop inside a function:\t\c"
for Var in $@
do
    [ 1 $Var 2 ] && echo "OK \c" || echo "no \c"
done ; echo

} # End of Function3

################################################################################
#
# For loop with 'in list' "$@". Special case. Works like "$1" "$2" ...
#
Function4 () {

echo "Function4: for loop inside a function:\t\c"
for Var in "$@"
do
    [ 1 $Var 2 ] && echo "OK \c" || echo "no \c"
done ; echo

} # End of Function4


################################################################################
# main ### main ### main ### main ### main ### main ### main ### main ### main #
################################################################################
#
# Here is the heart of this script:
#
echo "Processing the following command line arguments: ${*:-none}"
echo "Script:    for loop outside a function:\t\c"
for Var
do
    [ 1 $Var 2 ] && echo "OK \c" || echo "no \c"
done ; echo

# Same as function calls
Function1 -eq -ne -gt -ge -le -lt
Function2 -eq -ne -gt -ge -le -lt
Function3 -eq -ne -gt -ge -le -lt
Function4 -eq -ne -gt -ge -le -lt '-ge 1 -a 2 -ge'

# Now the same call like Function4 but with Function1
Function1 -eq -ne -gt -ge -le -lt '-ge 1 -a 2 -ge'
Function1

exit $?