Shell scripts [was Re: Avoiding /etc/passwd and /etc/group scans]

Igor Pechtchanski pechtcha@cs.nyu.edu
Tue Oct 22 21:10:00 GMT 2002


On Tue, 22 Oct 2002, Christopher Faylor wrote:

> On Tue, Oct 22, 2002 at 10:15:31PM -0400, Igor Pechtchanski wrote:
> >On Tue, 22 Oct 2002, Pierre A. Humblet wrote:
> >> I would also distribute a script or a program to chmod all scripts
> >> in a tree. Under user control, not from setup.
> >
> >I have a reasonably customizable script that I use for exactly the
> >opposite -- some files in my tree are created executable, and I use this
> >script to chmod -x all those that aren't (using extensions OR magic).  If
> >people think it would be helpful, I could modify it to do what you wanted
> >and send it to this list.
>
> I'd like to see what you have.  Thanks for offering.
> cgf

Note that the attached script changes the mode of files to non-executable
if they *don't* conform to a particular pattern, i.e., does the opposite
of what's needed here.

The "customization" can be performed by adding extensions to EXEEXT or
patterns to EXEPAT.  The patterns are separated by the ^A character
(0x01).  The script currently requires perl and awk (which can be replaced
by perl).  It's pretty raw, but I haven't had a reason to polish it.
	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Water molecules expand as they grow warmer" (C) Popular Science, Oct'02, p.51
-------------- next part --------------
#!/bin/sh
#
# A script to fix up executable permissions
# Author: Igor Pechtchanski <pechtcha@cs.nyu.edu>
#

PROGNAME="`basename "$0"`"
USAGE="Usage: $PROGNAME [-v|--verbose] [-n|--dry-run] [-b|--batch] [dirs]"
DASH_PRINT=
ECHO=
TEE=cat
BATCH=
dup2() {
#  xargs -0 -n 1 perl -e 'exit unless ($a=shift);print STDERR "$a\n";print "$a\0"'
#  xargs -0 -n 100 perl -e 'foreach(@ARGV){print STDERR "$_\n";print "$_\0"}'
  perl -e '$/="\0";while(<>){chomp();print STDERR "$_\n";print "$_\0"}'
}
while [ -n "$1" ]; do
  case "$1" in
    -h|--help) echo "$USAGE" >&2 ; exit 0 ;;
    -v|--verbose) DASH_PRINT="-print" ; TEE=dup2 ;;
    -n|--dry-run) ECHO="echo" ;;
    -b|--batch) BATCH="true" ;;
    --) shift ; break ;;
    -*) echo "Invalid flag: $1" >&2 ; echo "$USAGE" >&2 ; exit 2 ;;
    *) break ;;
  esac
  shift
done

DIRS="${@:-.}"

#EXEEXT="sh exe bat com dll"
EXEEXT="exe bat com dll"
EXTFILTER="$(echo "$EXEEXT" | perl -pe 's/(\w+)/-name \\*.$1 -o/g')"

#DBGPRG='-exec echo CAUGHT ".(++$i)." {} \\;'
EXEPAT='^#! */^: *Use */eval.*exec'
#PATPRG='-exec perl -ne \"BEGIN{\\\$s=1};\\\$.=1&&/$p/&&exit(\\\$s=0);exit(\\\$s);END{exit(\\\$s)}\" {} \\;';
PATPRG='-exec awk \"BEGIN{S=1}NR=1&&/$p/{S=0;exit(0)}{exit(S)}END{exit(S)}\" {} \\;';
PATFILTER="$(echo "$EXEPAT" | perl -pe 's/\n$//;@p=split(//);foreach $p(@p){$p=~s@(['"'"'"/])@\\\\$1@g;$p="'"$PATPRG $DBGPRG"' -o";};$_=join(" ",@p)')"

eval "set -- $EXTFILTER $PATFILTER"

for DIR in $DIRS; do
  if [ -d "$DIR" -o -h "$DIR" ]; then
    FILTER="-type f"
  elif [ -f "$DIR" ]; then
    FILTER="-maxdepth 1"
  fi
  if [ -z "$BATCH" ]; then
    find "$DIR" $FILTER -perm -0100 \( "$@" \( $DASH_PRINT -exec $ECHO chmod a-x {} \; \) \)
  else
    find "$DIR" $FILTER -perm -0100 \( "$@" -print0 \) | $TEE | xargs -0 -n 1000 $ECHO chmod a-x --
  fi
done


More information about the Cygwin-developers mailing list