检查用户输入是单个参数并且文件名存在

时间:2018-02-09 04:12:45

标签: bash shell

我正在尝试创建一个shell脚本,它将文件名作为参数,然后以字节为单位显示文件的文件大小。但是,我不确定如何检查用户是否只提供了一个参数,并且文件名是否存在。

任何帮助都会被批评

{{1}}

1 个答案:

答案 0 :(得分:-1)

你走了,

#!/bin/bash
# get filename
fileName="$1"
# check that the user supplied only a single argument 
if [ -z $1 ]; then
echo "Syntax: $(basename $0) filename"
exit 1
fi
# make sure file exits for reading
if [ ! -f $fileName ]; then
echo "Filename $fileName does not exists"
exit 1
fi
# display the file size of the file in bytes
actualsize=$(wc -c <"$file")
echo size is $actualsize Bytes