write to a file with a specific font size - bash

I want to write to a file in bash but I wan to use a specific font size. For example, I want to write to the file hello but with a font size of 30.
echo "Hello "
Also, is there another way to indent in bash when writing to a file besides using spaces like below?
echo " Hello"

As #thatotherguy commented, details like fonts are determined by the program reading the file, so it depends on what sort of file you're creating, and you'll typically use different tools to create different types of files. echo and other shell commands just work with text; the literal characters h, e, l, l, and o are sent to the terminal, no size or font data goes along with it.
If you're trying to simply make big font in your terminal there are tricks, like ASCII art text using figlet:
$ figlet "Hello"
_ _ _ _
| | | | ___| | | ___
| |_| |/ _ \ | |/ _ \
| _ | __/ | | (_) |
|_| |_|\___|_|_|\___/
And since you asked about indentation, notice that figlet supports centered text and other sorts of formatting:
figlet -c "Hello"
_ _ _ _
| | | | ___| | | ___
| |_| |/ _ \ | |/ _ \
| _ | __/ | | (_) |
|_| |_|\___|_|_|\___/
It is possible to make some changes to the text that gets displayed in your terminal (you've probably seen colored text before from some commands), but not fonts. You can use tput to modify the text you output, e.g. with colors or bold, but not size or font (which are configured by your terminal itself). Some examples of that in this question, but tput is easier than figuring out all the \e... escape sequences they're talking about.
# it looks plain here, but if you run this in your shell it ought to be underlined
$ echo "$(tput smul)hello$(tput rmul)"
hello
If ASCII art or the color/font features most terminals support isn't what you're looking for, you'll need to share more details about what you're trying to do.

Related

Julia cannot reproduce the rand

I am just reading and practicing the "3.5.2.1 rand" section of https://juliadatascience.io/standardlibrary and found the code below cannot reproduce same random numbers:
$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> using Random: rand, randn, seed!
julia> my_seed = seed!(123)
Random.TaskLocalRNG()
julia> rand(my_seed, 3)
3-element Vector{Float64}:
0.521213795535383
0.5868067574533484
0.8908786980927811
julia> rand(my_seed, 3)
3-element Vector{Float64}:
0.19090669902576285
0.5256623915420473
0.3905882754313441
The snapshot of the book:
Well spotted. Running rand(my_seed, 3) twice shouldn't give the same output. This problem was caused by a bug in Books.jl. The Books package stores outputs in files as a sort of caching mechanism. Unfortunately, two identical blocks of code will be written to the same file path, so the output for the last block overrides the output for the first block. I still haven't found a nice solution to that problem and normally it isn't a problem because the same code will usually give the same output, but not for calling rand.
This problem will be fixed in the HTML and PDF version on the website about 30 minutes after https://github.com/JuliaDataScience/JuliaDataScience/pull/235 is merged.
You're missing the second call to seed! that's in the book.

pass a variable into a ascii art

In bash/ Ubuntu,
If there is a ASCII art file: "ascii-art" in the following
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client ${CLIENT_ID}
Is there anyway to pass the variable "${CLIENT_ID}" into the ascii-art every time we call it?
The way we call it at the moment:
cat ascii-art
The following ways don't work
1. cat ascii-art | CLIENT_ID="1"
or
Add one more line in the first line of the file "ascii-art"
. command_line_parse.sh -c ${CLIENT_ID}
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client ${CLIENT_ID}
Then
cat ascii-art | CLIENT_ID="1"
Could any guru enlighten? Thanks.
The envsubst tool is written for exactly this purpose:
CLIENT_ID=foo envsubst ascii-art
This is a preferable tool to sed, which restricts the range of possible values (if you had a / in your id, or even worse a semicolon followed by another sed command, serious bugs could ensue).
See also TemplateFiles on the Wooledge wiki, which includes a native-bash implementation for systems without GNU gettext (which includes envsubst).
The following evaluates the echo command for the contents of the file:
CLIENT_ID=1000 eval "$(cat <<EOC
echo -e "$(<ascii-art)"
EOC
)"
CLIENT_ID=1000 assigns environment variable for the eval command
eval accepts a here document as its single argument
$(<ascii-art), in Bash, does the same as $(cat ascii-art)
EDIT regarding the scary eval
It's true that we should avoid using eval. But we should also understand the
purpose of the command and do use it when appropriate. We should understand
the security risks, and decide whether we should, or we shouldn't use eval
in certain situations.
The Bash Hackers Wiki gives a good description
of eval:
Perhaps the easiest way to think about eval is that it works in the same way
as running bash -c "bash code…" from a script, except in the case of
eval, the given code is executed in the current shell environment rather
than a child process.
So eval just executes the shell code we pass. How often we execute
external commands from Bash scripts? I guess, quite often. And the commands
are just trusted executables which may well be Bash scripts themselves.
Then why should one be scared of evaluating some echo "trusted content"?
It is up to the user(OP) to decide whether it is safe to use eval in certain
situation. However, this answer definitely gives him an option; it is an
alternate solution. So I don't understand the downvote on this answer.
First: look at the programs figlet, toilet or cowasy. These are probably finished implementation of what you want.
If you want to write it yourself:
sed 's/${CLIENT_ID}/42/g' ascii-art
Modified from RotatingPieces' answer above
First modify the "ascii-art" file, and change "${CLIENT_ID}" into "CLIENT_ID_ART"
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client CLIENT_ID_ART
Now the following approach might not be very elegant, but it works perfectly
CLIENT_ID="1" # The ${CLIENT_ID} will actually be from the command line parser
echo "sed 's/CLIENT_ID_ART/"${CLIENT_ID}"/g' ascii-art > call_art
chmod +x call_art
./call_art &
rm -f call_art
The above will print out the following in the output
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client 1

