Moving File to Desktop with Bash Script - macos

I am trying to write a bash script that writes content to a file and then moves the file to my desktop. The first chunk of code, before the line break, works well and outputs the necessary content to a file called main_output.txt.
Moving this file is where I run into trouble. I know relative paths are best, but I'm using OSX El Capitan and relative paths in bash are not working as expected. Any ideas would be greatly appreciated!
#/bin/bash
OUTPUT="$(ls -1t ~/Desktop/directory/inputs|head -n 1)"
echo $OUTPUT > ~/Desktop/directory/outputs/main_output.txt
FINALFILE = ~/Desktop/directory/outputs/main_output.txt
DESTINATION = ~/Desktop/
mv $FINALFILE $DESTINATION

There must be no space between around the = sign.
Your OUTPUT is done right, which is why it works. Change your second part to this:
FINALFILE=~/Desktop/directory/outputs/main_output.txt
DESTINATION=~/Desktop/
maybe you'll have to put double quotes around your string.

Related

Bash script doesn't read entire line

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

Change the output directory on command line For Loop

I have been working CSVKIT to build out a solution for parsing fixed-width data files to csv. I have put together the below code to iterate through all the files in a directory, but this same code also places the files back into the same directory it came from. As best practice I believe in using an 'IN' folder and an 'OUT' folder. I am also processing this command-line on a MAC.
for x in $(ls desktop/fixedwidth/*.txt); do x1=${x%%.*}; in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $x1.csv; echo "$x1.csv done."; done
I feel that I am missing something and or that I need to change something within my snippet shown below, but I just can't put my finger on it.
x1=${x%%.*}
Any help on this would be wonderful and I thank you in advance.
When you run,
in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $x1.csv
the output of your programm will be writing to the file, ${x1}.csv.
So, just set correct path to x1:
output_dir=/path/to/your/dir/
output_file=${output_dir}${x%%.*}.csv
in2csv -f fixed -s desktop/ff/ala_schema.csv $x > $output_file;
But, you should create your output_dir, before running this code. Otherwise you can receive an error, that directory doesn't exists.

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

Very simple script to copy a file
#!/bin/bash
#copy file
mtp-getfile "6" test2.jpg
I set it as executable and run it using
sudo sh ./test.sh
It gives me a file called test2.jpg that has no icon and I cannot open I get a 'Failed to open input stream for file' error
However, if I simply issue the following from the command line
mtp-getfile "6" test2.jpg
It works as expected. What is wrong with my script? I checked and the resulting .jpg file in each case has the same number of bytes. Very strange.
As commented by chepner, your file might have a DOS (Windows) invisible line ending on its name, which would cause an error. To get rid of this unwanted character(s), just create a new blank script on your "nix" system and type the name by hand (not by copying and pasting, to avoid problems), let's say, name it test2.sh.
Then copy all the contents of test.sh to test2.sh (copy and paste) and run test2.sh and see if it works. If it doesn't, try running the following code on the new script, to make sure that there are no unwanted characters on the code itself:
tr -d "\r" < /folder/test2.sh && echo >> /folder/test2.sh
And then try to run script2.sh again to see if it works. Note: the echo >> /folder/test2.sh part of the code above is just to make sure that your new script ends with a newline, which is a Posix standard (and without which some programs may misbehave because they expect the file to end with a newline).
Apparently it was a permissions issue.
I only had to do a sudu chown test2.jpg

sed command in build script

I'm new to sed command. I was reading the build script for some source code and I found this for loop in it.
for x in '*.la'
do
sed -i -e 's|^\(libdir=.\).*\(/opt/toolchains\)|\1\2|' x
done
I'm not able to understand the function this for loop is doing. Can anyone help.
It's iterating over a series of files in the current directory ending with ".la" and for each file found, it's editing the contents using sed to convert lines of the form:
libdir=X[zero-or-more-chars]/opt/toolchains
(where X is any character) into lines of the form:
libdir=X/opt/toolchains.
In other words, it's removing the [zero-or-more-chars] part of those lines.
Actually, this looks buggy because I would expect the sed command to reference $x, not x. I have a feeling you lost the $ somehow in the copy/paste step (or perhaps it's simply a bug).

Bash command route malfunction

Given this (among more...):
compile_coffee() {
echo "Compile COFFEESCRIPT files..."
i=0
for folder in ${COFFEE_FOLDER[*]}
do
for file in $folder/*.coffee
do
file_name=$(echo "$file" | awk -F "/" '{print $NF}' | awk -F "." '{print $1}')
file_destination_path=${COFFEE_DESTINATION_FOLDER[${i}]}
file_destination="$file_destination_path/$file_name.js"
if [ -f $file_path ]; then
echo "+ $file -> $file_destination"
$COFFEE_CMD $COFFEE_PARAMS $file > $file_destination #FAIL
#$COFFEE_CMD $COFFEE_PARAMS $file > testfile
fi
done
i=$i+1
done
echo "done!"
compress_javascript
}
And just to clarify, everything except the #FAIL line works flawessly, if I'm doing something wrong just tell me, the problem I have is:
the line executes and does what it have to do, but dont write the file that I put in "file_destination".
if a delete a folder in that route (it's relative to this script, see below), bash throws and error saying that the folder do not exist.
If I make the folder again, no errors, but no file either.
If I change the $file_destination to "testfile", it create the file with correct contents.
The $file_destination path its ok -as you can see, my script echoes it-
if I echo the entire line, copy the exact command with params and execute it onto a shell in the same directory the script is, it
works.
I don't know what is wrong with this, been wondering for two hours...
Script output (real paths):
(alpha)[pyron#vps herobrine]$ ./deploy.sh compile && ls -l database/static/js/
===============================
=== Compile ===
Compile COFFEESCRIPT files...
+ ./database/static/coffee/test.coffee -> ./database/static/js/test.js
done!
Linking static files to django staticfiles folder... done!
total 0
Complete command:
coffee --compile --print ./database/static/coffee/test.coffee > ./database/static/js/test.js
What am I missing?
EDIT I've made some progression through this.
In the shell, If I deactivate the python virtualenv the script works, but If I call deactivate from the script it says command not found.
Assuming destination files have no characters as spaces in their names, directories exist etc. I'd try adding 2>&1 e.g.
$COFFEE_CMD $COFFEE_PARAMS $file > testfile 2>&1
compilers may put desired output and/or compilation messages on stderr instead of stdout. You may also want to put full path to coffee , e.g. /usr/bin/coffee instead of just compiler name.
Found that the problem wasn't the bash script itself. A few lines later the deploy script perform the collectstatic method from django. Noticed that until that line the files were there, I started reading that the collecstatic have a cache system. A very weird one IMO, since I have to delete all the static files and start from scratch to have the script working.
So... the problem wasn't the bash script but the django cache system. Im not givin' reputation to me anyways.
The full deploy script is here: https://github.com/pyronhell/deploy-script-boilerplate and everyone is welcome if you can improve it.
Cheers.

Resources