Skip to main content

Thread: How to parse a file mode from a shell script


problem: bash file mode (read/write/execute) tests not consistent actual mode settings, when 1 using sudo. example, file may read-only owner , owner root, effective file permissions when using sudo wide open, because root can whatever. led problem me in 1 case edited /etc/sudoers changing mode , stuck. want detect other situations similar - file read-only owner, root, indicate intentions of os.

not having found shell script on web parse mode characters output ls -l decided write one. posting here, available others , can indexed. implemented main code function runs in bourne shell , bash. here code:

code:
parse_file_mode () {     indchars=`ls -l $1|sed -e 's/ .*$//' -e 's/\([-rwxse]\)/\1 /g'`     saveifs=$ifs     ifs=" "     cnt=1     ch in $indchars           case $cnt in         1) outstr="ftype=$ch" ;;         2) outstr="$outstr;fownerread=$ch" ;;         3) outstr="$outstr;fownerwrit=$ch" ;;         4) outstr="$outstr;fownerexec=$ch" ;;         5) outstr="$outstr;fgroupread=$ch" ;;         6) outstr="$outstr;fgroupwrit=$ch" ;;         7) outstr="$outstr;fgroupexec=$ch" ;;         8) outstr="$outstr;fotherread=$ch" ;;         9) outstr="$outstr;fotherwrit=$ch" ;;         10) outstr="$outstr;fotherexec=$ch" ;;       esac       cnt=`expr $cnt + 1`     done     ifs=$saveifs     echo $outstr  }
this return string can used set series of shell variables indicate mode characters. possible use function eval, this:

code:
eval `parse_file_mode /etc/sudoers`
which set variables @ current shell. used in shell script , variables defined in current scope.

marked solved start, comments , improvements welcome. if has better place (forum, repository) post this, feel free share it.



Forum The Ubuntu Forum Community Ubuntu Official Flavours Support General Help [SOLVED] How to parse a file mode from a shell script


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support