windows脚本调用时shell脚本失败

时间:2014-11-24 08:28:10

标签: linux windows shell netcat

我在我的Windows机器上使用netcat在执行我的shell脚本的linux机器上调用xinetd。在linux上执行此操作时 - > linux在Windows上执行此操作时效果很好 - > linux它不起作用。我不确定CRLF是否可能成为问题。

myscript.sh

#!/bin/sh
CMD="unknown"
CODE="unknown"

while read line;
do
  CMD=`echo ${line} | cut -d"=" -f1`
  CODE=`echo ${line} | cut -d"=" -f2`

  printf "CMD:#%s#\n" $CMD >> /tmp/xinetd.txt;
  printf "CODE:#%s#\n\n" $CODE >> /tmp/xinetd.txt

  case $CMD in
  key)
     case $CODE in
     100 | 150 | 200 | 250 | 300 | 350 | 400 | 450)
        echo $CMD $CODE
     ;;
     *)
       CMD="unknown"
     ;;
     esac
  ;;
  *)
    CMD="unknown"
  ;;
  esac
  break
done

if [ "${CMD}" == "unknown" ]
then
   echo ERROR=3
   exit 3
fi

Windows - > Linux调用:

echo key=100 | ncat.exe myip myport

Debugging-File: 
----------------------------------------------------
CMD:#key#
CODE:#100#

#ODE:# (I don't know where this is coming from ... is it line 17 echo $CMD $CODE? Buf if yeah, why formatting fails and I'm getting error?)

Return: 
----------------------------------------------------
ERROR=3
close: No error

Linux - > Linux调用:

echo key=100 | nc myip myport

Debugging-File:
----------------------------------------------------
CMD:#key#
CODE:#100#

Return: 
----------------------------------------------------
key=100

任何想法?

编辑1.1:

好吧,这似乎是一个CRLF问题。我得到了tipp,我应该在Notepad ++中创建一个文件UNIX-ANSI(file.txt),其中我使用的是同一个commadn(key = 100),然后执行

type file.txt | ncat.exe myip myport

这很好用。现在我正试图在linux方面解决这个问题......我不是xinetd大师,但我的想法是做一个

tr "\r\n" "\n"

在上面调用我的shell脚本之前。

我的xinetd服务:

service myservice
{
    type            = UNLISTED
    port            = myport
    socket_type     = stream
    protocol        = tcp
    wait            = no
    cps             = 100 3
    instances       = 500
    per_source      = 250
    flags           = NODELAY
    user            = myuser
    server          = myscript.sh
    server_args     = -e | /usr/bin/tr "\r\n" "\n"
    disable         = no
}

与以前相同的实际结果:

CMD:#key#
CODE:#100#

#ODE:#

解决方案:这很奇怪。我在shell脚本中编辑了2行,并添加了2行:

  CMD=`echo $line | tr "\r\n" "\n" | cut -d = -f1`
  CODE=`echo $line | tr "\r\n" "\n" | cut -d = -f2`

  CMD=`echo $CODE | sed "s/ //g"`
  CODE=`echo $CODE | sed "s/ //g"`

我的printf没有向我显示$ CODE上的前导空格字符。这就是问题所在。

0 个答案:

没有答案