gpt4 book ai didi

linux - "syntax error: unexpected end of file"使用 ssh

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:11 26 4
gpt4 key购买 nike

谁能告诉我这段代码有什么问题?它应该可以帮助我在远程服务器上进行速度测试。尝试使用 PUTTY 通过 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"

有什么想法吗?谢谢!

最佳答案

从那些不属于变量赋值的命令周围删除反引号。

此外,请确保在续行反斜杠后没有制表符、空格或回车符(并且文件中根本没有回车符)。

不是乘以丑陋的“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"

关于linux - "syntax error: unexpected end of file"使用 ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10965416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com