LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File permissions (https://www.linuxquestions.org/questions/linux-newbie-8/file-permissions-774289/)

allenms 12-08-2009 01:56 PM

File permissions
 
Trying to write a short script that will tell me whether the permissions for 2 files, whose names are given as arguments to the script, are identical. If the permissions are the same, output the common permission field. If they are different, I need to output each file name followed by its permission field. I've been trying to use the cut command but seem to be missing something. Any suggestions would be appreciated. Thanks,
Al

rweaver 12-08-2009 03:06 PM

This is kinda hacky but it's short and simple to understand and doesn't use many tools.

Code:

#!/bin/bash
FILEP1=`ls -l $1 | cut -f1 -d' '`
FILEP2=`ls -l $2 | cut -f1 -d' '`
FILEN1=`ls $1`
FILEN2=`ls $2`
DIFF=0
for i in `seq 1 10`; do
  VAL1=`echo $FILEP1 | cut -c$i`
  VAL2=`echo $FILEP2 | cut -c$i`
  if [ $VAL1 == $VAL2 ]; then
    echo "Bit $i is the same ($VAL1)";
  else
    echo "Bit $i is different ($VAL1 vs $VAL2)";
    DIFF=1
  fi
done
if [ $DIFF -gt 0 ]; then
  echo "$FILEP1 $FILEN1"
  echo "$FILEP2 $FILEN2"
fi

Needs error checking and probably won't function with special characters as written.

allenms 12-08-2009 03:20 PM

Thanks for the info but not quite what I was looking for. Here is what I've been working on:

#!/bin/bash
# Compare file permissions
#=========================

FILENAME=${1}
FILENAME1=${2}

if [ "${FILENAME}" = "" ] || [ "${FILENAME1}" = "" ]
then
echo "Please specify first file and press [Enter]:"
read ${1} >> file3
echo "Please specify second file and press [Enter]:"
read ${2} >> file3
exit
fi

MODE=$(stat --format=%a ${FILENAME})
MODE1=$(stat --format=%a ${FILENAME1})

#Uncomment and comment above for drwxr-xr-x format
#MODE=$(stat --format=%A ${FILENAME})
#MODE1=$(stat --format=%A ${FILENAME1})

if [ "${MODE}" = "${MODE1}" ]
then
echo "Both files have permissions of ${MODE}"
else
echo "${FILENAME} Permissions are ${MODE} : ${FILENAME1} Permissions
are
${MODE1}"
fi



I can get the user to input the files they wish to compare but it dies after that.
AL

Tinkster 12-08-2009 03:51 PM

Just remove the exit above the fi

raju.mopidevi 12-08-2009 05:18 PM

Quote:

Originally Posted by allenms (Post 3784486)
Thanks for the info but not quite what I was looking for. Here is what I've been working on:

#!/bin/bash
# Compare file permissions
#=========================

FILENAME=${1}
FILENAME1=${2}

if [ "${FILENAME}" = "" ] || [ "${FILENAME1}" = "" ]
then
echo "Please specify first file and press [Enter]:"
read ${1} >> file3
echo "Please specify second file and press [Enter]:"
read ${2} >> file3
exit
fi

It will quit because you are exiting.........

change that

evo2 12-08-2009 08:14 PM

Hi,

here it is.
Code:

#!/bin/bash
s1=$(stat --printf=%a $1)
s2=$(stat --printf=%a $2)
if [ "$s1" = "$s2" ] ; then
  echo "$s1"
else
  echo "$1 $s1"
  echo "$2 $s2"
fi

As far as I can tell this should do exactly as you asked for.

allenms 12-09-2009 01:35 PM

Thanks for the help folks. Even though I was "looking" at it, I missed the "exit". Must of had a senior moment that lasted longer than normal. Works as advertised.

chrism01 12-09-2009 04:57 PM

If you add

set -xv

as the 2nd line of the script, you would have been able to watch it run each cmd.


All times are GMT -5. The time now is 11:56 PM.