1

first let me say I have been teaching myself Linux, and I am not "techie" knowledgeable yet. I am using Ubuntu 18.04 and find myself using the BASH to create files I can run. One thing I am trying to do is this. I love ASCII art. I want to create a script that will take the file I tell it (ex: ./convert moon) moon being a text file containing the ASCII art. I want to get the script to edit the file I tell it to edit each line with echo " at the beginning, and " at the end of the line, for each line in the file. Lastly put the #!/bin/bash statement as the first line. I have been experimenting with different methods, but can't seem to get it right. If this was a Windows BAT file I would have no problems doing this. So any help anyone can give will be greatly welcomed...thanks in advance.

  • Why would you want this? What would be the use for that script? I mean, what is the benefit of creating a script to output what you already have in the file? Why not just cat moon? – terdon Nov 20 '18 at 11:34
  • files that contain ANSI pictures have command variables like / \ in them to makeup the picture. Back in the day, ANSI.SYS would handle this quite nicely when you ran it, and if color if any. That is the problem I was running into when ran, the system would think certain characters was variables and return errors. To avoid this I needed to put the echo " at the start and the closing " ending of each line doing it manually takes alot of time if you have a large picture. Yes I know it might seem stupid to some, but I happen to like ASCII and ANSI artwork – Asgi Wesa Dec 10 '18 at 16:58
  • Nothing stupid about that, I just don't understand why you are saying the echo will make a difference. Adding echo to each line will result in exactly the same thing as simply running cat file. – terdon Dec 10 '18 at 17:03
3

Remember to quote special characters, I wrote this answer covering this topic.

That can actually be done with a single sed call:

<moon sed -e 's/.*/echo "&"/' -e '1s_^_#!/bin/bash\n_' >moon.bash

This takes moon as the input file, the first expression substitutes every line with “echo "original line content"”, the second one substitutes the first line‘s beginning with the shebang followed by a newline character, and the output is stored as moon.bash.

If you use it regularly, I recommend adding it as a function to your ~/.bashrc file, e.g.:

ascii_convert(){ <$1 sed -e 's/.*/echo "&"/' -e '1s_^_#!/bin/bash\n_' >$1.bash ;}

(Don’t forget to save the file and source it with . ~/.bashrc ) This way you can convert any file with a simple:

ascii_convert moon

Example run

$ cat moon
1
2
3
$ ascii_convert moon
$ cat moon.bash 
#!/bin/bash
echo "1"
echo "2"
echo "3"
  • 1
    Use this script very careful! You will get errors if you have " symbol in any line of source script\file. @dessert if you can, add alternative script which escape " symbols in lines. – mature Nov 17 '18 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.