Write a shell script that prompt user input a text file name. If the file is not a ordinary file, then prompt user input again until the file inputed is a ordinary file.
时间: 2024-03-14 15:42:51 浏览: 81
shell script
Sure! Here's a shell script that does what you've described:
```bash
#!/bin/bash
while true; do
read -p "Please enter the name of a text file: " filename
if [ ! -f "$filename" ]; then
echo "Error: $filename is not a regular file."
else
break
fi
done
echo "You entered: $filename"
```
This script uses a `while` loop to repeatedly prompt the user for input until a valid filename is provided. The `read` command is used to read the user's input, which is then checked with the `[` command to see if it refers to a regular file. If the file is not a regular file, an error message is printed and the loop continues. If the file is a regular file, the loop is exited with `break`, and the script prints a confirmation message with the chosen filename.
阅读全文