-1

I'm newbies in bash scripting and i try to write script that add entry for cifs windows share to fstab. I writed a litle script but i'm not shure if it will work fine. I haven't linux to test it Can you help me to improve it or fix it? it will run on Debian 7/8 and Ubuntu 12.04 distrib

here is my code:

    #!/bin/bash

    cp -p /etc/fstab /etc/fstab.back-$(date +%F)

    dir_src=//fileserver/share
    dir_tgt=/mount/share
    fsoptions="cifs _netdev,users,rw,auto,users,credentials=/etc/.cifspasswd,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0"

    mkdir -p $dir_tgt
    chown -R `whoami`  $dir_tgt

    echo -e "$dir_src \t\t $dir_tgt \t\t $fsoptions" >> /etc/fstab

    printf "put your windows login: \n"
    read login
    echo -e "username=$login" >> /etc/.cifspasswd
    printf "\n"
    printf "Now put your windows password: \n"
    read password
    echo "password=$password" >> /etc/.cifspasswd
    echo "domain=dom.local" >> /etc/.cifspasswd

    mount -a >> /tmp/mount.log

if [ $? -ne 0]
then 

cat /tmp/mount.log | mail -s " $hostname $username fstab return " -a "FROM:$username@mycompany.com" it@mycompany.com

else

echo " all work fine " | mail -s " fstab ok " -a "FROM:$username@mycompany.com" it@mycompany.com


fi

Thank you for your help

user3178250
  • 1
  • 2
  • 5
  • Getting access a test VM (run one on your own laptop or something even) shouldn't be a too difficult and will help you in the long run.- Your script will only be able to work as intended when it is running as root. check for that first, you're failing to check if the script has already been ran before, running `chown` when the share might already be mounted .... – HBruijn Jul 29 '15 at 22:20

1 Answers1

2

Your script is not idempotent. That is if you run it a second time it will seriously break things.

This type of thing is something that a configuration management tool like puppet/chef/etc would handle better. You perform operations like this through an interface that will guarantee idempotency for you.

Your script has basically no error checking. What happens if the FS is read only, what happens if the FS is full? Did the person calling the script run it as root, or forget and run it is an unprivileged user?

You could put in lots of error checking into your script to achieve this, but at some point you will be just duplicating the logic you get from a good configuration management tool.

If it isn't clear, I strongly think you should be doing this type of task with a configuration management tool.

Zoredache
  • 130,897
  • 41
  • 276
  • 420