LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   My script runs twice (https://www.linuxquestions.org/questions/linux-newbie-8/my-script-runs-twice-4175455933/)

nolan45 03-28-2013 12:39 PM

My script runs twice
 
1 Attachment(s)
When I open the terminal / CLI my script runs twice in a row. The firs time it runs it gives in error message bash ip : command not found . The second time it runs it runs perfectly. Any ideas?

There is a picture attached to this message to show the actual error I am getting

nolan45 03-28-2013 12:41 PM

Here is the script that is running


#!/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 Machine IP
hostname

# Display IP address
ip addr show eth0

# Pause 30 seconds
sleep 30

shivaa 03-28-2013 01:28 PM

Is this your script or part of your .bashrc or .profile?

First, if it's a part of your .bashrc or .profile, then remove #!/bin/sh.

Second, ip command can be run by superuser only, that's why you're getting error of 'command not found' when it's run with normal user. So you have an option to use sudo before it.
Code:

# Display IP address
sudo ip addr show eth0

Third, I'd suggest that you should remove this part from your .bashrc or .profile, because it will need root privilage and you will have to supply password everytime.
Code:

# Display IP address
ip addr show eth0

And if your script is running twice, then possibly you have added this script to some common shell initialization file like /etc/profile etc, therefore it's running for every user i.e. cntuser as well as root.

Habitual 03-28-2013 01:31 PM

http://www.linuxquestions.org/questi...0/#post4920529 <-- did you do this as instructed?

grail 03-28-2013 01:47 PM

Quote:

Second, ip command can be run by superuser only, that's why you're getting error of 'command not found' when it's run with normal user.
Actually this is not correct. It is not in your path hence you cannot run it, however the part that is correct is it would be in the super users path.

The general rule of thumb to make sure a command works is to use the full path and of course verify that that once provided the full path that the required user can execute it,
if not, then the above may again be correct :)

nolan45 03-28-2013 01:52 PM

ok maybe I didnt understand what was being told to me earlier. So instead of having #!/bin/sh i should have ~/.bashrc or ~/.profile? Sorry guys I am trying to figure this all out. Im in the middle of reading a few things also. Thanks for the help

~/.bashrc or ~/.profile

# **********************************************************************
#
# 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 Machine IP
hostname

# Display IP address
ip addr show eth0

# Pause 30 seconds
sleep 30

shivaa 03-28-2013 01:57 PM

I don't know why you couldn't understand, but you just need to add following line at the end of your /home/cntuser/.bashrc or /home/cntuser/.profile file:
Code:

# Say hello to the user
echo Hello $USER

# Display Machine IP
hostname

# Display IP address
/sbin/ip addr show eth0

# Pause 30 seconds
sleep 30

Do not add anything except this lines. Let's know where you've added these lines?

@Grail:
You're right. I missed to mention that ip command can run with full path from normal user.


All times are GMT -5. The time now is 09:02 PM.