Results 1 to 7 of 7

Thread: how to check a directory's permissions in a bash script???

  1. #1
    Join Date
    Sep 2010
    Beans
    20

    how to check a directory's permissions in a bash script???

    i am writing a bash script and want to check the permissions of a certain directory and if it is set to 777 then continue with the rest if it is not set i must

    chmod 777 web

    how will i do this please??
    thank you

  2. #2
    Join Date
    May 2011
    Location
    /World/NZ/Auckland
    Beans
    438
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: how to check a directory's permissions in a bash script???

    I found this script:

    Code:
    #!/bin/ksh
    ls -ld $* | awk 'BEGIN {v["r1"]=400;
    v["w2"]=200; v["x3"]=100;
    v["s3"]=4100; v["S3"]=4000v["r4"]=40;
    v["w5"]=20 ; v["x6"]=10;
    v["s6"]=2010; v["S6"]=2000v["r7"]=4;
    v["w8"]=2  ; v["x9"]=1;
    v["t9"]=1001; v["T9"]=1000}
    
    {val=0 for (i=1;i<=9;i++) val=val+v[substr($0,i+1,1)i] printf "\%4d \%s\n",val,$NF}
    at this website. It looks as though, if you incorporate it into your bash, it will give you the value you want.

    You'll just have to change the bolded bit, so that it assigns the value to a variable to use in your boolean, rather than printing it.
    "Ubuntu speaks of the very essence of being human. It is to say, 'My humanity is caught up, is inextricably bound up, in yours.'" - Rev. Dr. Desmond Tutu.

    Don't forget to mark your thread as [SOLVED] (Here's how)

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: how to check a directory's permissions in a bash script???

    check out stat

    e.g.
    Code:
    stat /some/path
    To pick out the permissions, something like:
    Code:
    perms==$(stat /some/path | sed -n '/^Access: (/{s/Access: (\([0-9]\+\).*$/\1/;p}')
    if [[ $perms =~ 777 ]]; then
        # do stuff here
    fi

  4. #4
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: how to check a directory's permissions in a bash script???

    man test
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  5. #5
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: how to check a directory's permissions in a bash script???

    If you want all files to have the same mode, just run the chmod uncoditionally on all files. If the file already has that mode, nothing happens, if it has a different mode, it gets the new mode.

    Now on a side-note, chmod 777 is never a good solution.

  6. #6

    Re: how to check a directory's permissions in a bash script???

    Quote Originally Posted by geirha View Post
    ...Now on a side-note, chmod 777 is never a good solution.
    Windows assumes the user is an idiot.
    Linux demands proof.

  7. #7
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,796
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: how to check a directory's permissions in a bash script???

    Just simplifying DaithiF's idea:
    Code:
    #!/bin/bash
    ...
    perm=$(stat -c %a "$f")
    
    if [ "$perm" = "777" ]; then
        echo "$f permissions are 777"
        # doing the rest.
    fi
    ...

Bookmarks

Posting Permissions

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