【发布时间】:2021-03-19 09:19:51
【问题描述】:
下面你可以看到我的 bash 脚本
#createSession.sh
echo "Welcome to ADS2 Session Start..."
while [ true ]
do
echo "Starting service Daemon on the targert"
echo "Enter the ip-address of the target"
read x
if [ -z "$x" ]
then
echo "ip-address is empty"
else
echo "Connecting with target..."
if [[ $x =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
ssh nvidia@"$x" "/opt/ads2/arm-linux64/bin/ads2 svcd&"
echo "connection succeded"
break
else
"Make sure the io address in the coorect form"
fi
fi
done
第一个问题
如您所见,脚本会检查用户输入是否符合 ip-address 形式。但是,我希望脚本也接受 ip 地址作为具有这种形式fdt-c-agx-4arbitrary numbers 的字符串。字符串的最后一部分可以是任意 4 个数字。结果,if语句就像
if [[ $x =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || [ $x = #machtes fdt-c-agx-4arbitrary digits]
第二个问题
IP 地址可以以正确的形式输入,但不存在。因此,ssh 在这种情况下返回 ssh connection time out。但是字符串connection succeded 将被打印出来,这是不正确的。那么我怎样才能让脚本足够聪明地意识到这种情况下的连接没有建立呢?
提前问好
【问题讨论】:
-
As a result, the if statement be likeSoo...这样写?您以^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$为例,因此只需编写与另一个匹配的相同内容即可。 -
那么您认为
[0-9]是什么意思?我推荐网络上提供的正则表达式填字游戏,以便立即学习正则表达式。 -
@KamilCuk 你写的。 0 到 9 之间的数字
标签: bash shell ubuntu if-statement