2016-07-13 77 views
-1

我想知道爲什麼在我的shellcript下面寫下「not found」錯誤,所以請告訴我我在代碼中的錯誤(環境:mac OS X和CentOS6)ls和grep命令「找不到」shellcript中的錯誤

「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 
+0

我成功使用LS,grep的代碼,併爲如下 #!/ bin/sh的寫在 回聲$ 1 FILE = $ 1 VAR ='LS |對於$ {VAR}中的one_file,grep $ {FILE}' ;做 echo $ one_file echo「go」 done –

+0

這是什麼目的:''VAR ='ls | grep $ {FILE}'''?改爲使用:'for *中的one_file $ file「*;做...' –

回答

4

別噸做到這一點:

PATH=$5 

$PATH是shell的一個特殊變量。它定義執行子命令時要搜索的目錄列表。如果您覆蓋該變量,則shell不再知道在哪裏查找子命令,如lsgrep

試着給你的變量一些其他的名字,比如:

xpath=$5 
... 
cd /$xpath 

代替。

通常,避免使用大寫變量名稱。爲了自己的目的,shell使用許多不同的大寫變量名稱。

+0

@glennjackman - 謝謝。更好? –

+0

是的,但「xpath」具有特定的含義,因此將其用作變量名有點誤導。 「路徑= $ 5」很好, –

+0

謝謝大家。終於我通過這個錯誤,但我發現了另一個關於FTP連接....我會嘗試修復它,如果我得到它,我會告訴你在這個線程中正確的代碼。 –

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)

+0

此代碼用於ftp並刪除已發送的文件 –