shell script help

Reply

Join Date: Dec 2007
Posts: 3
Reputation: jjamd64 is an unknown quantity at this point 
Solved Threads: 0
jjamd64 jjamd64 is offline Offline
Newbie Poster

shell script help

 
0
  #1
Dec 11th, 2007
#I'm quite new to scripting and my boss has asked me to solve a simple problem and sadly, I can't figure out how to do it. Any help is appreciated very much.

#The following is a small shell script and the output that it produces for google.com.

#!/bin/sh
whois $1 | grep "Name Server"
dig +short $1 mx
dig +short mail.$1

[jjamd64@localhost ~]$ ./dn.sh google.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
10 smtp3.google.com.
10 smtp4.google.com.
10 smtp1.google.com.
10 smtp2.google.com.
googlemail.l.google.com.
66.249.83.83
66.249.83.19

#I need to modify this script so that it deletes the spaces in the beginning of the "name server" line, deletes the numbers and space before the mx records and replaces with a + symbol, and places a - symbol before the IP's. Here is what it should look like:

[jjamd64@localhost ~]$ ./dn.sh google.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
+smtp3.google.com.
+smtp4.google.com.
+smtp1.google.com.
+smtp2.google.com.
googlemail.l.google.com.
-66.249.83.83
-66.249.83.19

#I would like to use the sed command if possible and any hints would be much appreciated. I'm open to other suggestions as long as it produces the desired output. I really appreciate any direction and tips. THANKS!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 54
Reputation: radoulov is an unknown quantity at this point 
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: shell script help

 
0
  #2
Dec 12th, 2007
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh
  2. [ $# -ne 1 ]&&{
  3. printf "Usage %s [%s]\n" "$0" "host"
  4. exit 1
  5. }
  6. whois "$1"|fgrep "Name Server"|while read ns;do
  7. printf "%s\n" "${ns# }"
  8. done
  9. dig +short "$1" mx|while read j a;do
  10. printf "+%s\n" "$a"
  11. done
  12. dig +short mail."$1"|while read ms;do
  13. case $ms in
  14. ([0-9]*) printf "%s\n" "-$ms";;
  15. (*) printf "%s\n" "$ms"
  16. esac;
  17. done
  18.  
  19. exit

P.S. Change sh to a newer shell (zsh, bash, ksh93) if possibile
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: jjamd64 is an unknown quantity at this point 
Solved Threads: 0
jjamd64 jjamd64 is offline Offline
Newbie Poster

Re: shell script help

 
0
  #3
Dec 12th, 2007
Thank you Radoulov! Thats exactly what I was hoping for! It works perfectly. I would very much appreciate an explanation of the script you supplied. I understand the basic logic behind the statements but the syntax looks very cryptic to me since I'm a beginner.

This is what I came up with on my own:

#!/bin/sh
whois $1 | grep "Name Server"|sed "s/ //"
dig +short $1 mx|sed "s/[0-9][0-9] /+/"
echo -`dig +short mail.$1`

which doesn't work exactly right, but with your last while statement:

#!/bin/sh
whois $1 | grep "Name Server"|sed "s/ //"
dig +short $1 mx|sed "s/[0-9][0-9] /+/"
dig +short mail."$1"|while read ms;do
case $ms in
([0-9]*) printf "%s\n" "-$ms";;
(*) printf "%s\n" "$ms"
esac;
done


It works like a charm. Thanks again for your help!!!

-JJ
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 54
Reputation: radoulov is an unknown quantity at this point 
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: shell script help

 
0
  #4
Dec 12th, 2007
Originally Posted by jjamd64 View Post
[...]
I would very much appreciate an explanation of the script you supplied.
[...]
Line by line:

Shell Scripting Syntax (Toggle Plain Text)
  1. [ $# -ne 1 ]&&{
  2. printf "Usage %s [%s]\n" "$0" "host"
  3. exit 1
  4. }

Test if the number of arguments supplied ($#) is not 1 (-ne stands for not equal),
if so (&& - if the previous command/block returns true, logical AND):
print the usage syntax and exit with status 1
(in Unix shell return code different than 0 usually means "something went wrong").

Shell Scripting Syntax (Toggle Plain Text)
  1. whois "$1"|fgrep "Name Server"|while read ns;do
  2. printf "%s\n" "${ns# }"
  3. done

I changed your grep with fgrep because it's faster
(you can use it in this case, because you're matching fixed strings,
not regular expressions).
The while loop reads the lines from the pipe,
the parameter expansion strips the leading spaces (*)
and prints them.

(*)

${variable#pattern}
If the pattern matches the beginning of the variable's value, delete the shortest part that
matches and return the rest.
Shell Scripting Syntax (Toggle Plain Text)
  1. dig +short "$1" mx|while read j a;do
  2. printf "+%s\n" "$a"
  3. done

Here the while loop reads the lines where
shell word splitting based on the current IFS
(which defaults to space, tab and new line)
is performed, so j (junk) is the first "word"
(the numbers you want to strip) and a (address)
the second.
Then prints the address with leading + sign.

Shell Scripting Syntax (Toggle Plain Text)
  1. dig +short mail."$1"|while read ms;do
  2. case $ms in
  3. ([0-9]*) printf "%s\n" "-$ms";;
  4. (*) printf "%s\n" "$ms"
  5. esac;
  6. done

Here the case statement tests the value
of the ms variable against basic shell
globbing:
if the value begins with a digit,
print it with a leading - sign,
otherwise: just print it as it is.

HTH
Last edited by radoulov; Dec 12th, 2007 at 10:18 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum


Views: 3163 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC