LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding to DAEMONS/MODULES arrays from bash script (http://www.linuxquestions.org/questions/linux-newbie-8/adding-to-daemons-modules-arrays-from-bash-script-925411/)

crabchucker 01-24-2012 03:57 AM

Adding to DAEMONS/MODULES arrays from bash script
 
I have started playing around with Linux recently (mostly Arch atm) and i am trying to create a Bash script to install the main packages i will be using and to create a user, and i want to be able to add items to the modules and daemons arrays in rc.conf as in changing:

Code:

MODULES=()
to
Code:

MODULES=(vboxguest vboxsf vboxvideo)
and
Code:

DAEMONS=(hwclock syslog-ng network netfs crond)
to
Code:

DAEMONS=(hwclock syslog-ng network netfs crond sshd vbox-service dbus rpcbind)
What would be the easiest way to do this without destroying my rc.conf file(s)

Also how would i uncomment the Australia mirrors in /etc/pacman.d/mirrorlist from the script aswell

catkin 01-24-2012 04:15 AM

sed with the -i option

crabchucker 01-24-2012 05:41 AM

Thanks for the sed command, i done a bit of searching and came across this website www.brunolinux.com/02-The_Terminal/Find_and Replace_with_Sed.html and also http://www.grymoire.com/Unix/Sed.html#uh-2 which helped me understand how to use it and it seems to be working
Code:

sed -i 's/#Server = ftp:\/\/mirror.aarnet.edu.au\/pub\/archlinux\/$repo\/os\/$arch/Server = ftp:\/\/mirror.aarnet.edu.au\/pub\/archlinux\/$repo\/os\/$arch/g' /etc/pacman.d/mirrorlist
plus about 5 more + the modules and daemons arrays, so thanks for the help ^_^
I had to place the backslashes in the url's so it registered the forward slashes in the code instead of other options

David the H. 01-24-2012 09:09 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Perhaps a better way may be to do something like this. Modify the lines in the above script to:

Code:

MODULES=( $(</path/to/modulefile ) )
DAEMONS=( hwclock syslog-ng network netfs crond $(</path/to/daemonfile) )"

I'm assuming that this is a bash or ksh script here, as I know of no other shells that use that syntax.

Create those files and put the entries you want inside them, whitespace-delimited. The script should then import them into the arrays. Any other script you write then also only has to append values to those files to make them available.

BTW, have you checked to see whether the script doesn't already support something similar?

Edit: BTW2, your sed command can be made more readable:

Code:

sed -i 's|#Server = ftp://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch|Server = ftp://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch|g' /etc/pacman.d/mirrorlist
sed can use any ascii character except newline and null as a substitution delimiter, not just "/".

I'm assuming that the $variables in the urls are meant to be kept literal here? The single quotes around the expression will keep them from expanding. But also, regex expressions match substrings, so you don't really need the whole url, just enough of it to be sure you're getting only the lines you want. You may want to anchor the match to the start of the line as well.

Code:

sed -i 's|^#Server = ftp://mirror.aarnet.edu.au|Server = ftp://mirror.aarnet.edu.au|g' /etc/pacman.d/mirrorlist

crabchucker 01-24-2012 03:41 PM

Thanks for the advice i will try that out later on, i was just following what i found and was not sure if shortening it would work or not or if i didn't write the whole line, so i will see if that works


All times are GMT -5. The time now is 06:30 AM.