Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Help with bash variables


    Hi.

    I'm trying to make a simple script. This is what I've got now:

    Code:
    if [ "$PAM_USER" != "root" -a "$PAM_SERVICE" != "su" -a "$PAM_TYPE" == "open_session" ];
    However, this only runs the script if $PAM_USER is NOT root or $PAM_SERVICE is NOT su. What I'd like to do is run the script when those two variables are not met at the same time.

    For instance, if $PAM_USER is "user1" and $PAM_SERVICE is "su", I'd like the script to run. Or otherwise, if $PAM_USER is "root" and $PAM_SERVICE is "sshd" aswell.

    Any ideas? Thanks!

  2. #2
    Something like this should do:

    Code:
    if [ \( "$PAM_USER" == "root" -o "$PAM_SERVICE" == "su" \) -a ! \( "$PAM_USER" == "root" -a "$PAM_SERVICE" == "su" \) ];
    (Ideally you'd use some sort of exclusive-or, but bash doesn't seem to have it.)
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  3. #3
    I assume it's not as easy as:

    Code:
    if [ "$PAM_USER" != "root" -o "$PAM_SERVICE" != "su" -a "$PAM_TYPE" == "open_session" ];
    Right?

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by Pyrobisqit View Post
    I assume it's not as easy as:

    Code:
    if [ "$PAM_USER" != "root" -o "$PAM_SERVICE" != "su" -a "$PAM_TYPE" == "open_session" ];
    Right?
    That doesn't do what you described - but then I note that you didn't describe anything in your first post about PAM_TYPE, so I don't know what you want to do with that.

    Also, I (deliberately) don't know about bash's operator precedence in this case - IMHO you should use brackets to clarify, both to yourself and to bash.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  6. #5
    Quote Originally Posted by JohnGraham View Post
    That doesn't do what you described - but then I note that you didn't describe anything in your first post about PAM_TYPE, so I don't know what you want to do with that.

    Also, I (deliberately) don't know about bash's operator precedence in this case - IMHO you should use brackets to clarify, both to yourself and to bash.
    PAM_TYPE should make the script continue if it equals to "open_session" and end the script in case the value returned by $PAM_TYPE is not "open_session".

    I thought maybe I could make a first if with PAM_TYPE and then, insert a second if inside that runs the script whenever any of those two variables are met, but not when both are met?

    Sorry, I don't know very much about bash programming... In fact nothing at all, so your help is greatly appreciated ^^

  7. #6
    Quote Originally Posted by Pyrobisqit View Post
    PAM_TYPE should make the script continue if it equals to "open_session" and end the script in case the value returned by $PAM_TYPE is not "open_session".

    I thought maybe I could make a first if with PAM_TYPE and then, insert a second if inside that runs the script whenever any of those two variables are met, but not when both are met?
    That's probably what I'd do. This should work (but is untested):

    Code:
    if [ "$PAM_TYPE" == "open_session" ] ; then
        if [ \( "$PAM_USER" == "root" -o "$PAM_SERVICE" == "su" \) -a ! \( "$PAM_USER" == "root" -a "$PAM_SERVICE" == "su" \) ]; if
            # Your code here.
        fi
    fi
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •