小编Edu*_*ich的帖子

在 bash 中循环读取直到给出正确的输入

您好,我想知道将命令作为变量传递给提示的正确方法是什么?例如,我有:

#!/bin/bash
clear ;
i=`ifconfig tap0 | awk '{print $2}' | egrep "([0-9]{1,3}[\.]){3}[0-9]{1,3}"`

read -p "Enter your IP: " prompt
        if [[ $prompt == i ]]
    then
        echo "Correct IP, congrats"
    else 
read -p "Wrong IP, try again: " prompt
        if [[ $prompt == i ]]
    then
        echo "Correct IP, congrats"
    else
        echo "Wrong IP for the second time, exiting."
    exit 0
fi
Run Code Online (Sandbox Code Playgroud)

我确信这可以循环,但我不知道如何,。我从 bash 脚本开始,所以我正在学习肮脏的方式:) 谢谢

linux bash

4
推荐指数
1
解决办法
1420
查看次数

标签 统计

bash ×1

linux ×1