0
User=0
av="653"
ma="727"
pr="713"
mi="234"
n=192.168.1.20:/root/Ma
echo "Please select av ma ji im pr"
echo -n "First 2 initial of your Name eg: [av ma ji im pr]? "
read User
if [ "$User" = av ]
then
    echo `scp $User@$n .`
elif [ "$User" = ma ]
then    
    echo `scp $User@$n .`
elif [ "$User" = pr ]
then
    echo `scp $User@$n .`
elif [ "$User" = mi ]
then
    echo `scp $User@$n .`
else
    echo "UNKNOWN USER"
fi

脚本无法正常工作;我想要av它的值为 653

4

1 回答 1

0

您是否追求:

User=0
av="653"
ma="727"
pr="713"
mi="234"
n=192.168.1.20:/root/Ma
echo "Please select av ma ji im pr"
echo -n "First 2 initial of your Name eg: [av ma ji im pr]? "
read User
if [ "$User" = av ]
then echo `scp $av@$n .`
elif [ "$User" = ma ]
then echo `scp $ma@$n .`
elif [ "$User" = pr ]
then echo `scp $pr@$n .`
elif [ "$User" = mi ]
then echo `scp $mi@$n .`
else echo "UNKNOWN USER"
fi

如果不是,请澄清您的问题,以便更清楚您在寻找什么。我也不清楚你为什么有这个echo `scp $xy@$n .`符号——与 just 相比有什么好处scp $xy@$n .

scp="echo scp"  # For testing; you'd use just scp=scp in production work
User=0
av="653"
ma="727"
pr="713"
mi="234"
n=192.168.1.20:/root/Ma
echo "Please select av ma ji im pr"
echo -n "First 2 initial of your Name eg: [av ma ji im pr]? "
read User
if [ "$User" = av ]
then $scp $av@$n .
elif [ "$User" = ma ]
then $scp $ma@$n .
elif [ "$User" = pr ]
then $scp $pr@$n .
elif [ "$User" = mi ]
then $scp $mi@$n .
else echo "UNKNOWN USER"
fi

当我测试上面的脚本(文件xx.sh)时,输出是:

$ bash xx.sh
Please select av ma ji im pr
First 2 initial of your Name eg: [av ma ji im pr]? mi
scp 234@192.168.1.20:/root/Ma .
$

请注意,在 中bash,您可以使用间接扩展来简化代码:

scp="echo scp"  # For testing; you'd use just scp=scp in production work
User=0
av="653"
ma="727"
pr="713"
mi="234"
n=192.168.1.20:/root/Ma
echo "Please select av ma ji im pr"
echo -n "First 2 initial of your Name eg: [av ma ji im pr]? "
read User
case "$User" in
(av|ma|pr|mi) $scp ${!User}@$n . ;;
(*) echo "UNKNOWN USER"
esac

对于相同的输入,这给出了与先前脚本相同的结果。

于 2013-04-15T15:30:33.230 回答