linux - Bash 脚本错误

标签 linux bash shell

我正在尝试运行这些脚本,但我不断收到错误消息:

1-

#!/bin/bash

filename=$1


if [ -f $filename ]
then
    owner=`stat -c %U $filename`
    grep $owner /etc/passwd
    if [ $? -eq 0 ]; then
            perm=`stat -c %a $filename | head -c 1`
            if [ $perm -gt 3 ]; then
                cat $filename | grep NOTE
            fi
        fi
fi

错误信息是:

stat: missing operand Try `stat --help' for more information.


2-

#!/bin/bash

NoSum=$1
sum=0
echo "Please enter $NoSum values one at a time"
for (( i=1; i<=$NoSum; i++ ))
do
    echo "Next Value?"
    read num
    let "a = $sum + $num"
    sum=$a
done

echo "The sum is : $sum"

错误信息是:

Please enter values one at a time ./scr3: line 6: ((: i<=: syntax error: operand expected (error token is "<=") The sum is : 0


3-

#!/bin/bash

dir=$1

if [ -d $dir ]
    then
    perm=`stat -c %a $dir | head -c 1`
    if [ $perm -gt 5 ]; then
        cd $dir
        for file in $dir/*
        do
            if ! [ -x "$file" ]
            then
                echo "$file"
            fi
        done
    fi
fi

错误信息是:

stat: missing operand Try `stat --help' for more information. ./scr4: line 8: [: -gt: unary operator expected


知道如何修复它们吗?

最佳答案

程序没有任何问题。您没有提供命令行参数。您必须按以下方式运行它

1 和 3:

./script.sh <filename>

2:

./script.sh <number>

$1 代表第一个命令行参数

关于linux - Bash 脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22825717/

相关文章:

php - 通过在某些 RHEL/CentOS 安装上使用 file_exists on/proc/pid 阻止 php 查看进程是否存在

java - 来自 Java 的 Linux 命令行指令

c++ - 使用 C++ 读取 tcpdump 文件

c++ - 当父进程被杀死时,使用 fork() 创建的子进程是否会自动被杀死?

C++守护进程服务器停止

c++ - 是否可以缓存从 MapViewOfFile 返回的映射区域?

linux - 从 grep 输出中删除 </p> 标签

bash - 让 ssh 在目标机器上后台执行命令

bash - bash 陷阱 ERR 中的神秘 LINENO

Linux:将DD结果输出到文本文件