Echo in Bash: Escape Apostrophes?

I'm trying to use echo in bash inside of quotes.
When I try from a command line, it works fine.
For example: echo "I'm testing the apostrophe functionality."
yields I'm testing the apostrophe functionality.
Yet, when I write this in a script, it doesn't seem to work.
Here's a snippet of my code: (I'm trying to integrate ASCII art into my program)
if [ "$2" == "-s" ]
then echo " ___ __ _ _ "
echo " / _ \__ _ _ __ ___ ___ / _\ |_ __ _ _ __| |_ ___ _ __ "
echo " / /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|"
echo "/ /_\\ (_| | | | | | | __/_____|\ \ || (_| | | | || __/ | "
echo "\____/\__,_|_| |_| |_|\___| \__/\__\__,_|_| \__\___|_| "
echo ""
echo "Hello! My name is Siri."
echo "I'm not actually the Siri you're probably used to."
echo "I'm actually Apple's Siri's sister, the no-voice one."
echo "Sorry, but I'm in development right now."
echo "Come back later and maybe Eric will bring me out of beta."
echo "Thanks for reading this long debug message!"
fi
I've checked and double-checked all my quotes...
Yet it still yields:
./game-starter.sh: line 7: unexpected EOF while looking for matching ``'
./game-starter.sh: line 88: syntax error: unexpected end of file
Please help soon!
-HewwoCraziness
As you're using double quotes around your strings, certain characters are interpreted by the shell. One example is the backtick, as mentioned in Ryan's answer.
One option would be to use single quotes around your strings, although then you would have to escape the apostrophes in your message. I think that the best solution would be to use a heredoc instead:
cat <<'EOF'
___ __ _ _
/ _ \__ _ _ __ ___ ___ / _\ |_ __ _ _ __| |_ ___ _ __
/ /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|
/ /_\\ (_| | | | | | | __/_____|\ \ || (_| | | | || __/ |
\____/\__,_|_| |_| |_|\___| \__/\__\__,_|_| \__\___|_|
Hello! My name is Siri.
I'm not actually the Siri you're probably used to.
I'm actually Apple's Siri's sister, the no-voice one.
Sorry, but I'm in development right now.
Come back later and maybe Eric will bring me out of beta.
Thanks for reading this long debug message!
EOF
The quotes around the EOF mean that the string is interpreted literally, so characters such as | don't cause problems.
I don't think it's the apostrophes that are causing your issue; is it the ` character (you know, on the ~ key). It is used for running commands in place and other things, and is probably what's causing the issue, if I had to guess based on that error message.

Ruby variable from an external shell command

I’m using a gem called artii ( http://rubygems.org/gems/artii ) that creates ascii art images from text.
I can only seem to call it using system(), however I’d like to display the result as text in a webpage
My .rb file:
def makeText
#word = system('artii Hello World')
puts #word
#word
end
result of puts:
=> _ _ _ _
=> | | | | | | |
=> | |__| | ___| | | ___
=> | __ |/ _ \ | |/ _ \
=> | | | | __/ | | (_) |
=> |_| |_|\___|_|_|\___/
Then, in my haml file:
#{makeText}
=> true
Is there a way to take the result from the command line and convert it to a string, array, or hash to display in a webpage?
Thanks!
It seems ridiculous to me to call the gem as external command, either using system or backticks. You can use it from Ruby as a Ruby library, without any system interaction. The simplest invocation would be:
#word = Artii::Base.asciify('Hello World')
If you want more complex invocation (i.e. different fonts, styles, etc), then check out that gem's documentation.
You want to use backticks rather than the system method. Just enclose your shell command in backticks, and the return value will be a string containing whatever it output to standard out.
#word = `artii Hello World`
Note: Be careful not to pass user input to the shell without sanitizing it first, to prevent malicious users from executing arbitrary shell commands. As long as you're the one supplying the string in backticks, and not the user, you're fine.

How can I add color between \/ in terminal?

I am making text ASCII art for my .profile in terminal, and trying to colorize it. At first I as going to use the cat command and heredoc for printing out my art, but then I couldn't get the colors inside of the heredoc to work. So I went with the dirty fix, I am using echo -e for each line and then coloring it. If there's a better way, please let me know! Right now, I am having this problem.
Full picture:
_ _
__| |_ __ __ _| |__
/ _` | ' \/ _` | / /
\__,_|_|_|_\__,_|_\_\
Part that I am coloring:
/ _` | ' \/ _` | / /
Coloring:
echo -e "\033[37m/ _\` |\033[36m ' \\\033[1;35m/ _\` | / /";
Outputs:
/ _` | ' \033[1;35m/ _` | / /
As you can see, I am trying to insert a new color in between the \/. The \ is treating the \033[1;35m literally. Is there a way to color the change the color between the \/ without altering the image?
Also, I am using Mac OSX Lion.
Instead of a heredoc you may use the $'string' feature of Bash which makes it possible to directly use ANSI C escape sequences for colouring output.
man bash | less -p "\\$'string'"
(
asciiart=$'
_ _
__| |_ __ __ _| |__
\033[37m/ _` |\033[36m \' \\\033[1;35m/ _` | / /\033[m
\\__,_|_|_|_\\__,_|_\\_\\
'
echo "$asciiart" | sed '1d;$d'
)
To increase readability you may want to try figlet.
http://rudix.org/packages-def.html#figlet
Try with 5 bars instead of 3 \\\\\033[1;35m/
As for why, bash escape \\\\ to \\ then echo -e, escape it again to \. If you enable set -x (trace mode) you will see the command executed after bash processing (set +x to disable it).
What about simply using a few lines of POSIX printf
printf "\e[37m/ _\` |\e[36m \....\n"
instead of messing with all the pesky escape problems?

Resources