2012-02-21 69 views
0

我有一个混帐以下post-commit钩子:这可以摧毁我的整个网站吗?

#!/bin/sh 
# Save this in: /home/qwertymk/website-live.git/hooks/post-receive 

# let's save all of out js files before the checkout 
FILES="$(find /home/qwertymk/public_html -type f -name '*.js')" 
for f in $FILES 
do 
if [[ $f =~ /home/qwertymk/ignore-dir/dev-staging-area/ ]]; then continue; fi 
    echo "copying - $f" 
    cp $f $f.jscache 
done 
# done saving js files before checkout 

export GIT_WORK_TREE=/home/qwertymk/public_html/ 
git checkout -f 

# minify each js file that changed 
cd /home/qwertymk/website-live.git 
FILES=$(git whatchanged -n 1 --pretty=format: --name-only | grep '\.js$') 
for f in $FILES 
do 
    echo "processing - $f" 
    rm /home/qwertymk/public_html/$f.jscache 
    php /home/qwertymk/jsmin/curl.php /home/qwertymk/public_html/$f 
done 
# done minifing 

# anything that has a .jscache should be moved to the .js 
FILES="$(find /home/qwertymk/public_html -type f -name '*.js')" 
for f in $FILES 
do 
if [[ $f =~ /home/qwertymk/public_html/dev-staging-area/ ]]; then continue; fi 
if [ ! -f $f.jscache ]; then continue; fi 
    echo "restoring - $f" 
    rm $f 
    mv $f.jscache $f 
done 

有什么不对的脚本?

我可以安心使用吗?

是否有任何角落的情况下,这个剧本可以搞砸我的网站?

+2

有什么特别令你担心吗? – abresas 2012-02-21 03:20:07

+0

@abresas:事实上,我可能会意外删除所有myjs文件 – qwertymk 2012-02-21 03:32:45

+2

看看其他文章,使用'find ... |同时读取文件名;在S.O.上张贴这里。它每天都会被提及。使用'FILES = $(find ...)'将在文件名中有空格或其他奇数字符时破坏,无论是通过设计还是事故。它还有助于将'rm $ f'转换为'printf - rm $ f'\ n“'等,这样您就可以看到将执行的命令。当你满意时,输出是安全的,然后将整个脚本输出重定向到一个shell。即'myGitThing | bash'。祝你好运。 – shellter 2012-02-21 03:55:32

回答

3

看那个在这里S.O.使用

find ... -print0 | xargs ??? | while read filename ; do ... 

帖子其他职位它每天都会被提及。如果您的find版本支持-print0选项(表示以空字符结束的字符串输出),那么您已经设置好了。您需要为xargs找到相应的参数,以表明您使用以空字符结尾的字符串进行发送。

使用FILES=$(find ...)当文件名中有空格或其他奇怪的字符时(无论是通过设计还是意外)都会中断,因此这就是为什么要使用-print0(如果可以)。

为了确保安全,请将所有破坏性代码(如rm $f)转换为printf -- rm $f "\n"等,以便您可以在执行它们之前查看将执行的命令。

当您满意输出是安全的,然后将整个脚本输出重定向到一个shell。即

myGitThing | bash 
1

我自己拿上looping over find resultsfeatured上的代码审查),这是足够安全处理任何字符,你可以把一个路径(其相当于用于在生产服务器上移动和删除重要文件):

while IFS= read -r -d '' -u 9 
do 
    file_path="$(readlink -fn -- "$REPLY"; echo x)" 
    file_path="${file_path%x}" 
    echo "START${file_path}END" # Literal 
    printf %q "$file_path" # Bash escaped 
done 9< <(find /home/qwertymk/public_html -type f -name '*.js' -print0) 

请参阅链接了解所有奇怪语法的解释。

相关问题