如何在测试条件下使用 -d 选项的地方编写 while 循环?
How to write a while loop where I'm using -d option in test condition?
我如何在 while 循环中测试目录,如果目录不存在,那么它会执行以下空白操作。当目录不存在时,我试图提示用户输入,以便用户可以重试。一旦他们做对了,脚本就会退出。我的脚本有一个无限循环,我不知道如何修复它。这是我的 bash 脚本的样子:
#!/bin/bash
source_dir=
dest_dir=
while [[ ! -d "$source_dir" ]]
do
echo "This is not a directory. Enter the source directory: "
read "$source_dir"
done
while [[ ! -d "$dest_dir" ]]
do
echo "This is not a directory. enter the destination directory: "
read "$dest_dir"
done
当然,
read "$source_dir"
没有意义。要理解这一点,假设您的脚本是使用参数 FOO 调用的。因此,您首先将 source_dir
设置为 FOO,测试 [[ -d $source_dir ]]
将执行 [[ -d FOO ]]
并且目录 FOO 不存在。因此,您执行 read
命令,该命令将参数扩展为 read FOO
。这意味着从 STDIN 中读取一行并将其存储到变量 FOO 中。
如果你想改变变量source_dir
的值,你必须做一个
read source_dir
我如何在 while 循环中测试目录,如果目录不存在,那么它会执行以下空白操作。当目录不存在时,我试图提示用户输入,以便用户可以重试。一旦他们做对了,脚本就会退出。我的脚本有一个无限循环,我不知道如何修复它。这是我的 bash 脚本的样子:
#!/bin/bash
source_dir=
dest_dir=
while [[ ! -d "$source_dir" ]]
do
echo "This is not a directory. Enter the source directory: "
read "$source_dir"
done
while [[ ! -d "$dest_dir" ]]
do
echo "This is not a directory. enter the destination directory: "
read "$dest_dir"
done
当然,
read "$source_dir"
没有意义。要理解这一点,假设您的脚本是使用参数 FOO 调用的。因此,您首先将 source_dir
设置为 FOO,测试 [[ -d $source_dir ]]
将执行 [[ -d FOO ]]
并且目录 FOO 不存在。因此,您执行 read
命令,该命令将参数扩展为 read FOO
。这意味着从 STDIN 中读取一行并将其存储到变量 FOO 中。
如果你想改变变量source_dir
的值,你必须做一个
read source_dir