我正在编写一个脚本,需要生成另一个用于关闭应用服务器的脚本......
我的代码如下所示:
echo "STEP 8: CREATE STOP SCRIPT"
stopScriptContent="echo \"STOPING GLASSFISH PLEASE WAIT...\"\n
cd glassfish4/bin\n
chmod +x asadmin\n
./asadmin stop-domain\n
#In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict\n"
${stopScriptContent} > stop.sh
chmod +x stop.sh
但它没有被正确创建,输出 stop.sh 如下所示:
"STOPING GLASSFISH PLEASE WAIT..."\n cd glassfish4/bin\n chmod +x asadmin\n ./asadmin stop-domain\n #In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict\n
如你所见,很多事情都是错误的:
- 没有 echo 命令
- 按字面意思理解 \n,所以没有换行符
我的疑问是:
.sh
使一个脚本创建另一个脚本的正确方法是什么.sh
。- 您认为我做错了什么?
答案1
如果您想使用带有转义字符的 echo,\n
则应添加开关-e
。echo -e " ... "
但是,使用cat
这里的文件反而
cat > stop.sh <<EOF
echo "STOPING GLASSFISH PLEASE WAIT..."
cd glassfish4/bin
chmod +x asadmin
./asadmin stop-domain
#In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict
EOF
chmod +x stop.sh
答案2
从您的代码中删除 \n 并简单地执行此操作对我来说是有效的。
#!/bin/bash
echo "#!/bin/bash
echo 'Hello World'" > b.sh
bash b.sh
输出结果;
Hello World