Bash script doesn't read entire line - bash

First off, I am in the early stages of learning bash shell scripting, so I apologize if I say / do anything that doesn't make sense.
Currently, I'm trying to have an SBC, a Khadas VIM3 specifically, run a python script to find and label faces in any given video from a local server. Currently, I need to reduce the frame rate and resolution of the video, which is where the bash script comes into play. I need to automate this process and thought I'd do it using a bash script and crontab.
The file paths are found and output into a file from a separate script, and are read by the bash script. The problem comes when I try and call ffmpeg to use the file paths.
The Code :
pathFile="/home/khadas/Documents/paths"
while IFS= read -r line
do
ffmpeg -i "$line" -vf scale=960:540 -y "$line"
cp "$line" ./
done < $pathFile
The resulting error :
: No such file or directoryalRecognition/10/14-53.h264+/2019-09-26-10-14-53.mp4
cp: cannot stat '/home/khadas/Downloads/FacialRecognition/10/14-53.h264+/2019-09-26-10-14-53.mp4'$'\r': No such file or directory
Example of the paths file (There will be hundreds of entries) :
/home/khadas/Downloads/FacialRecognition/10/14-42.h264+/2019-09-26-10-14-42.mp4
/home/khadas/Downloads/FacialRecognition/10/59-06.h264+/2019-09-26-10-59-06.mp4
/home/khadas/Downloads/FacialRecognition/10/36-28.h264+/2019-09-26-10-36-28.mp4
/home/khadas/Downloads/FacialRecognition/10/14-53.h264+/2019-09-26-10-14-53.mp4
When using a trimmed down version, the script works as expected. Could it be an issue with the length of the lines? Any help is much appreciated.

According to the error message, cp is attempting to access a file whose name ends with .mp4'$'\r'. That appears to means that somewhere there are DOS/Windows-style line-endings. If you have the dos2unix utility, run it against your files including /home/khadas/Documents/paths.
-- John1024

Related

How to split a real-time stdout stream into several files?

I have a python script which is continuously writing a text stream to stdout.
Something like this (genstream.py):
while 1:
print (int(time.time()))
time.sleep(1)
I want a bash script which launch the python script, save its output to a set of files, let's say to split the output every hour to avoid the creation of a huge file which is difficult to manage.
The so created files will be then processed (i.e. one at the end of each hour) by the same bash script to insert the values into a database and moved to an archive folder.
I did my search in google/stack overflow (e.g. split STDIN to multiple files (and compress them if possible) Bash reading STDOUT stream in real-time or https://unix.stackexchange.com/questions/26175/ ) but I didn't find any solution so far.
I've tried to use also something easy like this (so without taking in account the time but only the number of lines)
python3 ./genstream.py | split -l5 -
but I have no output.
I've tried a combination of (named-)pipes and tee but nothing seems to work.
Try this:
python3 ./genstream.py | while read line; do
echo "$line" >> split_$(date +%Y-%m-%d-%H)
done

Run curl command on each line of a file and download the files

I have a file containing a list of pdf files.
this is a test.pdf
this is a/another test.pdf
test number 5_ example.pdf
...
I know that doing:
curl -O "https://report2018/this is another test.pdf"
will download the file.
So the scenario is use curl in bash script to download all the pdf's in the file one by one when the beginning of the URL should be: "https://report2018/" .
So a complete URL will be https://report2018/+PDF_NAME
Any ideas or suggestions how to do it in bash script?
It is a pretty basic script actually...
You can break your problem in two pieces:
Basic usage of bash, in general;
How to cycle a file.
Something like this will suffice:
#!/bin/bash
exec 3<file.list # Put the file in a file descriptor
while read -r line <&3; do # Read line by line the file descriptor
curl -sk "https://report2018/${line}" > "${line}" # make curl and save in file
done
exec 3>&- # Close file descriptor
Obviously you have to change the curl with your needs (E.G. User Agent and/or authentication).
Note that with > ${line}" after curl, you will save the file with the same name read by file.list.
I hope this helps you, and please, next time, use a search engine first.

What is mean that `grep -m 1 ` command in UNIX

I googled this command but there was not.
grep -m 1 "\[{" xxx.txt > xxx.txt
However I typed this command, error didn't occured.
Actually, there was not also result of this command.
Please anyone explain me this command's working?
This command reads from and writes to the same file, but not in a left-to-right fashion. In fact > xxx.txt runs first, emptying the file before the grep command starts reading it. Therefore there is no output. You can fix this by storing the result in a temporary file and then renaming that file to the original name.
PS: Some commands, like sed, have an output file option which works around this issue by not relying on shell redirects.

Bash script not copying files

I have a bash script which is pretty simple (or so I thought - but I don't write them very often):
cp -f /mnt/storage/vhosts/domain1.COM/private/auditbaseline.php /mnt/storage/vhosts/domain1.COM/httpdocs/modules/mod_monitor/tmpl/audit.php
cp -f /mnt/storage/vhosts/domain1.COM/private/auditbaseline.php /mnt/storage/vhosts/domain2.org/httpdocs/modules/mod_monitor/tmpl/audit.php
The script copies the contents of auditbaseline to both domain 1 and domain 2.
For some reason it won't work. When I have the first line in on its own it's okay but when I add the second line I can't get it to work it locks up the scripts and they can't be accessed.
Any help would be really appreciated.
Did you perhaps create this script on a Windows machine? You should make sure that there are no CRLF line breaks in the file. Try using dos2unix (http://www.linuxcommand.org/man_pages/dos2unix1.html) to convert the file in that case.

Deleting files from a list using shell

I am a beginner with Applescript & Shell and am writing a script that at a certain point requires me to delete files that are listed within a .txt file. I have searched extensively on stackoverflow and was able to come up with the following command that I am running from within my Applescript...
do shell script "while read name; do
rm -r \"$name"\
done < ~Documents/Script\\ Test/filelist.txt"
It seems to recognize and read the file, but I get an error that says this and I cannot figure out why:
error "rm: ~/Documents/Script Test/filetodelete.rtf: No such file or directory" number 1
That said, I can navigate to that exact directory and verify that a file by that name with that extension does indeed exist. Can someone help shed some light on why this error might be occurring?
You have a typo. The path to the file is most probably ~/Documents, not ~Documents (which in Bash would be the home directory of a user whose account name is Documents).
If your shell is not Bash, it might not even support ~ for $HOME.
In the data file, you also cannot use ~ to refer to your home directory. You could augment the loop with a simple substitution to support this:
while read -r file; do
case $file in '~'*) file=$HOME${file#\~};; esac
rm -r "$file"
done < ~/"Documents/Script Test/filelist.txt"
Notice also the use of read -r to avoid some pesky problems with the legacy default behavior of read.

Resources