The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read the configuration file from shell script nishanth hampal Shell Programming and Scripting 7 02-27-2008 03:42 AM
Read variable from file in a C shell script haouesse Shell Programming and Scripting 2 11-07-2006 09:34 AM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 06:53 AM
Shell Script to read specific lines in a file varshanswamy Shell Programming and Scripting 5 08-22-2005 07:12 PM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 07:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2008
aspect_p aspect_p is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
Shell script to read file into variable

the script i am trying to write will allow my server to give itself an ip address.

So far i am up to the following but i'm stuck.

tracert -m 1 > traceroute.txt

[Outputs]
1 routername (ipaddr) 2.094 ms 1.789 ms 1.243 ms

i want to get ipaddr as a variable and use it to write the ifcfg-eth script.
How should i go about this?
  #2 (permalink)  
Old 03-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
# one way that does not use an intermediate  file
tracert -m 1 | read one two ipaddr dummy
# from the file
awk '{ print $3}' traceroute.txt | read ipaddr
# if you want to lose the ( ) around the ipaddr if you really have one
awk '{ print $3}' traceroute.txt | tr -d '('  | tr -d ')'  | read ipaddr
  #3 (permalink)  
Old 03-19-2008
aspect_p aspect_p is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
i am using this in the script
traceroute -m1 ipaddr |grep 10.12 |awk '{ print $3}'| tr -d '(' | tr -d ')' | read ipaddr

echo $ipaddr

the only problem now is that it wont run as a single command.

ipaddr=$ ( traceroute -m1 10.0.100.1 |grep 10.12 |awk '{ print $3}'| tr -d '(' | tr -d ')' )
tracert.sh: line 3: syntax error near unexpected token `|'
tracert.sh: line 3: `ipaddr=( traceroute -m 1 10.100.0.100 |grep 10.12 |awk '{print $3}'| tr -d '(' | tr -d ')' )'
  #4 (permalink)  
Old 03-19-2008
aspect_p aspect_p is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
so i got the command to work on a single line, syntax error.

What i want to do now is trim the last three digits of the ip..

I have 192.168.1.1 i'm trying to achieve 192.168.1
  #5 (permalink)  
Old 03-19-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Code:
$ echo "192.168.1.1" | sed 's/\([0-9]*\)\(\.[0-9]*\)\(\.[0-9]*\)\(\.[0-9]*\)/\1\2\3/'
192.168.1
$
  #6 (permalink)  
Old 03-19-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Code:
 # echo "192.168.1.1" | awk -F'.' ' {IP=""; for(i=1;i<NF;i++) { IP=IP $i "." } print IP}'
192.168.1.
  #7 (permalink)  
Old 03-19-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by aspect_p View Post
What i want to do now is trim the last three digits of the ip..

I have 192.168.1.1 i'm trying to achieve 192.168.1

Do you want to remove the last two or the last three characters? You say three, but your example only has two removed.
Code:
ipaddr=192.168.1.1
printf "%s\n" "${ipaddr%??}"
printf "%s\n" "${ipaddr%???}"
On the other hand, if you want the last component of the dotted quad address removed:
Code:
ipaddr=192.168.1.1
printf "%s\n" "${ipaddr%.*}"
Closed Thread

Bookmarks

Tags
awk, awk trim, linux, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0