HodgeHi Posted June 26, 2009 Posted June 26, 2009 Hello, I am perhaps the worlds worst scripter, but to erradicate the fear that is creating a working script i have decided to have a crack at something i have been meaning to do for some time. I have an OS X Server that is connected to a AD for user authentication. Their home dirs are stored on our XRAID. During the User creation process i would like to be able to run a script that creates the User home dir, copies a ready-made user template into the users' home dir and then chowns the folder and its contents for that user. To start i have looked at getting a list of the users from the AD and also their corresponding UIDs. I have used the following command to get this data: dscl /Active\ Directory/All\ Domains list /Users UniqueID This gives a two column output; the first column is the usernames and the second column is the UID. Now my question how can i just extract the second column to use its' data. At first i though grep or perhaps an awk command? But how would i tell it to start at the second column? Hope you guys can help. Thanks, Mark
Chillibear Posted June 27, 2009 Posted June 27, 2009 You're quite right to think of awk. Pipe the output of the command through awk: | awk '{ print $NF }' That will print the last item on each line, for a specific one (say the third use $3, second $2, etc). Columns are split on whitespace by default. So if the command output looks like this: bob 123 alice 456 joe 873 Then you'd get an output (after above awk) of: 123 456 873 Is that what you wanted? 1
HodgeHi Posted June 27, 2009 Author Posted June 27, 2009 Yeah. That's what I am after. I found a thread on the apple discussions that went through the process of exporting the output to a file so now I can export the contents, along with a comma into a csv file. Is this the best way to do this? The next problem I have is Reading the csv file back into an array in terminal so I can then process one line at a time. This "should" then allow me to wrap the method into a GUI, but that's much later on
Chillibear Posted June 27, 2009 Posted June 27, 2009 To be honest rather than doing it using shell scripting I'd use a proper scripting language to do the processing (especially if files are involved). Shell scripting especially with feature rich shells like bash is fun, but it's often more work than just using a scripting language. I tend to do most of my scripts in Ruby these days (easy elegant syntax), but Perl or even PHP would be fine for tackling a task such as this. 1
HodgeHi Posted June 28, 2009 Author Posted June 28, 2009 Too bad i don't know any of them that well either. Learning isn't just the hardest part, it's the knowing what to search for to get the job done. How would one run terminal commands such as chown and chmod using php?
Chillibear Posted July 2, 2009 Posted July 2, 2009 For running external commands from PHP see: PHP: Program execution Functions - Manual But for things like chmod/chown you can directly call PHP functions to do that (see PHP: chmod - Manual, etc.) Since you're obviously more comfortable with shell scripting then you could use something like this to parse the comma separated file you've generated: #!/usr/local/bin/bash # Set the dividing character IFS=$',' # read the file, splitting into three variables per row while read col1 col2 col3 do # clean the whitespace off the begining and end of the variables col1=$(echo "${col1}" | sed -e 's/^[ ]*//g;s/[ ]*$//g') col2=$(echo "${col2}" | sed -e 's/^[ ]*//g;s/[ ]*$//g') col3=$(echo "${col3}" | sed -e 's/^[ ]*//g;s/[ ]*$//g') # print them to the screen echo "column 1 = '$col1'" echo "column 2 = '$col2'" echo "column 3 = '$col3'" done < <(cat data.csv) #END Where the data source (data.csv) contains somthing like this: aone, atwo, athree bone, btwo, bthree cone, ctwo, cthree Anyhow, good luck. 1
HodgeHi Posted July 3, 2009 Author Posted July 3, 2009 I'm not really comfortable with any coding language. Even HTML gives me grief Here's what i got though. It maybe could do with a little tidying up but should work for anyone really. #!/bin/bash # userlist.sh #Exporting the Usernames dscl /Active\ Directory/All\ Domains -list /Users UniqueID| awk '{print $1","$2}' > $PWD/Users.txt #Setting UIDs variable as the list of names in the Users.txt UIDs=$PWD/users.txt #Change the HomeDir variable to the location of the Users Home Dirs. HomeDir=/Users/TestHomes/ #Create the User Home Dir location folder sudo mkdir $HomeDir #Creating the name variable with the names from the users.txt file (UIDs variable) for name in $(awk 'BEGIN{FS=","}{print $1}' < "$UIDs" ) #for name in $(awk 'BEGIN{FS=","}{print $1}' < "$UIDs" ) # Field separator = : ^^^^^^ # Print first field ^^^^^^^^ # Get input from password file ^^^^^^^^^^^^^^^^^ #Run through the users.txt creating the home directories for each user do sudo mkdir $HomeDir/$name sudo cp -R /System/Library/User\ Template/English.lproj/ $HomeDir/$name #chown each Home Dir to the users who owns it sudo chown -R $name $HomeDir/$name let "n += 1" done exit 0
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now