ls和grep命令"未找到" shellcript中的错误

时间:2016-07-13 02:18:27

标签: linux bash macos shell

我想知道为什么"没找到"我在下面写的shellcript中的错误,所以请在这段代码中告诉我我的错误。(环境:mac OS X和CentOS6)

" xaf.sh"

#!/bin/sh
SERVER=$1
USER=$2
PASS=$3
FILE=$4
PATH=$5

echo $1
VAR=`ls | grep ${FILE}`

for one_file in ${VAR}; do
    echo $one_file
    echo "go"
    touch ./ftp_err/log

    ftp -nv 2>./ftp_err.log <<END
        open $SERVER
        user $USER $PASS
        cd /$PATH
        binary
        prompt
        put ${one_file}
    exit
END

    VAR2=`wc ./ftp_err.log`
    if [VAR2 -gt 0 ] ; then
        echo "you have an error in sending ${one_file}"
    else
        echo "you have succeeded Transfer of ${one_file}"
        rm ${one_file}
    fi
    rm ./ftp_err.log
done

...并命令我如何发出,执行结果如下(用户名和密码的一部分被#替换)

**Command**
./xaf.sh 192.168.202.171 #r#####er c#####b## 2 /tmp/

**Result**
./xaf.sh: line 9: ls: command not found
./xaf.sh: line 9: grep: command not found

2 个答案:

答案 0 :(得分:5)

不要这样做:

PATH=$5

$PATH是shell的特殊变量。它定义了执行子命令时要搜索的目录列表。如果覆盖该变量,shell将不再知道在何处查找lsgrep等子命令。

尝试为变量添加其他名称,如下所示:

xpath=$5
...
cd /$xpath

代替。

通常,避免使用大写变量名。 shell为自己的目的使用了许多不同的大写变量名。

答案 1 :(得分:1)

#!/bin/sh
SERVER=$1
USER=$2
PASS=$3
FILE=$4
XPATH=$5
LOG='ftp_err.log'


echo $1
VAR=`ls | grep ${FILE}`

for one_file in ${VAR}; do
    echo $one_file
    echo "go"
    touch ${LOG}

    ftp -n 2>${LOG} <<END
        open $SERVER
        user $USER $PASS
        cd /$XPATH
        binary
        prompt
        put ${one_file}
    exit
END
    VAR2=`grep '' ${LOG}|wc -l`
    if [${VAR2} -gt 0 ] ; then
        echo "you have an error in sending ${one_file}"
    else
        rm ${one_file}
    fi
    rm $LOG

done
大家好 这在Linux到Linux上正常工作。(centos6)