Script to download, gunzip merge files and gzip the fil again? - bash

My script skills are really limited so I wonder if someone here could help me. I would like to download 2 files, run gunzip, run a command called tv_merge and then gzip the new file. Here's what I Would like to be run from a script.
I would like to download two files (.gz) with wget:
wget -O /some/where/file1.gz http://some.url.com/data/
wget -O /some/where/file2.gz http://some.url.com/data/
Then gunzip the 2 files:
gunzip /some/where/file1.gz
gunzip /some/where/file2.gz
After that run a command called Tv_merge:
tv_merge -i /some/where/file1 -m /some/where/file2 -o newmaster.xml
After tv_merge. I would like to gzip the file:
gzip newmaster.xml
I would like to run all these commands in that order from a script, and I would like to put that to be run let's see every 8h like a crontab.

I'm assuming that file names are static. with provided information this should get you going.
#!/bin/bash
echo "Downloading first file"
wget -O /some/where/file1.gz http://some.url.com/data/
echo "First Download Completed"
echo "Downloading Second file"
wget -O /some/where/file2.gz http://some.url.com/data/
echo "Second Download Completed"
gunzip /some/where/file1.gz
gunzip /some/where/file2.gz
echo "Running tv_merge"
tv_merge -i /some/where/file1 -m /some/where/file2 -o newmaster.xml
gzip -c newmaster.xml > /some/where/newmaster.xml.gz
echo "newmaster.xml.gz is ready at /some/where/newmaster.xml.gz"
Save this to a file for example script.sh then chmod +x script.sh and you can run it with bash script.sh.

Related

File not adding in zip by bash script but work in command line in terminal

I am running a script in cron and trying to add a file in a zip by below code,
#!/bin/bash
cd /home/mainfolder/cron
date=$(date --date=yesterday +%F)
FILE=/home/mainfolder/folder/$date.zip
if test -f "$FILE"; then
echo $date
else
wget -r -np -nH --user=USER --password=PASSWORD https://sercret-website.com/$date.zip -P /home/mainfolder/folder/
zip -ur $date.zip file.txt
mkdir temp
cp /home/mainfolder/folder/$date.zip /home/mainfolder/temp/
cd /home/mainfolder/temp/
fi
my file name is yesterday date lets say today is 2019-12-18 (yyyy-mm-dd) my file name is 2019-12-17.zip
when I run in bash sh -x script.sh it didn't work
when i run normally in terminal it work well
zip -ur 2019-12-17.zip file.txt
I am using CentOS server.
Anyone know what I am doing wrong.
I tries
#!/bin/sh != #!/bin/bash
#!/bin/sh
#!/bin/bash
nothing work
I'd change date=$(date --date=yesterday +%F) to:
date=`date --date=yesterday +%F`
and write zip as
zip -ur ${date}.zip file.txt
and shebang as:
#!/bin/sh -x
Then I'd look for reason of failure in cron log files.

Running executable file with additional options or arguments

I'm writing a bash script Test.sh that aims to execute anotherscript (a linux executable file):
#!/bin/bash -l
mp1='/my/path1/'
mp2='/my/path2/anotherscript'
for myfile in $mp1*.txt; do
echo "$myfile"
"$mp2 $myfile -m mymode"
echo "finished file"
done
Notice that anotherscript takes as arguments $myfile and options -m mymode.
But I get the file not found error (says Test.sh: line 8: /my.path2/anotherscript: No such file or directory).
My questions are:
I have followed this question to get my bash script to run the executable file. But I'm afraid I still get the error above.
Am I specifying arguments as they should to execute the file?
I suggest you use
sh -c "$mp2 $myfile -m mymode"
instead of just
"$mp2 $myfile -m mymode"
#!/bin/bash -l
dir=`find /my/path1/ -name "*.txt"`
mp2='/my/path2/anotherscript'
for myfile in "$dir"; do
echo "$myfile"
"$mp2" "$myfile" -m mymode
echo "finished file"
done
Make sure anotherscript has execution right (chmod +x anotherscript).

youtube-dl problems (scripting)

Okay, so I've got this small problem with a bash script that I'm writing.
This script is supposed to be run like this:
bash script.sh https://www.youtube.com/user/<channel name>
OR
bash script.sh https://www.youtube.com/user/<random characters that make up a youtube channel ID>
It downloads an entire YouTube channel to a folder named
<uploader>{<uploader_id>}/
Or, at least it SHOULD...
the problem I'm getting is that the archive.txt file that youtube-dl creates is not created in the same directory as the videos. It's created in the directory from which the script is run.
Is there a grep or sed command that I could use to get the archive.txt file to the video folder?
Or maybe create the folder FIRST, then cd into it, and run the command from there?
I dunno
Here is my script:
#!/bin/bash
pwd
sleep 1
echo "You entered: $1 for the URL"
sleep 1
echo "Now downloading all videos from URL "$1""
youtube-dl -iw \
--no-continue $1 \
-f bestvideo+bestaudio --merge-output-format mkv \
-o "%(uploader)s{%(uploader_id)s}/[%(upload_date)s] %(title)s" \
--add-metadata --download-archive archive.txt
exit 0
I ended up solving it with this:
uploader="$(youtube-dl -i -J $URL --playlist-items 1 | grep -Po '(?<="uploader": ")[^"]*')"
uploader_id="$(youtube-dl -i -J $URL --playlist-items 1 | grep -Po '(?<="uploader_id": ")[^"]*')"
uploaderandid="$uploader{$uploader_id}"
echo "Uploader: $uploader"
echo "Uploader ID: $uploader_id"
echo "Folder Name: $uploaderandid"
echo "Now downloading all videos from URL "$URL" to the folder "$DIR/$uploaderandid""
Basically I had to parse the JSON with grep, since the youtube-dl devs said that implementing -o type variables into any other variable would clog up the code and make it bloated.

Pipe to another command bash

I cannot seem to get my bash script to work, i want to pipe the output from the gunzip command to another command but it is not working, can anyone help me?
The gunzip command outputs a tar file that i want to then use the tar command to put back yo the original file.
# let the user choose what they want to Restore
echo -n "Select the file or directory you want to Restore"
read Chosendata
echo -e "Starting Restore"
# unziping files
gunzip ${Chosendata} | tar xvf - #Here
# end the restore.
echo -e "Restore complete"
Use gunzip -c.
-c, --stdout write on standard output, keep original files unchanged
Or tar only: tar -xzf ${Chosendata}.

Shell Scripts with conditional echos

How do I exclude what my script is doing and only have echo's print?
For instance, i am taring a directory and I don't want every file it tar's to echo.. only the echo command.
#! /bin/bash
clear
echo "Compressing the files"
cd ~/LegendaryXeo/html/
tar --exclude=".git" -cvf site.tar *
mv site.tar ~/LegendaryXeo/work/
cd ~/LegendaryXeo/work/
clear
echo "Extracting the site"
tar -xvf site.tar
echo "Deleting Tar"
cd ~/LegendaryXeo/work/
rm -f site.tar
clear
echo "Copying files to server"
scp -r ~/LegendaryXeo/work/* user#site.com:~/../../domains/
Redirect the output of tar to /dev/null:
tar [your options] [files] &> /dev/null
Any echo command you have in your script will still output to the screen.

Resources