Linux 从另一个bash脚本调用bash脚本时遇到问题

Linux 从另一个bash脚本调用bash脚本时遇到问题,linux,bash,shell,Linux,Bash,Shell,我已经编写了一个脚本,用于从其他脚本调用它,但在这样做时遇到了问题 我希望从其他脚本调用的脚本正常工作: #!/bin/bash #################################################################################################### # This script will compare two directories given as args. # # exit status: 0 -->

我已经编写了一个脚本,用于从其他脚本调用它,但在这样做时遇到了问题

我希望从其他脚本调用的脚本正常工作:

#!/bin/bash

####################################################################################################
# This script will compare two directories given as args.
#
# exit status: 0 --> if directories are the same
#              1 --> if directories are not the same
#              2 --> incorrect number of args
#              3 --> one or more of the directories does not have read permissions
#              4 --> one or more of the directories does not exists
#              5 --> one or more of the directories is not a directory
####################################################################################################


[ $# -ne 2 ] && exit 2

[ ! -r "$1" ] || [ ! -r "$2" ] && exit 3
[ ! -e "$1" ] || [ ! -e "$2" ] && exit 4
[ ! -d "$1" ] || [ ! -d "$2" ] && exit 5

dir_1="$1"
dir_2="$2"
sha1_dir_1=$(find "$dir_1" -type f \( -exec sha1sum {} \; \) | awk '{print $1}' | sort | sha1sum)
sha1_dir_2=$(find "$dir_2" -type f \( -exec sha1sum {} \; \) | awk '{print $1}' | sort | sha1sum)

[ "$sha1_dir_1" = "$sha1_dir_2" ] && exit 0
[ "$sha1_dir_1" != "$sha1_dir_2" ] && exit 1
我通过从命令行调用它来测试它。下面是一个测试脚本,我似乎无法成功调用上面的脚本:

#!/bin/bash

dir_1="~/test"
dir_2="~/test1"
directories_are_same="$(~/bin/compare_directories "$dir_1" "$dir_2")"

{
    if [ $directories_are_same -eq 3 ]; then
        echo "One of the directories $dir_1 and $dir_2 does not have read permissions!"
        exit 1
    elif [ $directories_are_same -eq 4 ]; then 
        echo "One of the directories $dir_1 and $dir_2 does not exist!"
        exit 1
    elif [ $directories_are_same -eq 5 ]; then
        echo "One of the directories $dir_1 and $dir_2 is not a directory!"
        exit 1
    fi
} >&2

if [ $directories_are_same -eq 0 ]; then
    echo "The directories $dir_1 and $dir_2 contain identical content"
    exit 0
elif [ $directories_are_same -eq 1 ]; then
    echo "The directories $dir_1 and $dir_2 do not contain identical content"
    exit 0
else
    echo "Something went wrong" >&2
    exit 1
fi
我得到的测试脚本的输出是:

/home/astral/bin/updates_dir_if: line 8: [: -eq: unary operator expected
/home/astral/bin/updates_dir_if: line 11: [: -eq: unary operator expected
/home/astral/bin/updates_dir_if: line 14: [: -eq: unary operator expected
/home/astral/bin/updates_dir_if: line 20: [: -eq: unary operator expected
/home/astral/bin/updates_dir_if: line 23: [: -eq: unary operator expected
Something went wrong

我尝试使用脚本的完整路径、测试脚本的相对路径(即
/
)以及当前的方式
~/bin/scriptname
)。所有的结果都是一样的

我读过一些帖子,这些帖子似乎暗示我正在做的事情应该有效,例如:

在Testscript 1中引用您的参数:

echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 "$1" "$2"

我做错了什么?

我明白了。退出状态是我需要的,因此我需要调用它并将变量设置为存储在
$?
中的退出状态。问题是,我捕获了另一个脚本的stdout,并将其存储在变量
目录\u are\u same
中,就像它是我期望的返回值一样,而我需要的是它的退出状态。我可以回显另一个脚本中的内容,然后将stdout视为返回的字符串,但这不是这个脚本的设计方式

以下是工作测试脚本:

#!/bin/bash

dir_1=~/test
dir_2=~/test1
~/bin/compare_directories "$dir_1" "$dir_2"
directories_are_same="$?"

{
    if [ "$directories_are_same" -eq 3 ]; then
        echo "One of the directories $dir_1 and $dir_2 does not have read permissions!"
        exit 1
    elif [ "$directories_are_same" -eq 4 ]; then 
        echo "One of the directories $dir_1 and $dir_2 does not exist!"
        exit 1
    elif [ "$directories_are_same" -eq 5 ]; then
        echo "One of the directories $dir_1 and $dir_2 is not a directory!"
        exit 1
    fi
} >&2

if [ "$directories_are_same" -eq 0 ]; then
    echo "The directories $dir_1 and $dir_2 contain identical content"
    exit 0
elif [ "$directories_are_same" -eq 1 ]; then
    echo "The directories $dir_1 and $dir_2 do not contain identical content"
    exit 0
else
    echo "Something went wrong" >&2
    exit 1
fi
现在,当目录不同时,我得到:

The directories /home/astral/test and /home/astral/test1 do not contain identical content
当它们相同时,我得到:

The directories /home/astral/test and /home/astral/test1 contain identical content

使用
bash-x yourscript运行调用脚本
有关函数输出与退出状态的相关问题:@AstralAxiom:脚本在命令行中运行良好,并不意味着它在其他上下文中运行良好。脚本可能对环境、工作目录等敏感。此外,一个可能的错误是您没有引用
$directories\u are\u same
。虽然如果一切顺利,它可能会起作用,但在您的情况下,它实际上取决于
比较目录
的正确输出,引用它意味着您不必担心的一件事。我不明白您为什么要抓住
比较目录
的标准。实际上,比较的结果是通过退出代码传递给调用方的,而不是标准输出。@AstralAxiom:我现在看到您已经在自己的答案中反映了这一点。在本例中,引号约为$?当然,$directories\u和$directories\u是相同的,这是不必要的,但它们并没有害处。可能不需要完整的路径<代码>“~/test”不起作用,因为您引用了它,而bash会逐字解释引用的
~
(意思是,这不是您的主目录,而是一个名为
~
的目录)
dir_1=~/test
也应该有效(请注意缺少的引号)。您不应该通过添加新信息来更新答案,而应该重新编写答案,就像您已经知道了一切一样–历史记录保存在编辑历史记录中。好的,谢谢!当我没有注意到:)@AstralAxiom:Bash不是一种编程语言,它是一种脚本语言时,我不想承担这个责任。脚本语言是一种编程语言。至少bash是