I am trying to create a bash script to save myself some manual work making asterisk users. My bash is pretty shaky however and I have gotten as far as I can without the need of pouring over my scripting cookbooks this weekend. However I was wondering if anyone could make any suggestions for improvements in this? Many thanks if you can help and bonus points for insulting my lack of scripting knowhow
The double quote protects the embedded new lines, so you do not want the \n inserted manually. I also removed the -n switch because why remove an automatic new line and then add one in manually. As written, it will create a blank line between sip device entries which will improve readability of the file later.
In the writetovoicemail() function;
I do not see anything immediately except the redirect pathname is on a different line than the echo command, however I do not know if this is the way it was written or a side effect of being posted. Because of the way the shell processes redirect, the “>>” and the pathname need to be on the same line as the command.
I would also suggest removing the -n for the same reason I stated above.
When you do go live with the script, your asterisk line does not look like it would work.
Try…
asterisk -rx "sip reload"
asterisk -rx “voicemail reload”
Finally, you have your help section after you have already written the sip and voicemail lines. This will cause the file to get garbage in them. Place the help before the calls to writetosip and writetovoicemail and then exit the script .
If you’re doing this for a bunch of users on a regular basis, it might be better to look into using a database-backed realtime setup. Set it alll up and when you need to add users, you throw a row or two into a database and bang you’re done. You don’t even have to tell Asterisk to reload.
In your call to writetovoicemail you redirect the output the file, and you also do the same within the function. The redirect on the call to the function is not necessary.
Also I have a feeling you are going to have problems with the reload commands.
You I think you probably will need to quote the command to the Asterisk, though I have never tried it without quotes.