I
I
I_Vetrov2015-10-30 15:57:37
linux
I_Vetrov, 2015-10-30 15:57:37

Why doesn't exit 1 work everywhere in sh script code?

Hello!
There is a pre-commit hook script in sh for subversion. Among other things, the script must forbid committing files larger than a certain size. The fact is that even if the size ratio condition is met and the if-then path is followed, the script does not execute exit 1 and does not display the echo message .
With the help of different echo , it was found out that the script accurately receives and correctly interprets the values ​​of all variables, and also makes a comparison. If you write some echo before the line

echo "File '$file' too large to commit" >&2
and after exit 1 -- they will be executed.
Ultimately, a file of any size is committed, despite the fact that the script correctly compares and follows the branch with exit 1 .
There is also a part with a check for extensions in this script (not shown here), exit 1 is normally executed there.
In general, some oddities.
I would be grateful for any advice.
#!/bin/sh
REPOS="$1"
TXN="$2"
SVNLOOK="/usr/local/bin/svnlook"
maxsize=20000000 #ограничение на максимальный размер файла (не коммита целиком) в байтах
$SVNLOOK changed -t $TXN $REPOS | while read status file
do
     && continue  # Skip Deletions
     && continue     # Skip directories
    size=$($SVNLOOK filesize -t $TXN $REPOS $file)
    if [ $size -gt $maxsize ]
    then
        echo "File '$file' too large to commit" >&2
        exit 1
    fi
done
exit 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Softer, 2015-10-30
@I_Vetrov

What if something like that?

while read status file
do
     && continue  # Skip Deletions
     && continue     # Skip directories
    size=$($SVNLOOK filesize -t $TXN $REPOS $file)
    if [ $size -gt $maxsize ]
    then
        echo "File '$file' too large to commit" >&2
        exit 1
    fi
done < <($SVNLOOK changed -t $TXN $REPOS)

Y
younghacker, 2015-10-30
@younghacker

First of all, I would reverse the while loop so that it does not run in a subshell
AND output the result of the command to a file so that later I can analyze whether the contents are expected.

SVNRESULT=`$SVNLOOK changed -t $TXN $REPOS`
echo -e"`date '+%F %T'`\n${SVNRESULT}" >> ~/debug-svn.log
while read status file; do
....
done <<<"${SVNRESULT}"

And put all variables in double quotes, there may be spaces in filenames.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question