Making shapes with linux shell script

2019-09-15 03:11发布

For an assignment, I'm trying to make a shell script that will print a triangle that looks like the following:

+
| \
|  \
|   \
|    \
+-----

Here is my code in VIM:

echo'+
     | \
     |  \
     |   \
     |    \
     +----- '

However, instead of getting that as the output when I run the script, it outputs as the following:

VI output issue

Can anybody tell me what I'm doing wrong?

标签: linux shell
2条回答
SAY GOODBYE
2楼-- · 2019-09-15 03:58

Try this

#!/bin/bash
echo '
     +
     | \
     |  \
     |   \
     |    \
     +----- '

just start it on the next line since you need spaces before the "+"

查看更多
祖国的老花朵
3楼-- · 2019-09-15 03:58

How did your output got merged to 3 lines?
I think your original command was with a space after echo and double quotes:

echo "+
     | \
     |  \
     |   \
     |    \
     +----- "

And now pay attention to the last character of each line. When the last character is the \, the following line is appended to the current line.
Make sure each line ends with a space (or use single quotes).

查看更多
登录 后发表回答