基本 if then 语句遇到问题

基本 if then 语句遇到问题

学习脚本编写的基础知识,我有这行代码:

#!/bin/sh


if [ $# -ne 1]  
then
   echo "Please provide one directory to search"
   echo "Example usage: something.sh directory_name"
   exit 1
fi

但是,当我运行此命令时,我收到错误,if: Expression Syntax.我在这里到底缺少什么?下一个 if 语句也给了我同样的错误:(我希望它检查变量是否是目录

if [ !-d $1 ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi

完整脚本以防有帮助:

#!/bin/sh
if [ $# -ne 1 ]
then
   echo "Please provide one directory to search"
   echo "Example usage: something directory_name"
   exit 1
fi
if [ ! -d $1 ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi
cd "$1"
read=0
write=0
exe=0
numFiles=0
numDir=0
for filename in *
do
if [ -r filename ]
then
   `expr $read + 1`
fi
if [ -x filename ]
then
   `expr $exe + 1`
fi
if [ -w filename ]
then
   `expr $write + 1`
fi
if [ -f filename ]
then
   `expr $numFiles + 1`
fi
done
for dir in */
do
if [ -r dir ]
then
   `expr $read + 1`
fi
if [ -x dir ]
then
   `expr $exe + 1`
fi
if [ -w dir ]
then
    `expr $write + 1`
fi
if [ -d dir ]
then
   `expr $numDir + 1`
fi
done
total=`expr $numDir + $numFiles`
echo "In the directory $1"
echo "  Number of directories     : $numDir"
echo "  Number of files           : $numFiles"
echo "  Number of readable items  : $read"
echo "  Number of writable        : $write"
echo "  Number of executable items: $exe"
echo "-------------------------------"
echo "Total number of items: $total"

更新:以防万一有人偶然发现这一点,我在#!/bin/bash导致 if 表达式错误之前有标题注释

答案1

你有很多问题。这是工作版本。您需要使用 $ 来表示变量的值($filename而不是filename),并且需要将表达式的结果分配给变量,而不是仅仅调用表达式(read=expr $read + 1而不是expr $read + 1)。

 #!/bin/bash
if [ $# -ne 1 ]
then
   echo "Please provide one directory to search"
   echo "Example usage: assignment4.sh directory_name"
   exit 1
fi
if [ ! -d "$1" ]
then
   echo "$1: Invalid Directory. Aborting Script."
   exit 1
fi
cd "$1"
read=0
write=0
exe=0
numFiles=0
numDir=0
for filename in *
do
if [ -r "$filename" ]
then
   read=`expr $read + 1`
fi
if [ -x filename ]
then
   exe=`expr $exe + 1`
fi
if [ -w "$filename" ]
then
   write=`expr $write + 1`
fi
if [ -f "$filename" ]
then
   numFiles=`expr $numFiles + 1`
fi
done
for dir in */
do
if [ -r "$dir" ]
then
   read=`expr $read + 1`
fi
if [ -x "$dir" ]
then
   exe=`expr $exe + 1`
fi
if [ -w "$dir" ]
then
    write=`expr $write + 1`
fi
if [ -d "$dir" ]
then
   numDir=`expr $numDir + 1`
fi
done
total=`expr $numDir + $numFiles`
echo "In the directory $1"
echo "  Number of directories     : $numDir"
echo "  Number of files           : $numFiles"
echo "  Number of readable items  : $read"
echo "  Number of writable        : $write"
echo "  Number of executable items: $exe"
echo "-------------------------------"
echo "Total number of items: $total"
$ ./script.sh
Please provide one directory to search
Example usage: something.sh directory_name

$ ./script.sh /home/username

In the directory /home/username
  Number of directories     : 8
  Number of files           : 104
  Number of readable items  : 120
  Number of writable        : 120
  Number of executable items: 8
-------------------------------
Total number of items: 112

答案2

你缺少的是空格。

这有效:

if [ $# -ne 1 ]
then
   echo "One argument needed"
   exit 1
fi

同样地:

if [ ! -d $1 ] 

相关内容