ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
I often inadvertantly forget to actually write chmod when issuing ugo-w type options.
That is, I write (incorrectly, but would like to do so validly)
Code:
u-w filename
when i mean
Code:
chmod u-w filename
and the like.
I got to wondering... can't I just have bash natively understand these sorts of file permissions requests? I could write out aliases in a ~/.aliases file for every possible [ugo][-+][rwx] request, but that would take a long time, and there's probably a better way to do it... any ideas? Thanks!
Last edited by jhwilliams; 06-22-2007 at 10:06 PM.
First, ~/.aliases is not a file that bash normally reads. If you wish to adopt that convention, be sure to source that file from ~/.bashrc and either ~/bash_profile or ~/.profile.
Second, there are more possibilities than [ugo][+-][rwx], as the man page will show. But maybe those are all the possibilities you use. Which is perfectly fine.
Third, if those are all the possibilities you use, it would not take a long time to write out all of those aliases. There are only 18 of them. Copy and paste 'em from here if you like:
Code:
alias u+r='chmod u+r'
alias u+w='chmod u+w'
alias u+x='chmod u+x'
alias u-r='chmod u-r'
alias u-w='chmod u-w'
alias u-x='chmod u-x'
alias g+r='chmod g+r'
alias g+w='chmod g+w'
alias g+x='chmod g+x'
alias g-r='chmod g-r'
alias g-w='chmod g-w'
alias g-x='chmod g-x'
alias o+r='chmod o+r'
alias o+w='chmod o+w'
alias o+x='chmod o+x'
alias o-r='chmod o-r'
alias o-w='chmod o-w'
alias o-x='chmod o-x'
In case you're curious, I didn't type them all by hand. I whipped up this shell script to do it for me:
Code:
#!/bin/sh
for xxx in u g o
do
for yyy in + -
do
for zzz in r w x
do
echo alias $xxx$yyy$zzz=\'chmod $xxx$yyy$zzz\'
done
done
done
[ugo]*[+-][rwx]* is what I should have typed. There would then be 6*2*6 = 72 possible ways or writing it. I like the script idea, though. I'll give that a try. As for the .aliases... I adopted this file so that multiple shells could read from one place and the file would have a neutral name. Thanks for the help
This gives, not the 72 possibilities you mentioned, but the 98 possibilities you implied (7*2*7, because 2^3-1=7). With the asterisks, technically "uuuu+xxxx" would satisfy the pattern. While your code would not produce "ugo+rwx".
The script might then look like this:
Code:
for X in {{u{g{o,},o,},g{o,},o}}{+,-}{{r{w{x,},x,},w{x,},x}}
do
echo alias $X=\'chmod $X\'
done
A possibly less messy alternative:
Code:
for X in \
`echo -e {{u,}{g,}{o,}}{+,-}{{r,}{w,}{x,}}"\n" \
| egrep '[ugo]+[+-][rwx]+'`
do
echo alias $X=\'chmod $X\'
done
I await pixellany's response w/ bated breath. (And that's not sarcasm; hyperbole, maybe, but not sarcasm.)
Meanwhile, I have a gut feeling that cluttering the command structure this way may be bad. In any case, why not just alias chmod itself to something shorter like "cm"?
I await pixellany's response w/ bated breath. (And that's not sarcasm; hyperbole, maybe, but not sarcasm.)
Meanwhile, I have a gut feeling that cluttering the command structure this way may be bad. In any case, why not just alias chmod itself to something shorter like "cm"?
Was that "bated" or "baited"----whole different meaning.....
I was reacting to the initial post which said--in part:
Quote:
can't I just have bash natively understand these sorts of file permissions requests?
---and went on to say something about aliases being too much trouble.
I could type "chmod" several hundred times in the time that it has taken to respond to this thread. This said, I must confess that--as an engineer--I very often go to great lengths to develop labor-saving shortcuts--ones whose typical payback time is measured in years..
Yea, I was 5 when Trovalds started Linux; I'd have to say that my allegiance to UNIX paradigms is minimal. It seems to me that file permissions are overlooked by the novice or are a nuissance to set in the daily affairs of more advanced users, and since there isn't any other reasonable interpretation of the options, it would do everyone a favor.
It makes sense too:
rw+ugo filename
"Do the rw operation on filename from here on..."
Probably you're right though, it wouldn't end up saving alot of time. I was in this mostly as a thought experiment.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.