LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   nc command in script vs. in shell (https://www.linuxquestions.org/questions/linux-newbie-8/nc-command-in-script-vs-in-shell-4175428409/)

noguru 09-21-2012 12:05 PM

nc command in script vs. in shell
 
I am using the following Linux command and have tried it in a script (tried it in bash and csh) and from the bash shell:
nc -z -w 2 <hostid> 22
where host ID has been validated as a valid domain (tried it on my own domain name).

When I do it at the shell prompt, it works perfectly, but when the script issues the identical command, I get this error:
nc: getaddrinfo: Name or service not known

Can someone give me an idea as to what I am doing wrong?

Snark1994 09-21-2012 12:30 PM

What shell are you using, what hashbang are you using (the #! line at the start of the script), and what command are you using to run the script?

noguru 09-21-2012 01:10 PM

nc in script
 
As stated in my post, I have tried it with both bash (#!/bin/bash) and csh(#!/bin/csh) and my shell is a bash shell.
Also, further investigation shows that the cause is related to the value of the domain name.
The domain name comes from this line in my script:
for id in (grep -v '^#' ~/hostnames)

when I do:
echo $id
it looks OK, but it clearly is not working.

jefro 09-21-2012 03:07 PM

I guess you could try full path to nc.

/bin/nc or wherever.

I don't know how domain would fail if it works in shell. A script should run nc.

noguru 09-21-2012 03:51 PM

Problem solved: the grep operation was appending an extra character(0x00) at the end of my domain string. Using substring to remove it removed the cause of the error.

jefro 09-21-2012 05:11 PM

Thanks for the update.

Snark1994 09-22-2012 09:10 AM

Well done for posting the solution - could you mark the thread as 'SOLVED' please?

Thanks,


All times are GMT -5. The time now is 10:39 AM.