Find the answer to your Linux question:
Results 1 to 4 of 4
I am running this command in my script to display the current logged in user: data8=$(who | grep 'tty' | awk '{print $1,$2,$3,$4}') label8='Current User :' echo "${label8} ${data8}" is ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    26

    how to display text when no output is available

    I am running this command in my script to display the current logged in user:

    data8=$(who | grep 'tty' | awk '{print $1,$2,$3,$4}')
    label8='Current User :'
    echo "${label8} ${data8}"

    is there a way to display "no user" as the output if theres no current user logged in when i run this script?

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Yes indeed.

    Bash includes support for default values for a variable. They follow the format of:
    Code:
    ${parameter:-default_value}
    What this will do is return the value of $parameter if it is not blank, and default_value otherwise. Let's try it out:
    Code:
    tanya:~ alex$ msg=Hello; echo ${msg:-Goodbye}
    Hello
    tanya:~ alex$ msg=; echo ${msg:-Goodbye}
    Goodbye
    Do you see how to use this to solve your problem?

    Also, if you're looking for more cool things to do in Bash, this is the best guide I have ever found:
    http://tldp.org/LDP/abs/html/

    This default value trick is found at:
    http://tldp.org/LDP/abs/html/paramet...stitution.html
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    26
    Quote Originally Posted by Cabhan View Post
    Yes indeed.

    Bash includes support for default values for a variable. They follow the format of:
    Code:
    ${parameter:-default_value}
    What this will do is return the value of $parameter if it is not blank, and default_value otherwise. Let's try it out:
    Code:
    tanya:~ alex$ msg=Hello; echo ${msg:-Goodbye}
    Hello
    tanya:~ alex$ msg=; echo ${msg:-Goodbye}
    Goodbye
    Do you see how to use this to solve your problem?
    I am not sure exactly how to incorporate this into the lines that I already have. Which is this:

    data8=$(who | grep 'tty' | awk '{print $1,$2,$3,$4}')
    label8='Current User :'
    echo "${label8} ${data8}"

    This is my output:

    "Current User : john tty7 2008-09-16 08:25"

    I just need whatever behind "Current User :" to say "no user" if there is no output to be display. Please advice, thanks.

  4. #4
    Just Joined!
    Join Date
    Sep 2008
    Posts
    26
    so this is what i ended up using and it works:

    data8=$(who | grep 'tty' | awk '{print $1,$2,$3,$4}')
    label8='Current User :'
    echo "${label8} ${data8:-no user}"

    output:

    "Current User : no user"

    thanks for your help Cabhan

Posting Permissions

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