Page 1 of 1

How do I combine two functions into a single script? [SOLVED]

Posted: Mon Dec 18, 2023 9:04 am
by Jasper

Hi all

I have two scripts that I use. The first one downloads podcasts for me:

Code: Select all


#!/bin/sh
urxvt -e yt-dlp -f wa --external-downloader aria2c --embed-thumbnail --batch-file /root/download.txt --path /root/Downloads/Podcasts

Once I have my podcast, I use the second script to remove the unnecessary additional characters in the filename:

Code: Select all


#!/bin/sh
for file in *.m4a ; do
    head=${file#*[}     #part of file name up to (and including) left bracket '['
    find=[${head%]*}]   #pair of brackets with string (=the stuff that should be removed)
    rename  "$find" '' "$file"
done

In an ideal world I would like both to be combined so that I have a single script to run.

Can you inform me what needs to be done?

Both work perfectly, as is :D


Re: How do I combine two functions into a single script

Posted: Mon Dec 18, 2023 12:52 pm
by Trapster

Unless I'm missing something, it should be as simple as putting your second script into the first.
Just remove the

Code: Select all

#!/bin/sh

from the second script and add to change to the Podcast directory

Code: Select all

#!/bin/sh
urxvt -e yt-dlp -f wa --external-downloader aria2c --embed-thumbnail --batch-file /root/download.txt --path /root/Downloads/Podcasts

cd /root/Downloads/Podcasts
for file in *.m4a ; do
    head=${file#*[}     #part of file name up to (and including) left bracket '['
    find=[${head%]*}]   #pair of brackets with string (=the stuff that should be removed)
    rename  "$find" '' "$file"
done



Re: How do I combine two functions into a single script?

Posted: Tue Dec 19, 2023 9:22 am
by Jasper

@Trapster

Thanks for that :thumbup:

Happy Holidays to you and your family 🎄🎄🎄🎄


Re: How do I combine two functions into a single script? [SOLVED]

Posted: Sun Feb 11, 2024 7:18 pm
by user1234

I know this question is quite old; but for anyone searching for this same question, if you want to preserve both files, you can include either of the following in either of the script (you'll probably want this when each file has 100s of lines of code, and does it own separate job):

Code: Select all

. [path to your script]

Code: Select all

source [path to your script]

If anyone would ask me which to prefer, I'd say the above one since (i) it is shorter to write - just a single character, (ii) I have seen this used many times in woof-CE; and (iii) I think source is specific to bash only - I see in Kubuntu, . is a program in /usr/bin, while source is not.

Happy scripting :thumbup2: