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.

1 Answers1

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"
dessert
  • 40,956