简体   繁体   English

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

[英]Having trouble calling a bash script from another bash script

I have written a script for the purpose of calling it from other scripts and am having issues doing so.我编写了一个脚本,目的是从其他脚本中调用它,但这样做时遇到了问题。

The script I wish to call from other scripts works correctly:我希望从其他脚本调用的脚本可以正常工作:

#!/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

I have tested it by calling it from the command line.我已经通过从命令行调用它来测试它。 Below is a test script which I cannot seem to call the above script from successfully:下面是一个测试脚本,我似乎无法成功调用上述脚本:

#!/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

The output from the test script I am getting is:我得到的测试脚本中的 output 是:

/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

I have tried using the full path to the script, the relative path from the test script which would be ./ , and the way that it currently is ~/bin/scriptname .我尝试使用脚本的完整路径,测试脚本的相对路径是./ ,以及它当前的方式是~/bin/scriptname All give the same results.都给出相同的结果。

I have read some posts which seem to imply that what I am doing should work, such as:我读过一些似乎暗示我正在做的事情应该有效的帖子,例如:

StackOverflow post StackOverflow 帖子

Quote your args in Testscript 1:在 Testscript 1 中引用您的参数:

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

What am I doing incorrectly?我做错了什么?

I figured it out.我想到了。 The exit status is what I need, so I need to call it and set the variable to the exit status stored in $?退出状态是我需要的,所以我需要调用它并将变量设置为存储在$? . . The issue is that I was catching the stdout of the other script and storing it in the variable directories_are_same as if it were a return value that I was expecting, when what I needed was its exit status.问题是我正在捕获另一个脚本的标准输出并将其存储在变量directories_are_same _are_same 中,就好像它是我期望的返回值一样,而我需要的是它的退出状态。 I could echo something from the other script and then treat the stdout as a returned string, but that is not how this script was designed.我可以从另一个脚本中回显某些内容,然后将标准输出视为返回的字符串,但这不是该脚本的设计方式。

Here is the working test script:这是工作测试脚本:

#!/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

Now when the directories are different I get:现在,当目录不同时,我得到:

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

and when they are the same I get:当它们相同时,我得到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM