Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bit of shell script loop help please.

  • 21-02-2012 11:41pm
    #1
    Registered Users, Registered Users 2 Posts: 1,775 ✭✭✭


    I'm trying to write a script to reset the ownership on users home directory's

    Problem
    drwx
    12 root rootg 408 Feb 21 17:24 blogsj1

    What I'm trying to achieve
    drwxr-xr-x 12 blogsj1 Subgroup1 408 Feb 21 17:24 blogsj1

    Have a directory structure that looks like this

    Group1
    - SubGroup1
    - - blogsj1
    - - blogsj2
    - SubGroup2
    - - blogsj3
    - - blogsj4
    Group2
    - SubGroup3
    - - blogsj1
    etc.....

    So far I've figured out how to get a list of each users directory

    #!/bin/sh
    for file in ./*/*/*; do
    if [ -d $file ]; then
    echo $file;
    fi
    done

    Now I'm stuck
    How do I use that $file which shows "./Group1/SubGroup1/blogsj1" into separate variables such as

    $groupname
    $subgroupname
    $username

    I can guess the rest (I think)

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 1,775 ✭✭✭Sebzy


    OSI wrote: »
    You could use awk to get the various parts of the string using / as a delimiter

    eg:
    echo "/var/log/httpd" | awk -F/ '{print $2}'
    

    Would print out var

    Thanks but figured out a slightly different method.
    #!/bin/bash
    for file in ./*/*/*; do
       if [ -d $file ]; then
            group=`echo $file | cut -d/ -f2`;
            subgroup=`echo $file | cut -d/ -f3`;
            user=`echo $file | cut -d/ -f4`;
            echo $file;
            echo "Group : $group";
            echo "SubGroup : $subgroup";
            echo "User : $user";
            exit
       fi
    done
    

    On to the next step....


  • Registered Users, Registered Users 2 Posts: 326 ✭✭schrodinger


    Example:
    [schrodinger@purplehaze (13:01) ~/tmp/boards] $ mkdir -p group{1..3}/subgroup{1..3}/blog1
    [schrodinger@purplehaze (13:01) ~/tmp/boards] $ find ./*/*/*/ -type d | awk -F/ '{print $4 ":" $3 " " $0;} | xargs -n 2 chown
    [schrodinger@purplehaze (13:01) ~/tmp/boards] $ 
    


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    for file in `find /path -type d -print`
    do
    echo $file
    done
    

    will list all directories from the point path
    replace path with home or the user's home
    then replace the echo with an appropriate chmod command
    and you should be good.

    Note the ` surround the find are backquotes (left of the 1 on my keyboard), not '


Advertisement