2013-11-03 68 views
2

如何创建一个用一大堆内容创建文件的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 

回答

4

第二EOM应该是在一行的开头或使用

# code 
    cat<<-EOM 
    ... 
    EOM 

所以在这里:

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

注:

barmar说,如果你使用<<-EOM,请使用制表符不能包含空格。

+2

请注意,使用'<< - EOM'时,'EOM' **之前的缩进必须是TAB,而不是空格。 – Barmar