echo not printing to file - linux

i have a large list of telephone numbers I need to delete from a database, Im adding the telephone numbers to a single file on newlines and using the following script to generate the SQL insert command for me to manually paste.
file="Input.txt"
while IFS= read line
do
echo "delete from usr_preferences where uuid like '$line';"; >> output.txt
done <"$file"
Input file data -
1111111111
2222222222
3333333333
4444444444
It's working as expected other than it prints in the terminal rather than printing to file output.txt
What have I missed?
Thanks

Thanks to Andrey B. Panfilov and markp-fuso
Removing ; before the output command worked.

Related

BASH - Is there a way to save output of a command to a file with the source formatting?

So to put it less confusing:
I run a command which prints some formatted values in the bash e.g.:
NodeID (lot of whitespace) Heap_size (again) Time
And when i try to save the output with Name:~$ script > file.txt, the output is:
ESC[93mnode_s1aESC[0m^MESC[25C1.0g
Expected output:
node_s1a 1.0g ...
node_s2aaaaa 2.0g ...
Is there a way to save raw output with the formatting into a text file ?
You can use the printf command which is like the printf function in C or Java.
printf "%-20s%s" Name:~$ script >> test.txt
I'm assuming Name:~$ and script are variables because I've never seen them before.

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

Deleting text in a file with "sed" isn't working as expected

I am currently working on a little script for the "nslookup"-command and in my testing I encountered a problem I don't understand. In my script a .txt file is automatically created and the user can input some text to it if he wishes to. He can also delete specific lines in the document. I tried writing it with "sed" but it doesn't seem to be working correctly.
Here the menu from the terminal output:
Domains:
1) new_domain
2) domain
3) Create new Domain
4) Delete a Domain
5) Quit
Input>
The first two numbers also representing the line of each text.
The code for deleting a domain is the following:
filename=domains.txt
old_filename=domains_backup.txt
read -p "Which domain-number shall be deleted?: " num_input
mv $filename $old_filename
sed "/$num_input/d" < $old_filename > $filename
rm $old_filename
But when executing that script and the user wants to delete line 2 (domain) the text-file remains the same and is not updated.
When I try the same only using the terminal everything works fine.
Is there something I'm missing?
To delete a line by its line number you will want to use $num_input d rather than /$num_input/d : the second one matches lines that contain $num_input.
As a side note, if you use GNU sed you could let it handle the backup :
sed -i.backup "$num_input d" domains.txt
This would create a copy of the untouched domains.txt as domains.txt.backup (or whatever suffix you specify after -i) and update the domains.txt file.

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.

Assign new variable from each line of a text file

What I'm basically trying to do is automatically detect if there is text in a line, and if so create a new variable containing the text in said line , within a script. If there is no text in a line then the variable doesn't get created. I can do this manually by opening the file -
$ cat file.txt
sometxt
somemoretext
evenmoretext
...
then adding to my script the appropriate lines -
TXT=file.txt
VAR1=$(sed -n 1p $TXT)
VAR2=$(sed -n 2p $TXT)
...
but this is a pain since I have to count how many lines there are total, then copy and paste each line assigning the variables and changing 'VAR!' to 'VAR2' and '1p' to '2p'. There has to be an easier way. Thanks
#JNevil thanks for pointing me in the right direction.
Heres what ended up working for me -
for var_name in (cat links.txt); do
wget <servername.com>$var_name
done
Still dont know how to use curl but this worked fine!

Resources