LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-10-2009, 07:28 AM   #1
Scormen
LQ Newbie
 
Registered: Mar 2009
Posts: 8

Rep: Reputation: 0
Display IP in a shell script


Hi all,

I'm writing a little shell script, but it I'm having a little problem.

The script has to write a other script, this is what I have:
Code:
echo "
 INTIPP=$(which ifconfig) | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print \$1}'
 echo ${INTIPP}
" > /script.sh
That's quite useless, I know, but is only a piece of the script

When I look into /script.sh, I see this:
Code:
 INTIPP=/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
 echo
Thats wrong, behind "echo", there should be my IP address.

When I execute the command $(which ifconfig) | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' in a terminal, it works. But not from the script.

Any idea?

Thanks,
Kris
 
Old 04-10-2009, 07:41 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
INTIPP=`$(which ifconfig) | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print \$1}'`
echo $INTIPP

should do it. Basically, when you use echo, anything in double quotes is taken to be a string, unless there's a $, in which case the variable is substituted with its value. You can't assign to variables inside an echo.

Last edited by Nylex; 04-10-2009 at 07:42 AM.
 
Old 04-10-2009, 07:50 AM   #3
Scormen
LQ Newbie
 
Registered: Mar 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for your answer, Nylex!

But, the output is then:
Code:
 INTIPP=192.168.2.198
 echo
The output on "the first rule" should realy hold the command itself, also in the output, because when using DHCP, each time running /script.sh, the IP will be different.

So, in /script.sh the output should have to be something like this:
Quote:
INTIPP=/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
192.168.2.5
To say it in other words:
The first script have to make a script (/script.sh).
/script.sh has to display the IP address.
Because the IP address is received from a DHCP, it is always different, so it should be echoed by a command.

Thanks,
Kris

Last edited by Scormen; 04-10-2009 at 07:53 AM.
 
Old 04-10-2009, 08:03 AM   #4
Scormen
LQ Newbie
 
Registered: Mar 2009
Posts: 8

Original Poster
Rep: Reputation: 0
I got it

The first script is:
Code:
#!/bin/bash
echo "
#!/bin/bash
 INTIPP=\`$(which ifconfig) | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print \$1}'\`
 echo \${INTIPP}
" > /script.sh
This will make /script.sh like this:
Quote:
#!/bin/bash
INTIPP=`/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
echo ${INTIPP}
When executing /script.sh, it will return the IP address.

Thanks for the help!
Kris
 
  


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
display erro log in linux using shell script venu_babu Programming 1 08-31-2008 03:01 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
How do I display real shell script output to web page. farmerjoe Programming 1 04-07-2005 01:03 PM
How do I make PHP display output from my shell script? farmerjoe Programming 2 04-03-2005 01:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:53 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