LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-21-2012, 12:05 PM   #1
noguru
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Rep: Reputation: Disabled
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?
 
Old 09-21-2012, 12:30 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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?
 
Old 09-21-2012, 01:10 PM   #3
noguru
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-21-2012, 03:07 PM   #4
jefro
Guru
 
Registered: Mar 2008
Posts: 11,731

Rep: Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445
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.
 
Old 09-21-2012, 03:51 PM   #5
noguru
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-21-2012, 05:11 PM   #6
jefro
Guru
 
Registered: Mar 2008
Posts: 11,731

Rep: Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445
Thanks for the update.
 
Old 09-22-2012, 09:10 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

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

Thanks,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 12:46 PM
Command in shell script Grassie Coetzee Linux - Software 1 03-13-2005 11:42 AM


All times are GMT -5. The time now is 02:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration