2012-06-09 156 views
0

任何人都可以告訴我這段代碼有什麼問題嗎?它應該是幫助我在遠程服務器上進行速度測試。我在嘗試用油灰通過SSH來執行它時出現以下錯誤: 語法錯誤:文件使用ssh「語法錯誤:文件意外結束」

ssh_server=$1 
    test_file=".scp-test-file" 
    # Optional: user specified test file size in kBs 
    if test -z "$2" 
    then 
     # default size is 10kB ~ 10mB 
     test_size="10000" 
    else 
     test_size=$2 
    fi 
    # generate a 10000kB file of all zeros 
    echo "Generating $test_size kB test file..." 
    `dd if=/dev/zero of=$test_file bs=$(echo "$test_size*1024" | bc) \ 
     count=1 &> /dev/null` 
    # upload test 
    echo "Testing upload to $ssh_server..." 
    up_speed=`scp -v $test_file $ssh_server:$test_file 2>&1 | \ 
     grep "Bytes per second" | \ 
     sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/g"` 
    up_speed=`echo "($up_speed*0.0009765625*100.0+0.5)/1*0.01" | bc` 
    # download test 
    echo "Testing download to $ssh_server..." 
    down_speed=`scp -v $ssh_server:$test_file $test_file 2>&1 | \ 
     grep "Bytes per second" | \ 
     sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/g"` 
    down_speed=`echo "($down_speed*0.0009765625*100.0+0.5)/1*0.01" | bc` 
    # clean up 
    echo "Removing test file on $ssh_server..." 
    `ssh $ssh_server "rm $test_file"` 
    echo "Removing test file locally..." 
    `rm $test_file` 
    # print result 
    echo "" 
    echo "Upload speed: $up_speed kB/s" 
    echo "Download speed: $down_speed kB/s" 

任何想法的意外的結束?謝謝!

+1

我有,你已經遍佈散'標誌不理解他們_DO _... – sarnold

回答

2

從那些不屬於賦值變量的命令中移除反引號。

此外,請確保在續行反斜槓(並且文件中完全沒有回車)之後沒有製表符,空格或回車符。

不要乘以醜陋的「0.0009765625」,除以2^17131072

你爲什麼除以1?只是省略。除以100而不乘以0.01。

儘管變量的內容不太可能包含空格,但您應該習慣於在擴展時引用變量。

#!/bin/bash 
ssh_server=$1 
test_file=".scp-test-file" 
# Optional: user specified test file size in kBs 
if test -z "$2" 
then 
    # default size is 10mB 
    test_size="10000" 
else 
    test_size=$2 
fi 
# generate a file of all zeros 
echo "Generating $test_size kB test file..." 
dd if=/dev/zero of="$test_file" bs=$(echo "$test_size*1024" | bc) \ 
    count=1 &> /dev/null 
# upload test 
echo "Testing upload to $ssh_server..." 
up_speed=$(scp -v "$test_file" "$ssh_server:$test_file" 2>&1 | \ 
    sed -n '/Bytes per second/s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/p') 
up_speed=$(echo "scale = 2; $up_speed/131072 * 100.0" | bc -l) 
# download test 
echo "Testing download to $ssh_server..." 
down_speed=$(scp -v "$ssh_server:$test_file" "$test_file" 2>&1 | \ 
    sed -n '/Bytes per second/s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/p') 
down_speed=$(echo "scale = 2; $down_speed/131072 * 100.0" | bc -l) 
# clean up 
echo "Removing test file on $ssh_server..." 
ssh $ssh_server "rm '$test_file'" 
echo "Removing test file locally..." 
rm "$test_file" 
# print result 
echo 
echo "Upload speed: $up_speed kB/s" 
echo "Download speed: $down_speed kB/s" 
+0

感謝您的幫助,但我做了所有這些事情,仍然得到同樣的錯誤的感覺。你可以發表一個例子嗎? –

+0

@EionSparks:請參閱我編輯的答案。 –

相關問題