Score:2

how to execute a string block from a file

tn flag

I have the following command saved in mat.txt file:

printf "
       _       ____            __ _
 _ __ (_)_  __/ ___|_ __ __ _ / _| |_
| '_ \| \ \/ / |   | '__/ _` | |_| __|
| | | | |>  <| |___| | | (_| |  _| |_
|_| |_|_/_/\_\\____|_|  \__,_|_|  \__|


"

when I execute this file after made it executable using:

chmod +x mat.txt

It gives me an Error:

enter image description here

It's saying like command not found, file end reached when searching for ' & syntaxerror.

Anyone knows why?

marcelm avatar
cn flag
Please don't post screenshots of text. You can copy/paste the error as a code block here. This makes the content of the error searchable, and is better for people with visual impairments.
Score:5
hr flag

From man bash:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, , and, when history expansion is enabled, !.

In other words, " ... " is not sufficient to protect the unbalanced backtick on line 4 of your text; the shell is interpreting it as the start of a command substitution.

OTOH you can't use single quotes, because your text contains single quotes.

I'd suggest avoiding the issue of quoting altogether by using a here document. You should also use a shebang to make sure your file is interpreted by the intended shell. So:

#!/bin/sh

cat <<'TXT'
       _       ____            __ _
 _ __ (_)_  __/ ___|_ __ __ _ / _| |_
| '_ \| \ \/ / |   | '__/ _` | |_| __|
| | | | |>  <| |___| | | (_| |  _| |_
|_| |_|_/_/\_\\____|_|  \__,_|_|  \__|


TXT

then

$ ./mat.txt
       _       ____            __ _
 _ __ (_)_  __/ ___|_ __ __ _ / _| |_
| '_ \| \ \/ / |   | '__/ _` | |_| __|
| | | | |>  <| |___| | | (_| |  _| |_
|_| |_|_/_/\_\\____|_|  \__,_|_|  \__|
Freddy avatar
cf flag
It looks like the first line needs to be indented by 7 spaces :)
hr flag
@Freddy thanks I hadn't noticed that
Keeran avatar
tn flag
Thank you @steeldriver . That helped.
Score:0
jp flag

You didn't ask for a specific interpreter in your question. Therefore, Python(which allows assigning a multi-line string to a variable using triple quotes be it single ''' ... ''' or double """ ... """) might be an option like so:

#!/bin/python3

a = '''
       _       ____            __ _
 _ __ (_)_  __/ ___|_ __ __ _ / _| |_
| '_ \| \ \/ / |   | '__/ _` | |_| __|
| | | | |>  <| |___| | | (_| |  _| |_
|_| |_|_/_/\_\\____|_|  \__,_|_|  \__|
'''

print(a)

Which executes like so:

$ ./mat.txt 

       _       ____            __ _
 _ __ (_)_  __/ ___|_ __ __ _ / _| |_
| '_ \| \ \/ / |   | '__/ _` | |_| __|
| | | | |>  <| |___| | | (_| |  _| |_
|_| |_|_/_/\_\____|_|  \__,_|_|  \__|
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.