LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-08-2009, 01:56 PM   #1
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-08-2009, 03:06 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
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.

Last edited by rweaver; 12-08-2009 at 03:09 PM.
 
Old 12-08-2009, 03:20 PM   #3
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 12-08-2009, 03:51 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Just remove the exit above the fi
 
2 members found this post helpful.
Old 12-08-2009, 05:18 PM   #5
raju.mopidevi
Senior Member
 
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Blog Entries: 12

Rep: Reputation: 92
Quote:
Originally Posted by allenms View Post
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
 
2 members found this post helpful.
Old 12-08-2009, 08:14 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 12-09-2009, 01:35 PM   #7
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-09-2009, 04:57 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
If you add

set -xv

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strange (to me) behavior of file permissions when copying file warrob Linux - General 10 10-25-2009 05:13 AM
File permissions v. directory permissions Completely Clueless Linux - Newbie 7 07-09-2009 08:33 AM
how to hide a file using file permissions in linux without using dot davender84 Linux - General 4 03-26-2009 12:13 AM
file permissions OK, but command permissions? stabu Linux - General 2 10-05-2005 12:00 PM
locking a usage policy file/ftp file permissions gbow Linux - Newbie 0 02-16-2004 05:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration