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:

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

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]

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


echo " all work fine " | mail -s " fstab ok " -a "FROM:$"


Open in new window


Thank you for your help
Georges OrwellSystem AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:
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
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:

cat /dev/null > $HOME/.cifspasswd
hostname="$(cat /etc/hostname)"
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"
echo -e "mount -t cifs $dir_src $dir_tgt -o credentials=$HOME/.cifspasswd" >> "$HOME"/.bashrc""
. ~/.bashrc
ls -l "$dir_tgt" | wc -l
if [ "$?" -eq 0 ]
echo "pb mount" | mail -s  "$hostname $username" " mount error " -a FROM:"$(whoami)"
echo " all work fine " | mail -s " mount ok " -a FROM:"$(whoami)"

Open in new window

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:
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.