Adventures in Bash and Raspberry PI: A Media/NAS Server Install Script

I’ve been running the Raspberry PI for about two weeks now and I’ve got to say, I am loving every minute of it. I am thinking of making a php extension in C to use the GPIO pins, but have been too busy with the Christmas season and New Year; and after the holidays, I’m having lots of deadlines at work, so, maybe that will come later (ah, lazy me).

xbmc

After choosing which of my two toys (tomato router and raspberry pi) will act as my main Media/NAS box, I’ve decided for the latter (mainly because the Raspberry PI is more powerful than a router to serve media – I am adding an arsenal slowly but surely as a media streaming solution for my home, namely an android TV box, and maybe an additional PI for some other fun uses).

Now for the Raspberry PI configuration. I first manually installed the system to see if the system works, mainly:

1. Install ntfs-3g (for my external drive), and samba related packages for shareable storage seen by my Windows machines (samba, samba-common and samba-bin)
2. Use blkid to find the UUID for my drive, then added the drive in /etc/fstab, and the mount point in the /mnt folder
3. Mount the drive using mount -a
4. Add a user and attach an smbuser and password to that user.
5. Edit the smb.conf to use user shares
6. Restart the samba server
7. Install the minidlna server, and edit the folders where the media files are.

Those things to do manually fine for me, but, I would have liked to make it a bit automated. So, the lazy person that I am, I made a bash script.

Things to learn from this bash script:

There are some snippets which can be useful for other uses:

1. Trimming a string. See the code snippet below:

trim() {
local str="$*"
str="${str#"${str%%[![:space:]]*}"}" # leading whitespaces
str="${str%"${str##*[![:space:]]}"}" # trailing whitespaces
echo -n "$str"
}

This bash function removes the leading and trailing spaces of a string presented in bash. The $* is a bash parameter variable which returns the whole parameters passed through the function (parametrized variables are so named as $1, $2, etc).

Trailing and leading whitespaces are removed using pattern matching and delete.

2. Getting a field value. Use the cut command to get a specific value with a specified delimiter. The example below gets the device UUID of a block device.

uuid=`blkid|grep "$drive"|cut -d " " -f2|cut -d "=" -f2|cut -d \" -f2`

3. Deleting a line containing a specified string from a file. Use grep -v to remove the line.
4. Remove a block of code starting and ending with specified string. Use sed with /d command and start and end text patterns to do this, as shown below:

sed '/#####startsection#####/,/#####endsection#####/ s/.*//' /etc/samba/smb.conf|sed '/^$/d'

5. Adding multiple lines of code to a file. Use the bash Here Strings and IO redirection.

cat >> /etc/samba/smb.conf <<EOF

Download the whole script here.

 
 

Leave a Reply

Your email address will not be published. Required fields are marked *