shell脚本判断文件是否为空,如果为空输出1,不为空输出0
时间: 2024-03-15 12:45:09 浏览: 40
可以使用以下shell脚本来实现:
```bash
#!/bin/bash
if [ -s "$1" ]
then
echo "0"
else
echo "1"
fi
```
其中,`$1`表示传入的文件路径参数。`-s`表示判断文件是否存在且不为空。如果文件存在且不为空,则输出0;否则输出1。
相关问题
shell脚本判断/home/user/test.txt文件是否为空,如果为空输出1,不为空输出0
可以使用以下shell脚本来实现:
```bash
#!/bin/bash
if [ -s "/home/user/test.txt" ]
then
echo "0"
else
echo "1"
fi
```
其中,`-s`表示判断文件是否存在且不为空。如果`/home/user/test.txt`文件存在且不为空,则输出0;否则输出1。
shell脚本判定文件是否为空
您好,要判断文件是否为空,可以使用以下的shell脚本命令:
```shell
#!/bin/bash
file="/path/to/file"
if [ -s "$file" ]; then
echo "文件不为空"
else
echo "文件为空"
fi
```
其中,`/path/to/file`是您要判断的文件路径,保存在变量`file`中。在`if`语句中,使用`-s`选项判断`$file`是否为一个非空文件。如果是,输出“文件不为空”,否则输出“文件为空”。您可以将脚本中的路径修改为您要判断的文件路径,然后将脚本保存为`.sh`文件,运行即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)