Search code examples
bashmountcifs

A script executed by MOUNT


I write a script in BASH that performs MOUNT for a particular folder The MOUNT command requires a UID & GID I want the script to take from etc / passwd / The details and each will be written to his own variable and then in the MOUNT command I will summon the variables The script:

#Var 
Username='user'
Password='password'
Uid=''
Gid=''

$ mount -t cifs -o username=$Username,password=$Password,uid=$Uid,gid=$Gid,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777 //192.168.1.120/storage /mnt/storage

Solution

  • I hope that I understood correctly that your question is about the variable assignments.
    Here's a solution:

    #!/bin/bash
    
    Username=${1}
    Password=${2}
    Uid=`cat /etc/passwd | grep ${Username} | cut -d: -f3`
    Gid=`cat /etc/passwd | grep ${Username} | cut -d: -f4`
    
    
    mount -t cifs -o username="${Username}",password="${Password}",uid=${Uid},gid=${Gid},rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777 //192.168.1.120/storage /mnt/storage
    

    Note that:

    • Username and password are provided as the first and second parameter to the script.
    • Uid and Gid are read from /etc/passwd
    • I've put double quotes around your password to accomodate for passwords with spaces