linux - 如果变量声明在bash的for循环内,为什么不能更改输入数据
问题描述
所以我试图制作一个无限循环,创建库。
但文件变量只接受一次输入。
代码:
for (( ; ; ))
do
file=${1?Error: no input}
mkdir "$file"
sleep 1
done
解决方案
该循环中没有任何内容要求输入。$1
由用户在运行脚本时提供一次(甚至在循环开始之前)。在 shell 脚本中请求输入的标准方法是使用read
命令。像这样的东西:
while read -p "Enter a directory to create: " file; do
mkdir "$file"
done
此循环将在收到文件结尾时终止,这意味着用户必须按 Control-D 退出它。如果您想在用户只按返回而不输入任何内容的情况下退出,您可以这样做:
while read -p "Enter a directory to create: " file; do
if [ -z "$file" ]; then
echo "Error: no input" >&2
break # This exits the while loop
fi
mkdir "$file"
done
推荐阅读
- react-native - React Native:未捕获的错误未定义不是对象(评估'_reactNativeImagePicker.default.showImagePicker')
- ios - 金属 - 如何根据颜色重叠纹理
- r - 将数据集的列作为函数参数中的变量与数据集一起传递
- tensorflow - 如何从 TensorFlow 获取每个类的百分比预测?
- selenium - 线程中的异常未解决的编译问题:
- flutter - 不从 Firestore 获取帖子
- c - 定义 const char* 变量如何间接导致核心转储?
- function - 为什么将值分配给特定变量会在以下模型中提供帮助?
- firebase - 评估项目 ':app' 时出现问题。构建gradle问题
- loops - 将每次迭代保存在新行中