Tuesday, July 21, 2009

A shell script to do a bunch of things

This is a shell script I wrote for Krim to do following things:
1. Add a mount point into /etc/fatab.
2. create user and mount the folder.


#!/bin/bash

# add lab mount point into /etc/fstab. then create user, mount the folder
# Usage: addlab <vlab1|vlab2> <user_number>
# eg: addlab vlab1 8978
if [ $# -eq 0 ]
then
echo "Usage: $0 <vlab1|vlab2> <user_number>"
exit -1
fi

echo "You are about to create a lab for user lifelabs_$2 [y/n]:"; read x

if [ $x != 'y' ]
then
echo "bye"
exit -1
fi

vlab1="//to-vlab1/LIFELABS_$2 /home/lifelabs_$2/data cifs rw,mand,file_mode=0666,gid=lifelabs_$2,uid=lifelabs_$2,nosetuids,user=TO-VLAB1/lifelabs,password=velvet 0 0"

vlab2="//to-vlab2/LIFELABS_$2 /home/lifelabs_$2/data cifs rw,mand,file_mode=0666,gid=lifelabs_$2,uid=lifelabs_$2,nosetuids,user=TO-VLAB2/lifelabs,password=velvet 0 0"


#search if this lab already in fstab
if grep -q LIFELABS_$2 /etc/fstab
then
echo "LIFELABS_$2 IS ALREADY EXIST IN FSTAB FILE !!! EXIT"
exit -1
fi

if [ "vlab1" == $1 ]
then
echo $vlab1
echo "$vlab1" >> /etc/fstab
elif [ $1 == "vlab2" ]
then
echo $vlab2
echo "$vlab2" >> /etc/fstab
else
echo "no such lab - $1"
exit -1
fi

#add a user and setup password
adduser lifelabs_$2
echo lifelabs_$2:LIFELABS$2 | chpasswd

su - lifelabs_$2 -c "mkdir data"

mount /home/lifelabs_$2/data

exit 0

No comments:

Google+