[英]Shell Scripting if then else loop with scp
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
The script is not working properly; 该脚本无法正常运行; I want the value of
av
which is 653 我想要
av
值为653
Are you after: 你在追寻:
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
If not, please clarify your question so it is clearer what you are seeking. 如果不是,请澄清您的问题,以便更清楚地找到您要查找的内容。 I'm also not clear why you have the
echo `scp $xy@$n .`
notation — what is the benefit of that compared with just scp $xy@$n .
我也不清楚为什么要使用
echo `scp $xy@$n .`
表示-与仅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
When I tested the script above (file xx.sh
), the output was: 当我测试以上脚本(文件
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 .
$
Note that in bash
, you could use indirect expansion to simplify the code: 请注意,在
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
This gives the same result as the prior script for the same input. 对于相同的输入,这将提供与先前脚本相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.