bash add entry to fstab by script

Hi all,

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 have not 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

Open in new window

----------------------------------------------------------------------------------------------

Thank you for your help
Georges OrwellSystem AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duncan RoeSoftware DeveloperCommented:
Suggest:
dir_src=/fileserver/share # was: dir_src=//fileserver/share
cat /derv/null > /etc/.cifspasswd # At start of your script, to expunge old contents
printf "put your windows login: " # was: printf "put your windows login: \n"
printf "Now put your windows password: " # was: printf "Now put your windows password: \n"
printf "\n" # after "read password"

Open in new window


If it still doesn't work, post back what info you can
0
Georges OrwellSystem AdministratorAuthor Commented:
Finaly I changed my goal.
it's so difficult to write on fstab with no root account.
I looked for add mount command in .bashrc.

there is my script:

#!/bin/bash
cat /dev/null > $HOME/.cifspasswd
hostname="$(cat /etc/hostname)"
username="$(whoami)"
dir_src="//mydom.local/wonderfile"
dir_tgt="$HOME/securise"
mkdir -p "$dir_tgt"
chown -R "$(whoami)" "$dir_tgt"
chmod 700 "$dir_tgt"
printf "\n"
echo -n "put your windows login and press [ENTER]:"
read login
echo "======> username=$login"
echo -e "username=$login" >> "$HOME"/.cifspasswd""
printf "\n"
echo -n "Now put your windows password and press [ENTER]:"
read -s password
printf "\n"
echo -e "password="$password"" >> "$HOME"/.cifspasswd""
echo "domain=mydom.local" >> "$HOME"/.cifspasswd""
chmod 600 "$HOME"/.cifspasswd""
if grep -i "mount -t cifs" "$HOME"/.bashrc"" ; then
printf "\n"
else
echo -e "mount -t cifs $dir_src $dir_tgt -o credentials=$HOME/.cifspasswd" >> "$HOME"/.bashrc""
fi
. ~/.bashrc
ls -l "$dir_tgt" | wc -l
if [ "$?" -eq 0 ]
then
echo "pb mount" | mail -s  "$hostname $username" " mount error " -a FROM:"$(whoami)@mydom.fr it@mydom.fr"
else
echo " all work fine " | mail -s " mount ok " -a FROM:"$(whoami)@mydom.fr  it@mydom.fr"
fi

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Georges OrwellSystem AdministratorAuthor Commented:
RAS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.