LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2013, 08:19 AM   #1
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Rep: Reputation: Disabled
Say hello in script


I will start this off by saying this will go into my homework. However it is not something that has to be in there just something I wanted to add to the project because I am already done with the assignment itself. More less wanted to experiment with somethings. What I want to do is when the script runs to great the user by there log in name. i.e. if my log in name was Nolan i want it to say hello Nolan

Is this where you would user the who command?? But the only problem with the who command is that there is more information on there then I want to show up. Any help you guys could give me with this would be appreciated I am all of 4 weeks into Linux so everything is a little confusing to me still.

Below is what I have so far

#!/bin/sh

# **********************************************************************
#
# This script will greet the user then display
# the hostname the IP address of this
# computer and then sleep for 30 seconds
# before exitting.
#
# **********************************************************************

# Say hello to the user
echo hello user

# Display IP address
ip addr show eth0

# Display Machine IP
hostname

# Pause 30 seconds
sleep 30
 
Old 03-28-2013, 08:37 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
There are lots of ways to say hello, like you can add following snippet in your script:
Code:
user=$(echo $LOGNAME)
if [[ $user -eq Nolan ]]; then
echo "Hello! Nolan."
else
echo "Sorry you're not Nolan!"
fi
OR,
Code:
user=$(who am i | cut -d" " -f1)
if [[ $user -eq Nolan ]]; then
echo "Hello! Nolan."
else
echo "Sorry you're not Nolan!"
fi
OR,
Code:
if [[ $(whoami) -eq Nolan ]]; then
echo "Hello! Nolan."
else
echo "Sorry you're not Nolan!"
fi

Last edited by shivaa; 03-28-2013 at 08:42 AM. Reason: Little typo
 
1 members found this post helpful.
Old 03-28-2013, 08:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Welcome aboard.

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

The command whoami will give you the username without the extra stuff. But bash, and probably most other modern shells will also have some preset variables for you, such as $USER and $HOSTNAME, that can give you the same info. See the man page for the full list of environment variables.


Finally though, a small warning. When you use "#!/bin/sh" as the shebang, the system will use whatever is defined as its default POSIX interpreter, and the script will be interpreted in regards to to POSIX compliance. If you use features specific to a certain shell in such scripts (such as bash's arrays, which are not defined in POSIX), then there's no guarantee it will work properly.

Note that undefined features like arrays may still work for you if your system's sh interpreter actually is bash, but many linux distros use dash as their default sh interpreter instead, which is a stricter POSIX based shell. dash doesn't support arrays.

So always be sure to use the actual interpreter you're coding for in your shebang. If you're coding for bash, make it "#!/bin/bash". Save "#!/bin/sh" for scripts that need to be made as portable as possible, such as those in used by init.

See here for the main differences between bash and POSIX scripting:
http://mywiki.wooledge.org/Bashism

Last edited by David the H.; 03-28-2013 at 08:49 AM. Reason: some rewording and added link
 
1 members found this post helpful.
Old 03-28-2013, 08:54 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
Just to confuse.... If you want to run this script during the login process you will need to put it into ~/.bashrc or similar config file. In that case shebang (#!<something>) will have no any meaning.
The current username is available in a variable, $LOGNAME or $USER, so probably you only need to write:
echo hello $USER
 
1 members found this post helpful.
Old 03-28-2013, 09:12 AM   #5
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
I currently have the script in /etc/profile.d

From your post it appears thats not correct?
 
Old 03-28-2013, 09:29 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
that kind of script is sourced by a shell, therefore the shebang is not in use at all. Those scripts are executed for all the user logged in. You need to check man bash (look for login session).
I do not really know what do you want to reach, so I can't say it is ok or wrong.
 
Old 03-28-2013, 09:58 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As pan64 pointed out, if you want all this stuff to be printed (i.e. say hello to user etc.) when you login into your session, then simply add following snippet at the end of your ~/.bashrc or ~/.profile file, so when you will login into your session, that is, when you will open a new terminal, it will be invoked.
Code:
~$ vi .bashrc
..........
..........
if [[ $(whoami) -eq Nolan ]]; then
echo "Hello! Nolan."
else
echo "Sorry you're not Nolan!"
fi
There is no need to add #!/bin/bash in your ~/.bashrc or ~/.profile file, because they will be executed automatically by your working shell.
 
1 members found this post helpful.
Old 03-28-2013, 10:01 AM   #8
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
shivaa thanks for pointing that out. Makes sense now. There is a lot to learn in Linux so some of the questions i ask may seem ridiculous to you guys but I am just trying to learn
 
Old 03-28-2013, 10:04 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by nolan45 View Post
shivaa thanks for pointing that out. Makes sense now. There is a lot to learn in Linux so some of the questions i ask may seem ridiculous to you guys but I am just trying to learn
You're most welcome! We're actually here to solve such problems and help others as much as we can. You can keep asking your doubts/problems.

In the meantime, if you need good stuff for study, then check this blog. Hope links will be useful for you.
 
Old 03-28-2013, 03:47 PM   #10
freebsd_Rules_All_OSes
LQ Newbie
 
Registered: Mar 2013
Posts: 19

Rep: Reputation: Disabled
I use festival, a speech synthesis program to greet me by voice and by adding a line to /etc/profile

For example

Code:
echo "Welcome $USER, today is $(date +"%A %B %d %Y %l%M%P")" | festival --tts
So when I login as frank, It speaks this

Welcome frank, today is Thursday March 28 2013 322pm

In case some of you are saying I could had used $(date +"%c") to represent the full date, I knew that already, but it doesn't sound right because the full date is abbreviated i.e Thu Mar whereas my version it says Thursday March and is more understandable.

Code:
echo "Howdy $USER, you're the greatest" | festival --tts
Anyway, you can make it say whatever you want just use your imagination

Last edited by freebsd_Rules_All_OSes; 03-28-2013 at 04:05 PM.
 
Old 03-28-2013, 08:02 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Here's a good Linux tutorial http://rute.2038bug.com/index.html.gz
 
  


Reply


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
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 10:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 02:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 07:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM


All times are GMT -5. The time now is 05:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration