编写一个Shell脚本,使该脚本可接收一个文件名,并实现文件类型判断的功能,至少支持3种文件类型
时间: 2024-06-13 19:09:27 浏览: 8
以下是一个Shell脚本,可以接收一个文件名,并实现文件类型判断的功能,支持3种文件类型(txt、pdf、jpg):
```shell
#!/bin/bash
filename=$1
filetype=$(file -b --mime-type "$filename")
if [[ $filetype == "text/plain" ]]; then
echo "The file is a txt file."
elif [[ $filetype == "application/pdf" ]]; then
echo "The file is a pdf file."
elif [[ $filetype == "image/jpeg" ]]; then
echo "The file is a jpg file."
else
echo "The file type is not supported."
fi
```
该脚本首先获取用户输入的文件名,然后使用`file`命令获取文件的MIME类型。根据MIME类型判断文件类型,如果是txt、pdf或jpg文件,则输出相应的提示信息,否则输出文件类型不支持的提示信息。
相关问题
编写一个Shell脚本,使该脚本可接收一个文件名,并实现文件类型判断的功能,至少支持3种文件类型。
以下是一个Shell脚本,可以接收一个文件名,并实现文件类型判断的功能,支持3种文件类型:文本文件、图片文件和压缩文件。你可以根据需要自行添加或修改文件类型的判断条件。
```shell
#!/bin/bash
# 获取文件名
filename=$1
# 判断文件类型
if [[ $(file -b --mime-type "$filename") == "text/plain" ]]; then
echo "This is a text file."
elif [[ $(file -b --mime-type "$filename") == "image/jpeg" || $(file -b --mime-type "$filename") == "image/png" ]]; then
echo "This is an image file."
elif [[ $(file -b --mime-type "$filename") == "application/zip" ]]; then
echo "This is a compressed file."
else
echo "Unknown file type."
fi
```
运行脚本时,需要给脚本文件添加执行权限,可以使用以下命令:
```shell
chmod +x script.sh
```
然后可以使用以下命令来运行脚本,并传入文件名作为参数:
```shell
./script.sh filename
```
其中,filename是你要判断类型的文件名。
用linux编写一个Shell脚本,使该脚本可接收一个文件名,并实现文件类型判断的功能,至少支持3种文件类型
以下是一个可以接收文件名并实现文件类型判断的Shell脚本,支持判断三种文件类型(文本文件、图片文件、压缩文件):
```shell
#!/bin/bash
# 获取文件名
filename=$1
# 判断文件类型
if [[ -f $filename ]]; then
file $filename | grep -q "text"
if [[ $? -eq 0 ]]; then
echo "$filename is a text file."
else
file $filename | grep -q "image"
if [[ $? -eq 0 ]]; then
echo "$filename is an image file."
else
file $filename | grep -q "compressed"
if [[ $? -eq 0 ]]; then
echo "$filename is a compressed file."
else
echo "Unknown file type."
fi
fi
fi
else
echo "$filename is not a file."
fi
```
该脚本首先获取用户输入的文件名,然后使用`file`命令判断文件类型。如果是文本文件,则输出“文件名 is a text file.”;如果是图片文件,则输出“文件名 is an image file.”;如果是压缩文件,则输出“文件名 is a compressed file.”;如果无法判断文件类型,则输出“Unknown file type.”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)