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

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

Related

How do you append a string built with interpolation of vars and STDIN to a file?

Can someone fix this for me.
It should copy a version log file to backup after moving to a repo directory
Then it automatically appends line given as input to the log file with some formatting.
That's it.
Assume existence of log file and test directory.
#!/bin/bash
cd ~/Git/test
cp versionlog.MD .versionlog.MD.old
LOGDATE="$(date --utc +%m-%d-%Y)"
read -p "MSG > " VHMSG |
VHENTRY="- **${LOGDATE}** | ${VHMSG}"
cat ${VHENTRY} >> versionlog.MD
shell output
virufac#box:~/Git/test$ ~/.logvh.sh
MSG > testing script
EOF
EOL]
EOL
e
E
CTRL + C to get out of stuck in reading lines of input
virufac#box:~/Git/test$ cat versionlog.MD
directly outputs the markdown
# Version Log
## version 0.0.1 established 01-22-2020
*Working Towards Working Mission 1 Demo in 0.1 *
- **01-22-2020** | discovered faker.Faker and deprecated old namelessgen
EOF
EOL]
EOL
e
E
I finally got it to save the damned input lines to the file instead of just echoing the command I wanted to enter on the screen and not executing it. But... why isn't it adding the lines built from the VHENTRY variable... and why doesn't it stop reading after one line sometimes and this time not. You could see I was trying to do something to tell it to stop reading the input.
After some realizing a thing I had done in the script was by accident... I tried to fix it and saw that the | at the end of the read command was seemingly the only reason the script did any of what it did save to the file in the first place.
I would have done this in python3 if I had know this script wouldn't be the simplest thing I had ever done. Now I just have to know how you do it after all the time spent on it so that I can remember never to think a shell script will save time again.
Use printf to write a string to a file. cat tries to read from a file named in the argument list. And when the argument is - it means to read from standard input until EOF. So your script is hanging because it's waiting for you to type all the input.
Don't put quotes around the path when it starts with ~, as the quotes make it a literal instead of expanding to the home directory.
Get rid of | at the end of the read line. read doesn't write anything to stdout, so there's nothing to pipe to the following command.
There isn't really any need for the VHENTRY variable, you can do that formatting in the printf argument.
#!/bin/bash
cd ~/Git/test
cp versionlog.MD .versionlog.MD.old
LOGDATE="$(date --utc +%m-%d-%Y)"
read -p "MSG > " VHMSG
printf -- '- **%s** | %s\n' "${LOGDATE}" "$VHMSG" >> versionlog.MD

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/^$/ /'"

Random text in linux terminal

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}"

Replace variable in bash file using sed - output lags

I'm trying to write a bash script that search and replace a specific
user input saved in config.sh using sed. This does work; however it
only works partially as shown below.
config.sh
#!/bin/bash
#
#UserName to be deleted
delUserName=""
#Source
delUserSrc=/Users/"$delUserName"
#Destination
delUserDest=/Users/John/BackUp/"$delUserName"/"$delUserName".zip
main.sh
#!/bin/bash
#
source scripts/config.sh
echo -e "\nEnter user you wish to delete: \c"
read -r UserName
sed -i '' -e "s/delUserName=.*/delUserName=$UserName/g" scripts/config.sh
echo -e "delUserName: $delUserName"
echo -e "delUserSrc: $delUserSrc"
echo -e "delUserDest: $delUserDest"
output1:
Enter user you wish to delete: Test
delUserName:
delUserSrc:/Users/
delUserDest:/Users/John/BackUp/ / .zip
output2:
Enter user you wish to delete: Test1
delUserName:Test
delUserSrc:/Users/Test
delUserDest:/Users/John/BackUp/Test/Test.zip
output3:
Enter user you wish to delete: Test1
delUserName:Test1
delUserSrc:/Users/Test1
delUserDest:/Users/John/BackUp/Test1/Test1.zip
expected output1:
Enter user you wish to delete: Test
delUserName:Test
delUserSrc:/Users/Test
delUserDest:/Users/John/BackUp/Test/Test.zip
expected output2:
Enter user you wish to delete: Test1
delUserName:Test1
delUserSrc:/Users/Test1
delUserDest:/Users/John/BackUp/Test1/Test1.zip
The script lags. sed instantaneously changed the value for $delUserName BUT The proper values for $delUserName, $delUserSrc, and $delUserDest only echo on the 2nd run. The scripts run well when all variables are in main.sh except i have to do it this way. Save the user input into $UserName. Any idea why the values don't show when run the 1st time?
Thanks
Here is what I think is happening.
The sed command replaces text in files. It does not modify the value of variables in memory. These values are assigned when you source config.sh.
So right after your sed line, you need to put this line :
source scripts/config.sh
It is the same line as above in your script. This is required there also so that your newly replaced values will be loaded in the variables so that you can display them. Once the new values are loaded in memory, then the echo statements will be able to expand the variables to that new value.

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