-1

The goal is to use the bash function inside the cat lines that creates an html file.

Must be easy knowing the way, but this does not "compile" while writing at Sublime:

#!/usr/bin/env bash

function create_file_with_random_numbers {

    file="/User/example/folder/random_numbers.html"

    bash -c cat <<EOF >${file}

    Your random numbers are: "[ { $(( RANDOM % 70 ))  } and { $(( RANDOM % 70 ))  } ]"

EOF

}

I tried to scape the cat file with ", [, {, etc., is it possible?

Thanks

Dieglock
  • 169
  • 1
  • 16
  • 3
    The code, as presented, should work (although it's strange and not generate a valid html). Instead of `bash -c cat`, just `cat`. What exactly is your question? What exactly "is possible"? bash does not "compile" scripts, scripts are parsed and executed, there is no "compilation" involving. – KamilCuk Jan 22 '20 at 02:07
  • Is it possible to call the random function inside the cat lines? – Dieglock Jan 22 '20 at 02:13
  • 3
    Did you try it? Did you try to answer the question on your own? There are no "cat lines", there is no such term. And yes, the arithmetic expansion is expanded inside a here document. Take a look at [bash manual here document](https://www.gnu.org/software/bash/manual/bash.html#Here-Documents) if "EOF" `is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion .....` The `$(( ))` is a arithmetic expansion. – KamilCuk Jan 22 '20 at 02:21
  • You are right. The script works. My bad. Thanks. – Dieglock Jan 22 '20 at 02:22
  • 1
    Calm down. Someone comes here to learn. If I do not express my self properly, please have in count English is a second language to me. Thanks. – Dieglock Jan 22 '20 at 02:27

1 Answers1

0

You can use printf:

printf 'Your random numbers are: "[ { %d } and { %d } ]"\n' "$(( RANDOM % 70 ))" "$(( RANDOM % 70 ))" > "$file"
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134