用内容创建shell脚本

时间:2013-11-03 19:01:36

标签: bash shell

如何创建一个shell脚本来创建包含大量内容的文件。我目前有这个,它似乎没有运作良好。任何更清洁的方法也受到欢迎。

echo "Enter 1 to generate View"
echo "Enter 2 to generate Model and View"
echo "Enter 3 to generate View, Model and Template"

while true; do
read -p "Please select an option: " option
read -p "Please enter the name of the class you wold like to genrate: " class
case $option in
    1 ) 
        FILE="views/$class"
        /bin/cat <<EOM >$FILE
            some content        
        EOM
        break;;
    2 ) 
        exit;;
    3 ) 
        exit;;
    * ) 
        echo "Enter 1 to generate View"
        echo "Enter 2 to generate Model and View"
        echo "Enter 3 to generate View, Model and Template";;
esac
done

1 个答案:

答案 0 :(得分:4)

第二个EOM应该在一行的开头,或者使用

 # code
    cat<<-EOM
    ...
    EOM

所以这里:

    /bin/cat <<EOM >$FILE
some content        
EOM

注意:

barmar所述,如果您使用<<-EOM,请使用制表符而不是空格。