Eu tenho 4 funções que são 100 linhas de código simples no meu .bashrc
Essas funções são idênticas, exceto pelo nome da função e as primeiras 3 linhas de código que são variáveis.
Como posso puxar as 97 linhas de código que são comuns para um bloco instanciado separado que eu chamo de cada uma das 4 funções?
Eu tentei fazer um bloco de função comum dessas 97 linhas comuns e chamá-lo de uma função muito pequena, mas não consegui fazer isso funcionar.
Aqui está a função com as 3 linhas únicas no topo e o código comum abaixo disso
function download_podcaster()
######################################################################
########## ##########
# UNIQUE CODE FOLLOWS #
#set-up the directory variables
dir_zz="/home/user/podcast_author"
pone_dir="podcast_author"
downloads_z="~/Downloads"
########## UNIQUE CODE ABOVE ##########
########## ##########
######################################################################
# vvvvv COMMON CODE BELOW vvvvvv
echo; echo " .... this output is from youtube-dl ..."; echo
#download the file
youtube-dl -f 140 --restrict-filenames -o $dir_zz'/%(title)s.%(ext)s' $1
#make dir if does not aleready exist
mkdir -p $dir_zz
#change to the downloads directory
cd $dir_zz
echo
echo "current dir is: "$(pwd)
echo
#open the downloads location to show the files with pcmanfm
#pcmanfm ~/Downloads &
pcmanfm $dir_zz &> /dev/null
file_name_z=$(youtube-dl --get-filename --restrict-filenames "$1")
echo; echo "file name from provided by youtube-dl: "$file_name_z; echo
#grab the filename from youtube, and parse it into something useful
#remove 11 digits before end of file name
file_name_z=$(echo $file_name_z | sed 's|...........\.mp4$|.mp4|g' | sed 's|...........\.m4a$|.m4a|g' \
| sed 's|...........\.webm$|.webm|g' \
| sed 's|,||g' | sed 's|!||g' | sed 's| |_|g' | \
sed 's|-\.||g' | sed 's|webm||g' | sed 's|mp4||g' | sed 's|\?||g').m4a
# remove , remove ! replace " " with "_"
# remove "-." remove "webm"
echo; echo "file name after , ! \" \" ? removed: "$file_name_z; echo
var1=$(ls -t | grep -E "^[0-9]{3}" | sort | tail -n 1 | cut --bytes=1-3)
echo; echo "\$var1 3 digit highest number from file set is: "$var1; echo
sleep .25
#create the variable to assign the next file number to front of file name
next_file_number=$(printf "%03d\n" $((10#$var1+5)))
echo; echo "File number plus 5 is: "$next_file_number; echo
sleep .25
#new file name with three digit number in front of filename
file_name_y=${next_file_number}_${file_name_z}
echo; echo "concatenated filename is \$file_name_y: ""$file_name_y"; echo
#move the old file to the new file name
mv "$file_name_z" "$file_name_y"
echo; echo " ""$file_name_y"; echo
#plug phone in. Phone mount point in file system can be seen here
#echo; cd /var/run/user/$UID/gvfs; ls; echo
#reference
#https://askubuntu.com/questions/342319/where-are-mtp-mounted-devices-located-in-the-filesystem
#
#How to get to the phone directory on the phone if the directory is dynamically allocated on the
#reference
#https://askubuntu.com/a/454697/624987
#phone or changes, use this
#cd /var/run/user/$UID/gvfs; cd * ; cd *; cd Music; mkdir -p $phone_dir; cd $phone_dir; ls
#
#"cd *" changes to the first directory shown
#grab the directory on the phone in which to place the file
cd /var/run/user/$UID/gvfs; cd * ; cd *; cd Music; mkdir -p $phone_dir; cd $phone_dir
phone_dir_long_path=$(pwd)
echo " ... now copying the file to the phone -->"; echo
#copy file to phone
cp $dir_zz/"$file_name_y" "$phone_dir_long_path"
echo
#open terminal at directory of files
gnome-terminal --title="test" --command="bash -c '$phone_dir_long_path; ls; $SHELL'"
echo
#open the file name with the default app, usually vlc
xdg-open $dir_zz/$file_name_y &
echo
}
Use parâmetros posicionais :
Claro que você também pode configurar funções para não precisar digitar essas coisas:
Alternativo:
Use apenas um parâmetro posicional e codifique o restante com uma
case ... esac
instrução: