Improved loops

This commit is contained in:
Bob Mottram 2015-11-02 12:11:49 +00:00
parent d50e8ad1d3
commit 5ca98a2050
3 changed files with 19 additions and 19 deletions

View File

@ -43,13 +43,13 @@ function show_help {
} }
function sip_user_exists { function sip_user_exists {
for line in $(cat $CONFIG_FILE) IFS=''
do while read line; do
if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
USER_EXISTS="yes" USER_EXISTS="yes"
return return
fi fi
done done < $CONFIG_FILE
} }
function update_sip_user { function update_sip_user {
@ -59,8 +59,8 @@ function update_sip_user {
rm -f $NEW_CONFIG_FILE rm -f $NEW_CONFIG_FILE
fi fi
touch $NEW_CONFIG_FILE touch $NEW_CONFIG_FILE
for line in $(cat $CONFIG_FILE) IFS=''
do while read line; do
if [ ! $USER_FOUND ]; then if [ ! $USER_FOUND ]; then
if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
USER_FOUND="yes" USER_FOUND="yes"
@ -78,7 +78,7 @@ function update_sip_user {
fi fi
fi fi
echo $line >> $NEW_CONFIG_FILE echo $line >> $NEW_CONFIG_FILE
done done < $CONFIG_FILE
mv $NEW_CONFIG_FILE $CONFIG_FILE mv $NEW_CONFIG_FILE $CONFIG_FILE
} }
@ -88,8 +88,8 @@ function add_sip_user {
rm -f $NEW_CONFIG_FILE rm -f $NEW_CONFIG_FILE
fi fi
touch $NEW_CONFIG_FILE touch $NEW_CONFIG_FILE
for line in $(cat $CONFIG_FILE) IFS=''
do while read line; do
if [[ "$line" == '</provision>' ]]; then if [[ "$line" == '</provision>' ]]; then
echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
@ -98,7 +98,7 @@ function add_sip_user {
echo '</user>' >> $NEW_CONFIG_FILE echo '</user>' >> $NEW_CONFIG_FILE
fi fi
echo $line >> $NEW_CONFIG_FILE echo $line >> $NEW_CONFIG_FILE
done done < $CONFIG_FILE
mv $NEW_CONFIG_FILE $CONFIG_FILE mv $NEW_CONFIG_FILE $CONFIG_FILE
usermod -aG sipwitch $MY_USERNAME usermod -aG sipwitch $MY_USERNAME
} }

View File

@ -41,13 +41,13 @@ function show_help {
} }
function sip_user_exists { function sip_user_exists {
for line in $(cat $CONFIG_FILE) IFS=''
do while read line; do
if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
USER_EXISTS="yes" USER_EXISTS="yes"
return return
fi fi
done done < $CONFIG_FILE
} }
function remove_sip_user { function remove_sip_user {
@ -57,21 +57,21 @@ function remove_sip_user {
rm -f $NEW_CONFIG_FILE rm -f $NEW_CONFIG_FILE
fi fi
touch $NEW_CONFIG_FILE touch $NEW_CONFIG_FILE
for line in $(cat $CONFIG_FILE) IFS=''
do while read line; do
if [ ! $USER_FOUND ]; then if [ ! $USER_FOUND ]; then
if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
USER_FOUND="yes" USER_FOUND="yes"
fi fi
fi fi
if [ ! $USER_FOUND ]; then if [ ! $USER_FOUND ]; then
echo $line >> $NEW_CONFIG_FILE echo "$line" >> $NEW_CONFIG_FILE
else else
if [[ "$line" == '</user>' ]]; then if [[ "$line" == '</user>' ]]; then
USER_FOUND= USER_FOUND=
fi fi
fi fi
done done < $CONFIG_FILE
mv $NEW_CONFIG_FILE $CONFIG_FILE mv $NEW_CONFIG_FILE $CONFIG_FILE
} }

View File

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