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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-08-2009, 01:56 PM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Rep:
|
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.
|
12-08-2009, 03:06 PM
|
#2
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
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.
|
|
|
12-08-2009, 03:20 PM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
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
|
|
|
12-08-2009, 03:51 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Just remove the exit above the fi
|
|
2 members found this post helpful.
|
12-08-2009, 05:18 PM
|
#5
|
Senior Member
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Rep:
|
Quote:
Originally Posted by allenms
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.
|
12-08-2009, 08:14 PM
|
#6
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
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.
|
|
|
12-09-2009, 01:35 PM
|
#7
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
12-09-2009, 04:57 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434
|
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 07:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|