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

14 lines
492 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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."