Random text in linux terminal - linux

Sup guys. How I can make terminal to show the text that I want ? And how to edit the text that is already displayed
For example terminal is showing now:
user#host: sudo writetext
bash: writetext: command not found
How to edit this text to be displayed like this
user#host: sudo writetext 5
writelext line 1 executed
writelext line 2 executed
writelext line 3 executed
writelext line 4 executed
writelext line 5 executed
I don't need the program to work, i just need to know how to display random text in terminal

You can add an alias to the bashrc
vim ~/.bashrc
Go to the end of the file
add line: alias writeText='echo "write text executed"'
Then reload the bashrc with: source ~/.bashrc
After this you should be able to call the alias by typing in writeText
Here you can also add a more advanced echo function.
If you want to pass parameters you have to write a separate function as described here:
Passing argument to alias in bash

Write a shell script and add echo commands inside to display whatever u want to display

There are many ways to print text to stdout, you should read some man pages:
man echo
man print
man printf
more powerful tools:
sed, awk ...
Examples:
seq
kent$ seq -f "whatever %g" 5
whatever 1
whatever 2
whatever 3
whatever 4
whatever 5
awk
kent$ awk -v v=5 'BEGIN{for(i=1;i<=v;i++)print "whatever "i}'
whatever 1
whatever 2
whatever 3
whatever 4
whatever 5

If you're trying "make the terminal display text that I will type."
You could try read, assign a variable to the read and then echo it
read text
echo "${text}"

Related

Linux Bash Trying to write checklist progress "bar" of sorts. Replace the same line

Sorry if I am not giving you enough info, this is my first time posting here.
I am trying to make this in a bash script.
Downloading...............
"run bash commands and when they are done, replace the "Downloading..." text with the text bellow in the same line aka space."
Downloading............... DONE!
go to next line and show
Installing................
"run bash commands again and when they are done, replace the "Installing..." text with the text bellow in the same line aka space."
Installing................ DONE!
I hope you get what I mean. Thanks in advance.
I've tried:
#/bin/bash
tput sc # save cursor
printf "Something that I made up for this string"
sleep 1
tput rc;tput el # rc = restore cursor, el = erase to end of line
printf "Another message for testing"
sleep 1
tput rc;tput el
printf "Yet another one"
sleep 1
tput rc;tput el
But it doesn't make new lines, it just uses one line to show all text.
I'm assuming you pulled the tput code from somewhere, and I'm guessing that 'somewhere' also explained that tput is being used to overwrite the same line multiple times (as your script actually does).
From your description it doesn't sound like you need to overwrite any lines so using tput is the wrong solution.
If I understand your description correctly you should be able to do everything you want with some (relatively) simple printf commands, eg:
printf "Downloading .... " # no '\n' in the output so cursor remains at end of current line
# run your bash commands here
printf "DONE!\n" # append to end of current line and then add a new line (\n)
printf "Installing .... " # no '\n' in the output so cursor remains at end of current line
# run more bash commands here
printf "DONE!\n" # append to end of the current line and then add a new line (\n)
Keep in mind that if any of your 'bash commands' generate any output then the cursor will be moved (probably to a new line) thus messing up your output. Net result is that you'll need to make sure your 'bash commands' do not generate any output to stdout/stderr (alternatively, make sure all output - stdout/stderr - is redirected to files).
If your requirement is to have the 'bash commands' send output to the terminal then you may need to go back to using tput ... but that's going to depend on exactly how you want the output to appear.
NOTE: If this (above) does not meet your requirement then please update the question with more details.

How to create text file in a directory and add text to it in one command from terminal

I'm doing an assignment on terminal commands in Ubuntu. The problem I'm currently stuck on asks me to create a text file in a directory I'm not currently in, and add text to it, all using one command. I was trying to run it as:
touch /home/user/Desktop/index.html
echo "text" > index.html
...
but keep getting errors.
You also need to specify the path when writing "text" into the file:
touch /home/user/Desktop/index.html ; echo "text" > /home/user/Desktop/index.html
Also, there's no need to touch the file first. The > operator will automatically create the file if it doesn't exist, so you can just type:
echo "text" > /home/user/Desktop/index.html
cat > /the/directory/your_file
hello world!
foo bar
baz
^D
comments:
the part between cat... and ^D is text you enter.
^D (control-D) is an End-of-File marker you type to tell the program, cat, that that is the end of the file you just created, your_file.
If you now do cat /the/directory/your_file, (NOTE: no redirection operator '>' here!), you will see the contents of the file you just created.
Be sure to type only one ^D (control-D); if you hit it twice you will find yourself logged out from the terminal...; the second ^D went to your terminal and told it 'End-of-File', which to it means, bye bye, aka. 'exit'.
Also you can use printf:
printf 'Hello\nworld' > /home/user/Desktop/index.html

