qfedu-linux-advanced-level/day1/homework/h7.sh

14 lines
492 B
Bash
Raw Normal View History

2023-08-14 20:13:50 +08:00
#!/bin/bash
# 编写一个Shell脚本接受一个文件路径作为参数并检查该文件是否存在。如果存在则输出 "文件名和文件大小",否则输出 "File does not exist."
# 方法一
if [ -e $1 ]; then
output=`ls -lh $1 | awk '{print $5}'`
echo "文件名: $1, 文件大小: $output"
else
echo "File does not exist."
fi
# 方法二
[ -e $1 ] && echo "文件名: $1, 文件大小: `ls -lh $1 | awk '{print $5}'`" || echo "File does not exist."