如何包含一个循环来验证远程主机上是否存在文件,直到该语句在 KSH 中为真?



所以我正在尝试验证远程主机上是否存在文件,如果是,请说有效。如果没有,那就说它无效。我已经可以在一个函数中工作。但是,我似乎无法想出一个循环来确认此陈述是正确的,以便继续编写脚本。或者,在弄乱之后,它确实继续使用脚本,但它没有正确验证文件以重复提问。经过多次失败的尝试,这就是我想出的。

validate_d()
{
valid="file exists"
invalid="file doesn't exist"
ssh -q *host* [[ -f $userpath]] && echo "valid" || echo "invalid";
}
while (*this is my script asking the user if they input the correct path, if not, keep asking for correct path*)
done
validate_d
until [[ this statement is confirmed that the file exists ]]; do
print $invalid
read userpath
done

在 till 语句中,我无法弄清楚如何验证文件是否存在并且条件为 true。我应该使用 till 还是 while with if?任何形式的反馈都会有很大的帮助。谢谢!

将有很多方法可以 a( 测试远程文件是否存在和 b( 如何传回有效/无效的代码。

下面是一个裸露的示例:

while true
do
printf "Enter fullpath of file to check for: "
read userpath
ssh -q hostname [ -f "${userpath:-undefined}" ]
[ $? -eq 0 ] && echo 'valid' && break
echo 'invalid'
done

[ -f ${userpath} ]在名为"远程主机"的主机上远程执行。

如果找到由${userpath}表示的文件,则返回0(真(,否则返回1(假(。

$?包含来自ssh/-f ${userpath}构造的返回代码。

如果找到文件($?=0(,我们回显"有效"并脱离循环,否则我们打印"无效"并执行循环的下一次迭代。

假设远程主机上不存在/tmp/test1/tmp/test2文件,但文件/tmp/test3确实存在,运行上述代码片段如下所示:

$ while true
do
printf "Enter fullpath of file to check for: "
read userpath
ssh -q hostname [ -f "${userpath:-undefined}" ]
[ $? -eq 0 ] && echo 'valid' && break
echo 'invalid'
done
Enter fullpath of file to check for: /tmp/test1
invalid
Enter fullpath of file to check for: /tmp/test2
invalid
Enter fullpath of file to check for: /tmp/test3
valid
$ 

最新更新