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 ...
- 09-16-2008 #1Just 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?
- 09-16-2008 #2
Yes indeed.
Bash includes support for default values for a variable. They follow the format of:
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:${parameter:-default_value}
Do you see how to use this to solve your problem?Code:tanya:~ alex$ msg=Hello; echo ${msg:-Goodbye} Hello tanya:~ alex$ msg=; echo ${msg:-Goodbye} Goodbye
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.htmlDISTRO=Arch
Registered Linux User #388732
- 09-16-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
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.
- 09-16-2008 #4Just 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


Reply With Quote
