如何在bash shell脚本中没有参数的情况下退出?

问题描述:

我已经编写了代码来计算文件和目录的数量,但是如果没有给出参数,我会努力使它退出。这里是我现在所拥有的,第一个是陈述是问题。如果没有提供参数,我该如何更改此if语句以使其退出?如何在bash shell脚本中没有参数的情况下退出?

#!/bin/bash 
if [$# -eq 0]; 
echo "no arguments" 
exit 1 
fi 
cd "$1" || exit 
n=0 
m=0 
for d in *; 
do 
    if [ -d "$d" ]; then 
     n=$((n+1)) 
    else 
     m=$((m+1)) 
    fi 
done 
echo "Files $m" 
echo "Directories $n" 
+0

使用shellcheck.net来诊断常见错误。 – chepner

if [ $# -eq 0 ]; then。该空间不是可选的,也不是then