LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Shell Script for making directories from a file name (https://www.linuxquestions.org/questions/linux-server-73/shell-script-for-making-directories-from-a-file-name-734454/)

vikki 06-20-2009 05:19 PM

Shell Script for making directories from a file name
 
Hello guys

I have a file with usersnames in the following format
user1
user2
user3

i want to make directories of all using shell script. and then giving them required permissions. my users are on other LDAP server and i have binded ldap using pam mechanism.

please help, i am complete newbie in shell scripting.

Thanks in advance

unSpawn 06-20-2009 06:12 PM

If you're new to scripting you might want some Bash scripting guides like http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html or http://www.tldp.org/LDP/Bash-Beginne...tml/index.html and later on the http://www.tldp.org/LDP/abs/html/.

fritz001 06-20-2009 08:21 PM

for beginner in `cat usernames`; do mkdir $beginner chmod 640 $beginner; done

colucix 06-21-2009 01:53 AM

Quote:

Originally Posted by fritz001 (Post 3580939)
for beginner in `cat usernames`; do mkdir $beginner chmod 640 $beginner; done

This will give spurious directories and a bunch of errors.... :rolleyes:

fritz001 06-21-2009 09:06 AM

for beginner in `cat usermanes`; do mkdir $beginner; chmod 640 $beginner; done

Now it's correct :)

unSpawn 06-21-2009 09:49 AM

Quote:

Originally Posted by fritz001 (Post 3581324)
Now it's correct

No it isn't. It may look syntactically correct but I still wouldn't ever use it like that because of 0) expanding in this "for" loop versus 'while|read' (try expanding a ludicrous amount of items), 1) lack of proper quoting (spaces), 2) duplicated commands (mkdir --mode), 3) the octal mode in the chmod command (this would render any directories created inaccessable) and 4) not rooting it in the directory the directories should be created in. Sure, that was not a requirement and most of this may seem like nitpicking but scripting often isn't only listing commands but also taking into account proper error handling...

vikki 06-24-2009 06:21 AM

I got all my users of ldap from this.. to a file
#getent passwd > ldap

redirected name and gid value of users in another file with ':' as a delimeter
#gawk -F: '{ print $1,":"$4 }' ldap > arpit

now the script....
#!/bin/bash
NEW_USERS="/root/arpit"
cat ${NEW_USERS} | \
while read USER GID
do
mkdir /home/${USER}
chown ${USER}${GID} /home/${USER}
cp -f /etc/skel/.* /home/${USER}/
done


after that i tried to set user quota to my users.. but didnt worked.. can someone help me with it

after setting quota on one user i tried this.. but didnt worked
edquota -p user1 `awk -F: {print $1}' /root/arpit'

please help

colucix 06-24-2009 07:18 AM

Quote:

Originally Posted by vikki (Post 3584497)
edquota -p user1 `awk -F: {print $1}' /root/arpit'

This command has misplaced quotes. It should be:
Code:

edquota -p user1 `awk -F: '{print $1}'`
or using the bash notation for command substitution:
Code:

edquota -p user1 $(awk -F: '{print $1}')


All times are GMT -5. The time now is 02:01 PM.