1

I have a bash script. I need a modification for safety.

my original bash script:

mv /home/script/backup /home/script/backup2
mysql -u user -ppassword -Ddatabase --batch --skip-column-names -e 'select id, url from videos where url like "%http%" limit 1' | 
while read id url

do youtube-dl -o "/home/script/uploads/$id.mp4" -f 18/17/22/mp4 "$url"
done

mysql -u user -ppassword -Ddatabase -e "UPDATE videos SET url=CONCAT(id,'.mp4') WHERE url LIKE '%http%' limit 1" ;

mysql -u user -ppassword -Ddatabase -e "UPDATE videos SET source_id='1' WHERE source_id in (3) limit 1";

mv /home/script/backup2 /home/script/backup

exit

I need this bash script:

mv /home/script/backup /home/script/backup2
mysql -u user -ppassword -Ddatabase --batch --skip-column-names -e 'select id, url from videos where url like "%http%" limit 1' | 
while read id url

do youtube-dl -o "/home/script/uploads/$id.mp4" -f 18/17/22/mp4 "$url"
done

here: I want to check the file size. if it is greater than 10 KB I want to run the following all commands.

mysql -u user -ppassword -Ddatabase -e "UPDATE videos SET url=CONCAT(id,'.mp4') WHERE url LIKE '%http%' limit 1" ;

mysql -u user -ppassword -Ddatabase -e "UPDATE videos SET source_id='1' WHERE source_id in (3) limit 1";

mv /home/script/backup2 /home/script/backup

exit

If less than 10 KB or not found a file, I want to run only this command.

mv /home/script/backup2 /home/script/backup

exit

2 Answers 2

1

To get the file size in bytes you can use the stat program. For more information on this type man stat into the command line. stat -c %s <file path> will give an output of the file size in bytes which you should then be able to check.

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to what James said, another way to check the file size is using the du command.

du -h somefile.txt

result:

4,0K somefile.txt

To omit the filename you can issue

du -h somefile.txt | cut -f 1

result:

4,0K

1 Comment

You may wish to omit the -h (human readable) this will result in the number being given in K (but the K won't be printed), and you won't need to check and strip the units. -h will result in different units for larger files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.