LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 12:39 PM   #1
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Rep: Reputation: Disabled
My script runs twice


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
Attached Thumbnails
Click image for larger version

Name:	linux.jpg
Views:	50
Size:	118.7 KB
ID:	12154  
 
Old 03-28-2013, 12:41 PM   #2
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
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

Last edited by nolan45; 03-28-2013 at 12:48 PM.
 
Old 03-28-2013, 01:28 PM   #3
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
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.

Last edited by shivaa; 03-28-2013 at 01:32 PM.
 
Old 03-28-2013, 01:31 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...0/#post4920529 <-- did you do this as instructed?
 
Old 03-28-2013, 01:47 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
Old 03-28-2013, 01:52 PM   #6
nolan45
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-28-2013, 01:57 PM   #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
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.
 
  


Reply



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
compute cluster runs old script after changing the script Reignfire Linux - Server 1 09-27-2012 05:35 AM
Script runs Manually but not in Cron, yet other parts of the script run mccartjd Linux - Newbie 5 01-08-2012 06:54 PM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:27 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
Open Source Consulting | Domain Registration