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:
/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
. All give the same results.
I have read some posts which seem to imply that what I am doing should work, such as:
Quote your args in Testscript 1:
echo "TestScript1 Arguments:" echo "$1" echo "$2" echo "$#" ./testscript2 "$1" "$2"
What am I doing incorrectly?