+ Post New Thread
Results 1 to 7 of 7
Coding Thread, terminal script to create home dirs in OS X in Coding and Web Development; Hello, I am perhaps the worlds worst scripter, but to erradicate the fear that is creating a working script i ...
  1. #1

    Join Date
    May 2006
    Location
    West Bromwich
    Posts
    2,190
    Thank Post
    299
    Thanked 215 Times in 185 Posts
    Rep Power
    56

    terminal script to create home dirs in OS X

    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

  2. #2

    Join Date
    Feb 2009
    Posts
    95
    Thank Post
    3
    Thanked 33 Times in 32 Posts
    Rep Power
    17
    You're quite right to think of awk. Pipe the output of the command through awk:

    Code:
     |  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:
    Code:
    bob 123
    alice 456
    joe 873
    Then you'd get an output (after above awk) of:
    Code:
    123
    456
    873
    Is that what you wanted?

  3. Thanks to Chillibear from:

    HodgeHi (5th May 2010)

  4. #3

    Join Date
    May 2006
    Location
    West Bromwich
    Posts
    2,190
    Thank Post
    299
    Thanked 215 Times in 185 Posts
    Rep Power
    56
    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

  5. #4

    Join Date
    Feb 2009
    Posts
    95
    Thank Post
    3
    Thanked 33 Times in 32 Posts
    Rep Power
    17
    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.

  6. Thanks to Chillibear from:

    HodgeHi (5th May 2010)

  7. #5

    Join Date
    May 2006
    Location
    West Bromwich
    Posts
    2,190
    Thank Post
    299
    Thanked 215 Times in 185 Posts
    Rep Power
    56
    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?

  8. #6

    Join Date
    Feb 2009
    Posts
    95
    Thank Post
    3
    Thanked 33 Times in 32 Posts
    Rep Power
    17
    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:

    Code:
    #!/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:
    Code:
    aone, atwo, athree
    bone, btwo, bthree
    cone, ctwo, cthree
    Anyhow, good luck.

  9. Thanks to Chillibear from:

    HodgeHi (3rd July 2009)

  10. #7

    Join Date
    May 2006
    Location
    West Bromwich
    Posts
    2,190
    Thank Post
    299
    Thanked 215 Times in 185 Posts
    Rep Power
    56
    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.

    Code:
    #!/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

SHARE:
+ Post New Thread

Similar Threads

  1. Script to create a VPN connection
    By Norphy in forum Scripts
    Replies: 5
    Last Post: 20th April 2010, 11:37 AM
  2. Script to create a scheduled task
    By FN-GM in forum Scripts
    Replies: 3
    Last Post: 2nd May 2009, 07:17 PM
  3. Moving Home Dirs?
    By mmoseley in forum Wireless Networks
    Replies: 19
    Last Post: 1st October 2007, 11:57 AM
  4. Replies: 11
    Last Post: 6th September 2007, 07:10 PM
  5. VB Script to create folders
    By markesmith in forum Scripts
    Replies: 2
    Last Post: 26th April 2007, 03:03 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •