面试题答案
一键面试#!/bin/bash
if [ -f test.txt ] && [ -w test.txt ]; then
size=$(stat -c%s test.txt)
echo "文件大小为: $size 字节"
else
if [ ! -f test.txt ]; then
echo "文件 test.txt 不存在"
elif [ ! -w test.txt ]; then
echo "文件 test.txt 不可写"
fi
fi
if [ -f test.txt ] && [ -w test.txt ]; then
:检查当前目录下是否存在名为test.txt
的文件并且该文件可写。-f
用于判断文件是否存在且为普通文件,-w
用于判断文件是否可写。size=$(stat -c%s test.txt)
:获取文件大小,stat -c%s
格式输出文件大小(字节数)。- 如果不满足条件,再分别判断是文件不存在还是文件不可写,并输出相应提示信息。