Ver código fonte

Improved loops

Bob Mottram 9 anos atrás
pai
commit
5ca98a2050
3 arquivos alterados com 19 adições e 19 exclusões
  1. 9
    9
      src/freedombone-addsipuser
  2. 7
    7
      src/freedombone-rmsipuser
  3. 3
    3
      src/freedombone-sipfreeext

+ 9
- 9
src/freedombone-addsipuser Ver arquivo

@@ -43,13 +43,13 @@ function show_help {
43 43
 }
44 44
 
45 45
 function sip_user_exists {
46
-    for line in $(cat $CONFIG_FILE)
47
-    do
46
+    IFS=''
47
+    while read line; do
48 48
         if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
49 49
             USER_EXISTS="yes"
50 50
             return
51 51
         fi
52
-    done
52
+    done < $CONFIG_FILE
53 53
 }
54 54
 
55 55
 function update_sip_user {
@@ -59,8 +59,8 @@ function update_sip_user {
59 59
         rm -f $NEW_CONFIG_FILE
60 60
     fi
61 61
     touch $NEW_CONFIG_FILE
62
-    for line in $(cat $CONFIG_FILE)
63
-    do
62
+    IFS=''
63
+    while read line; do
64 64
         if [ ! $USER_FOUND ]; then
65 65
             if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
66 66
                 USER_FOUND="yes"
@@ -78,7 +78,7 @@ function update_sip_user {
78 78
             fi              
79 79
         fi
80 80
         echo $line >> $NEW_CONFIG_FILE
81
-    done
81
+    done < $CONFIG_FILE
82 82
     mv $NEW_CONFIG_FILE $CONFIG_FILE
83 83
 }
84 84
 
@@ -88,8 +88,8 @@ function add_sip_user {
88 88
         rm -f $NEW_CONFIG_FILE
89 89
     fi
90 90
     touch $NEW_CONFIG_FILE
91
-    for line in $(cat $CONFIG_FILE)
92
-    do
91
+    IFS=''
92
+    while read line; do
93 93
         if [[ "$line" == '</provision>' ]]; then
94 94
             echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
95 95
             echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
@@ -98,7 +98,7 @@ function add_sip_user {
98 98
             echo '</user>' >> $NEW_CONFIG_FILE
99 99
         fi
100 100
         echo $line >> $NEW_CONFIG_FILE
101
-    done
101
+    done < $CONFIG_FILE
102 102
     mv $NEW_CONFIG_FILE $CONFIG_FILE
103 103
     usermod -aG sipwitch $MY_USERNAME
104 104
 }

+ 7
- 7
src/freedombone-rmsipuser Ver arquivo

@@ -41,13 +41,13 @@ function show_help {
41 41
 }
42 42
 
43 43
 function sip_user_exists {
44
-    for line in $(cat $CONFIG_FILE)
45
-    do
44
+    IFS=''
45
+    while read line; do
46 46
         if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
47 47
             USER_EXISTS="yes"
48 48
             return
49 49
         fi
50
-    done
50
+    done < $CONFIG_FILE
51 51
 }
52 52
 
53 53
 function remove_sip_user {
@@ -57,21 +57,21 @@ function remove_sip_user {
57 57
         rm -f $NEW_CONFIG_FILE
58 58
     fi
59 59
     touch $NEW_CONFIG_FILE
60
-    for line in $(cat $CONFIG_FILE)
61
-    do
60
+    IFS=''
61
+    while read line; do
62 62
         if [ ! $USER_FOUND ]; then
63 63
             if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
64 64
                 USER_FOUND="yes"
65 65
             fi
66 66
         fi
67 67
         if [ ! $USER_FOUND ]; then
68
-            echo $line >> $NEW_CONFIG_FILE
68
+            echo "$line" >> $NEW_CONFIG_FILE
69 69
         else
70 70
             if [[ "$line" == '</user>' ]]; then
71 71
                 USER_FOUND=
72 72
             fi
73 73
         fi
74
-    done
74
+    done < $CONFIG_FILE
75 75
     mv $NEW_CONFIG_FILE $CONFIG_FILE
76 76
 }
77 77
 

+ 3
- 3
src/freedombone-sipfreeext Ver arquivo

@@ -34,8 +34,8 @@ CONFIG_FILE=/etc/sipwitch.conf
34 34
 extensions=()
35 35
 
36 36
 # get the used extensions
37
-for line in $(cat $CONFIG_FILE)
38
-do
37
+IFS=''
38
+while read line; do
39 39
     if [[ "$line" == "<extension>"* ]]; then
40 40
         ext=$(echo "$line" | awk -F '>' '{print $2}' | awk -F '<' '{print $1}')
41 41
         extensions+=($ext)
@@ -43,7 +43,7 @@ do
43 43
     if [[ "$line" == '</provision>' ]]; then
44 44
         break
45 45
     fi
46
-done
46
+done < $CONFIG_FILE
47 47
 
48 48
 #echo "used extensions:"
49 49
 #echo $extensions