如何更有效地编写 shell

今天看到这样一篇文章 A Shell Script to do shell scripting faster
确实很有使用价值,毕竟我们不能保证,我们写的 shell
一次就能做到无错,那不断地修改就是必须的,
文章作者提供的一个脚本就提供一种就好的工作机制:

#!/usr/bin/bash

if [ ! -f $1 ]; then
  echo "#!/usr/bin/bash" > $1
fi

while [ 1 ];
do
        vi $1
        chmod 755 $1
        ./$1
        read dummy
done

不光 bash 可以如此,其他的 shell 也可以:

#!/usr/bin/bash

file=`echo $1 | sed 's/\(.*\)\.\(.*\)/\1/'`
ext=`echo $1 | sed 's/.*\.\(.*\)/\1/'`

if [ ! -f $1 ]; then
        if [ $ext = "pl" ]; then
                echo "#!/usr/bin/perl " > $1
        elif [ $ext = "py" ]; then
                echo "#!/usr/bin/python " > $1
        elif [ $ext = "c" ]; then
                echo "#include<stdio.h>" >> $1
                echo -e "int main()\n{ \n\n} " >> $1
        elif [ $ext = "sql" ]; then
                echo "WHENEVER SQLERROR EXIT 1 " > $1
                echo "SET SERVEROUTPUT ON" >> $1
                echo "SET PAGESIZE 0" >> $1
                echo "SET FEEDBACK OFF" >> $1
                echo "SET VERIFY OFF" >> $1
                echo "SET HEADING OFF" >> $1
                echo -e "\n\nDECLARE\n\n\n" >> $1
                echo -e "BEGIN\n\n\n" >> $1
                echo -e "END;\n/" >> $1
        else
                echo "#!/usr/bin/bash" > $1
        fi
fi


while [ 1 ];
do
        vi $1
        chmod 755 $1
        if [ $ext = "java" ]; then
                javac $1
                [ $? -eq 0 ] && java $file
        elif [ $ext = "c" ]; then
               gcc $1
               [ $? -eq 0 ] && ./a.exe
        elif [ $ext = "sql" ]; then
                sqlplus  -s sys/unix11@xe  @$1 <<EOF
                exit
EOF
        else
                ./$1
        fi
        read dummy
done

Post a Comment

Your email is never shared. Required fields are marked *

*
*