Shell Scripting如果然后用scp循环

时间:2013-04-15 14:02:15

标签: shell

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

1 个答案:

答案 0 :(得分: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 .`符号 - 与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中,您可以使用indirect expansion来简化代码:

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

这给出了与同一输入的前一个脚本相同的结果。