编写脚本filetype.sh,判断用户输入文件的文件路径,显示其文件类型,(普通,目录,连接,其他文件类型)。
时间: 2024-05-08 12:22:14 浏览: 47
可以使用 `stat` 命令来获取文件类型。以下是 `filetype.sh` 脚本的实现:
```bash
#!/bin/bash
read -p "Enter file path: " file_path
if [ -f "$file_path" ]; then
echo "$file_path is a regular file."
elif [ -d "$file_path" ]; then
echo "$file_path is a directory."
elif [ -L "$file_path" ]; then
echo "$file_path is a symbolic link."
else
echo "$file_path is a different type of file."
fi
```
首先,我们使用 `read` 命令来获取用户输入的文件路径。然后,我们使用 `if` 语句来测试文件的类型,使用 `-f`、`-d`、`-L` 分别判断普通文件、目录、链接文件。如果都不是,则表示为其他文件类型。
例如,如果用户输入的路径是 `/home/user/test.txt`,则输出结果为:
```
/home/user/test.txt is a regular file.
```
如果用户输入的路径是 `/home/user/testdir`,则输出结果为:
```
/home/user/testdir is a directory.
```
如果用户输入的路径是 `/home/user/testlink`(指向 `/home/user/test.txt`),则输出结果为:
```
/home/user/testlink is a symbolic link.
```
如果用户输入的路径是 `/home/user/test.unknown`,则输出结果为:
```
/home/user/test.unknown is a different type of file.
```