Checking User Input Is A Single Argument And That The File Name Exists
本问题已经有最佳答案,请猛点这里访问。
我正在尝试生成一个shell脚本,该脚本将文件名作为参数,然后以字节为单位显示文件的大小。但是,我不确定如何检查用户只提供了一个参数以及文件名是否存在。
任何帮助都会被告知
1 2 3 4 5 6 | #!/bin/bash FILENAME-echo -n :Enter name of file" read sourcefile FILESIZE=$(stat -c%s"FILENAME") echo"Size of $FILENAME = $FILESIZE bytes." |
干得好,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/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 |