Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Alternative commands wtf?

  • 23-03-2009 3:01pm
    #1
    Closed Accounts Posts: 2,349 ✭✭✭


    I have a UNIX exam tomorrow. There's questions on past papers that go like this:

    Give TWO alternative command line instructions to implement the following:
    a) chmod u=rwx,g=rx,o-rwx <filename>
    b) cd $HOME
    c) cat readme.txt | grep unix | wc -l
    d) mkdir "A directory name with spaces!"


    To me these are fairly basic commands and I have no idea how I could implement them any other way... any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 279 ✭✭velocirafter


    is (b) cd ~

    I'm working on a windows machine at the moment so I'm not sure about that


  • Closed Accounts Posts: 155 ✭✭cooperla


    For c) I'd say you could just grep the file directly instead of cat'ing it first.


  • Closed Accounts Posts: 155 ✭✭cooperla


    ... And for a) you could specify the numbers instead of rwx (i.e. chmod 757 filename).


  • Registered Users, Registered Users 2 Posts: 500 ✭✭✭warrenaldo


    a) chmod 757 <filename>


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    a) chmod 750 <filename>
    b) cd ~
    c) grep -o <string> <filename> | wc -l
    d) mkdir a\ directory\ name\ with\ spaces/


  • Advertisement
  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    warrenaldo wrote: »
    a) chmod 757 <filename>

    It's 750 (+rwx,+rx,-rwx) ... unless the - is a typo of course. Or I could be confusing it with VMS :o


  • Registered Users, Registered Users 2 Posts: 3,568 ✭✭✭ethernet


    grasshopa wrote: »
    I have a UNIX exam tomorrow. There's questions on past papers that go like this:

    Give TWO alternative command line instructions to implement the following:
    a) chmod u=rwx,g=rx,o-rwx <filename>
    b) cd $HOME
    c) cat readme.txt | grep unix | wc -l
    d) mkdir "A directory name with spaces!"


    To me these are fairly basic commands and I have no idea how I could implement them any other way... any ideas?
    1. chmod 757 filename
    2. cd ~ OR cd /home/me
    3. grep unix < readme.txt | wc -l
    4. mkdir new\ dir

    Even thought I think you're sitting a few seats away from me in this lab I was too lazy to get up! :D


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    Macros42 wrote: »
    It's 750 (+rwx,+rx,-rwx) ... unless the - is a typo of course. Or I could be confusing it with VMS :o

    I just checked this to be sure - it is 750. Be careful with this in an exam - the notation can use +, - and =. They probably put this in to catch you out. Besides all that it wouldn't make sense to give other more access than group anyway.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Nice one, thanks.

    I do need two ways though.

    For (a) I'd be chancing my hole to change the "u=" to "u+" yes?
    For (b) there's cd ~ OR cd /home/username
    For (c) I have: sed -n "/unix/p" < readme.txt | wc -l
    For (d) I haven't a clue.

    a) chmod u=rwx,g=rx,o-rwx <filename>
    b) cd $HOME
    c) cat readme.txt | grep unix | wc -l
    d) mkdir "A directory name with spaces!"


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    I gave you others for c & d:

    c) grep -o <string> <filename> | wc -l
    and ethernet's "grep unix < readme.txt | wc -l" will do it too.
    d) mkdir a\ directory\ name\ with\ spaces/

    Can't think of a second one for d though.
    In a I don't think you'd get away with + instead of =. If, for example, group already had 'w' you'd be adding rx to give rwx not rx as requested.


  • Advertisement
  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    Bit messy but here's a valid alternative for a)

    chmod ug=rx,u+w,o-rwx filename

    so along with:
    chmod 750 <filename>

    there's your two options.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Macros42 wrote: »
    Bit messy but here's a valid alternative for a)

    chmod ug=rx,u+w,o-rwx filename

    so along with:
    chmod 750 <filename>

    there's your two options.

    Thanks, I didn't put the full list in that last post, only the second alternatives I had.

    I was thinking about (d):
    MYDIR="blah blah"; mkdir $MYDIR

    I might get away with that. however it doesn't work (I tried escaping the quotes)...


  • Registered Users, Registered Users 2 Posts: 3,568 ✭✭✭ethernet


    Forgot to mention a plain old 'cd' without any params will always bring you to your home directory.

    Stumped to come up with something noticeably different for d.

    Best of luck :)


  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    Keep it nice and simple

    b) cd $(getent passwd $(id -un) | cut -d: -f6)

    d) mkdir 'A directory name with spaces!' or

    Infact mkdir "A directory name with spaces!" doesnt actually work in bash.


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    djmarkus wrote: »
    Infact mkdir "A directory name with spaces!" doesnt actually work in bash.

    Yes it does. Just tested it. At least it does in Ubuntu.


  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    dmarkey@dmarkey-XPS:~> mkdir "A directory name with spaces!"
    bash: !": event not found

    Dunno what kind of shell ubuntu is using these days.


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    steve@Sethanon:~$ bash
    steve@Sethanon:~$ mkdir "test dir"
    steve@Sethanon:~$ cd test\ dir/
    steve@Sethanon:~/test dir$ 
    

    Just a thought - Unix tests don't rely on bash anyway. They are based on sh usually. That's installed on every flavour of *nix whereas bash isn't. And just tested mkdir "test dir" in sh too - it works.
    steve@Sethanon:~$ sh
    $ mkdir "test dir"
    $ cd test\ dir
    $ pwd
    /home/steve/test dir
    


  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    try the command that was quoted at the start, namely
    mkdir "A directory name with spaces!"

    This however.
    mkdir "A directory name with spaces\!"
    will work


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    steve@Sethanon:~$ mkdir "A directory name with spaces!"
    mkdir "A directory name with spaces"
    steve@Sethanon:~$ cd A\ directory\ name\ with\ spaces/
    steve@Sethanon:~/A directory name with spaces$ 
    

    What distro are you using?

    [edit]Ah - the ! is throwing it - it's echoing the result. Try mkdir "A directory name with spaces" in your distro (no !)


  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    OpenSuSE

    Tested on Solaris also. Ubuntu must have done something strange with bash.

    Edit:

    I see whats going on, you're using dash on ubuntu these days.


  • Advertisement
  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    Just tested it on my digiweb shell account too - not Ubuntu.

    $ cat /etc/issue
    CentOS release 4.4 (Final)
    [username@web2 account.net]$ mkdir "test dir"
    [username@web2 account.net]$ cd test\ dir/
    [username@web2 test dir]$
    


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    djmarkus wrote: »
    I see whats going on, you're using dash on ubuntu these days.

    Look at my post - I specifically entered bash to test.

    steve@Sethanon:~$ bash
    steve@Sethanon:~$ mkdir "test dir"
    steve@Sethanon:~$ cd test\ dir/
    steve@Sethanon:~/test dir$

    [edit]and bash is my default shell on Ubuntu:
    steve@Sethanon:~$ echo $SHELL
    /bin/bash


    [edit2]Bit puzzling why it won't work for you - if it's accepted in sh then it should be accepted in all shells. Maybe Suse is doing something odd not Ubuntu or CentOS. Too late for me to think tho - off to bed. Google time tomorrow methinks :)


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    grasshopa wrote: »
    I was thinking about (d):
    MYDIR="blah blah"; mkdir $MYDIR

    I might get away with that. however it doesn't work (I tried escaping the quotes)...

    The reason this won't work is that when $MYDIR is evaluated it contains individual words separated by spaces, each of which is seen as a separate parameter.

    In fact, as has been pointed out by others, the presence of a bang in the interpolated(double) quotes means that the command won't run in bash as written.

    The following would have the same effect as the provided command ;)
    echo 'bash: !": event not found' && false
    


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    single quotes for the mkdir remove interpolation.

    e.g.

    mkdir 'my dir!' will work fine

    edit: I'm not being a smart arse. It's an important difference...

    i=test
    mkdir '$i'

    actually makes a directory called $i (i.e. not test).


  • Registered Users, Registered Users 2 Posts: 304 ✭✭PhantomBeaker


    Seeing as all require two different implemenations, here's a smartass one for c):
    awk ' BEGIN { count=0 } /unix/ { count++ } END { print count }' filename
    

    However, I realise this is a bit late, and hope you did well in your exam.

    Aoife


Advertisement