我正致力于在Open SuSE上创建自提取shell脚本。我引用这个链接。我的代码如下所示-
#!/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
如果需要行号:
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