LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Say hello in script (https://www.linuxquestions.org/questions/linux-newbie-8/say-hello-in-script-4175455900/)

nolan45 03-28-2013 07:19 AM

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

shivaa 03-28-2013 07:37 AM

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


David the H. 03-28-2013 07:42 AM

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

pan64 03-28-2013 07:54 AM

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

nolan45 03-28-2013 08:12 AM

I currently have the script in /etc/profile.d

From your post it appears thats not correct?

pan64 03-28-2013 08:29 AM

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.

shivaa 03-28-2013 08:58 AM

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.

nolan45 03-28-2013 09:01 AM

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

shivaa 03-28-2013 09:04 AM

Quote:

Originally Posted by nolan45 (Post 4920531)
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.

freebsd_Rules_All_OSes 03-28-2013 02:47 PM

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 :)

chrism01 03-28-2013 07:02 PM

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


All times are GMT -5. The time now is 08:13 AM.