#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!

#!/bin/sh
[ $# -ne 1 ]&&{ 
	printf "Usage %s [%s]\n" "$0" "host"
	exit 1
}
whois "$1"|fgrep "Name Server"|while read ns;do
	printf "%s\n" "${ns#   }"
done 
dig +short "$1" mx|while read j a;do
	printf "+%s\n" "$a"
done
dig +short mail."$1"|while read ms;do
	case $ms in
	([0-9]*) printf "%s\n" "-$ms";;
	(*) printf "%s\n" "$ms"
	esac;
done

exit

P.S. Change sh to a newer shell (zsh, bash, ksh93) if possibile :)

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

[...]
I would very much appreciate an explanation of the script you supplied.
[...]

Line by line:

[ $# -ne 1 ]&&{ 
	printf "Usage %s [%s]\n" "$0" "host"
	exit 1
}

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").

whois "$1"|fgrep "Name Server"|while read ns;do
	printf "%s\n" "${ns#   }"
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.

dig +short "$1" mx|while read j a;do
	printf "+%s\n" "$a"
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.

dig +short mail."$1"|while read ms;do
	case $ms in
	([0-9]*) printf "%s\n" "-$ms";;
	(*) printf "%s\n" "$ms"
	esac;
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.