Tmux doesn't show line breaks when calling `shell` to display output from Bash command

Given the following text file "HelloWorld.txt"
Hello World
~~~line break~~~
This is a text file
In .tmux.conf, I config the following setup:
bind F1 shell "cat HelloWorld.txt"
When I use this shortcut, Tmux prints the following:
Hello World
This is a text file
That line break just disappears mysteriously.
How can I preserve line breaks?
I couldn't find a bug report but this seems to be how tmux' run-shell command behaves. The workaround I found is to pipe the output via sed to replace each blank line with a space.
Your example would then become like this:
bind F1 run-shell "cat HelloWorld.txt | sed 's/^$/ /'"

How to show line number when executing bash script

I have a test script which has a lot of commands and will generate lots of output, I use set -x or set -v and set -e, so the script would stop when error occurs. However, it's still rather difficult for me to locate which line did the execution stop in order to locate the problem.
Is there a method which can output the line number of the script before each line is executed?
Or output the line number before the command exhibition generated by set -x?
Or any method which can deal with my script line location problem would be a great help.
Thanks.
You mention that you're already using -x. The variable PS4 denotes the value is the prompt printed before the command line is echoed when the -x option is set and defaults to : followed by space.
You can change PS4 to emit the LINENO (The line number in the script or shell function currently executing).
For example, if your script reads:
$ cat script
foo=10
echo ${foo}
echo $((2 + 2))
Executing it thus would print line numbers:
$ PS4='Line ${LINENO}: ' bash -x script
Line 1: foo=10
Line 2: echo 10
10
Line 3: echo 4
4
http://wiki.bash-hackers.org/scripting/debuggingtips gives the ultimate PS4 that would output everything you will possibly need for tracing:
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
In Bash, $LINENO contains the line number where the script currently executing.
If you need to know the line number where the function was called, try $BASH_LINENO. Note that this variable is an array.
For example:
#!/bin/bash
function log() {
echo "LINENO: ${LINENO}"
echo "BASH_LINENO: ${BASH_LINENO[*]}"
}
function foo() {
log "$#"
}
foo "$#"
See here for details of Bash variables.
PS4 with value $LINENO is what you need,
E.g. Following script (myScript.sh):
#!/bin/bash -xv
PS4='${LINENO}: '
echo "Hello"
echo "World"
Output would be:
./myScript.sh
+echo Hello
3 : Hello
+echo World
4 : World
Workaround for shells without LINENO
In a fairly sophisticated script I wouldn't like to see all line numbers; rather I would like to be in control of the output.
Define a function
echo_line_no () {
grep -n "$1" $0 | sed "s/echo_line_no//"
# grep the line(s) containing input $1 with line numbers
# replace the function name with nothing
} # echo_line_no
Use it with quotes like
echo_line_no "this is a simple comment with a line number"
Output is
16 "this is a simple comment with a line number"
if the number of this line in the source file is 16.
This basically answers the question How to show line number when executing bash script for users of ash or other shells without LINENO.
Anything more to add?
Sure. Why do you need this? How do you work with this? What can you do with this? Is this simple approach really sufficient or useful? Why do you want to tinker with this at all?
Want to know more? Read reflections on debugging
Simple (but powerful) solution: Place echo around the code you think that causes the problem and move the echo line by line until the messages does not appear anymore on screen - because the script has stop because of an error before.
Even more powerful solution: Install bashdb the bash debugger and debug the script line by line
If you're using $LINENO within a function, it will cache the first occurrence. Instead use ${BASH_LINENO[0]}

Echo text that is user-editable

Is it possible to output text to a shell window, via bash script, that is user-editable? I essentially want to pre-fill certain information and give the user the ability to edit it if it's wrong.
For instance, if I were to write in a script:
echo -n "Enter your name: Anthony"
while read user_input
do
# do stuff with $user_input
done
How can I allow the user to inline edit the word Anthony only (aka, don't allow backspacing past the A in Anthony), and how can I store the value into a variable once the RETURN key is pressed?
EDIT
I'm looking for something similar to the -i option of read (see answer posted here), but this is only available on bash 4+. Is there an alternative for bash 3?
I needed similar setup recently so what I did was
$ cat a.sh
function input {
python -c '
import sys,readline
readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]))
sys.stderr.write(raw_input(sys.argv[1]))
' "$#" 3>&1 1>&2 2>&3
}
A=$( input 'question: ' default )
echo "A='$A'"
$ ./a.sh
question: default
A='default'
Well, it's not actually bash, but it made the job done.

Resources