Bash - 自解压脚本错误

时间:2014-06-12 10:49:48

标签: linux bash opensuse self-extracting

我正致力于在Open SuSE上创建自解压shell脚本。我引用了this link。我的代码如下所示 -

#!/bin/bash
function die ()
            { echo "Error!"; 
                exit 1 ;
            }

echo Extracting...

# Script will need to find where is the archive. So search for "ARCHIVE" in this script and
# save the line number

archive=$(grep --text --line-number 'ARCHIVE:$' $0)

echo $archive

tail -n +$((archive + 1)) $0 | gzip -vdc - | tar -xvf - > /dev/null || die

echo Extraction completed

exit 0

ARCHIVE:

在我执行上面的脚本后,我得到了以下输出。我认为这是不正确的并导致错误。

Extracting...
22:ARCHIVE:
./symhelp_self_extract.sh: line 16: 22:ARCHIVE:: syntax error in expression (error token is ":ARCHIVE:")

gzip: stdin: unexpected end of file
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
Error!

任何人都可以解释这是什么语法错误吗?

谢谢,

Omky

1 个答案:

答案 0 :(得分:2)

如果您需要行号:

archive=$(grep --text --line-number 'ARCHIVE:$' "$0")
archive=${archive%%:*}

或者

archive=$(awk '/ARCHIVE:$/{print NR; exit}' "$0")

你问题的原因是你试图对不是数字的东西进行算术运算:

$((archive + 1))  ## here $archive = 22:ARCHIVE: before

总是引用你的变量:

archive=$(grep --text --line-number 'ARCHIVE:$' "$0")
...
tail -n "+$((archive + 1))" "$0"

使用awk你会得到一个更简单的方法:

awk -v r='ARCHIVE:$' '!p && $0 ~ r { p = 1; getline } p' "$0" | gzip -vdc - | tar -xvf - > /dev/null || die