LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-22-2007, 11:05 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
Bash development - move chmod options to bash


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 11:06 PM.
 
Old 06-23-2007, 01:41 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Three points.

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
Hope this helps.
 
Old 06-23-2007, 01:47 AM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
[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
 
Old 06-25-2007, 03:53 PM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Actually,I think this is what you want/meant:
Code:
{{u{g{o,},o,},g{o,},o}}{+,-}{{r{w{x,},x,},w{x,},x}}
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
 
Old 06-25-2007, 06:13 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
"Swimming upstream against the tide"---how's that for a redundant metaphor??

You are talking about changing some paradigms that have survived in the Unix world since the 70s. Not how I choose to spend my time.......YMMV.
 
Old 06-25-2007, 06:28 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
No one asked us if this set of aliases is a good idea. I'm sure that someone out there can tell us why it's a very bad one.

It just seemed like an interesting exercise in logic & coding.
 
Old 06-26-2007, 09:21 AM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
No no no. It's a very good idea, if that's what increases the original poster's productivity.

Quoth pixellany:

Quote:
You are talking about changing some paradigms that have survived in the Unix world since the 70s.
Not at all. He's adding new commands which are shortcuts. He types his commands faster than otherwise, and gets more work done.

What paradigms are being changed?
 
Old 06-26-2007, 09:44 AM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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"?
 
Old 06-26-2007, 09:52 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by archtoad6
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..
 
Old 06-26-2007, 01:13 PM   #10
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
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.
 
  


Reply

Tags
bash, chmod, development, options, permissions


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
Bash: how to handle --options? humbletech99 Programming 4 10-07-2011 08:21 AM
how to tell bash to move files to another folder? hq4ever Linux - Newbie 10 12-30-2010 04:15 AM
Move a job from one bash shell to another? marktaff Linux - General 3 01-13-2006 08:22 PM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 08:03 PM
Execution options in bash eremit Programming 6 03-28-2005 11:00 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration