我的 .bashrc 中有 4 个函数,它们是 100 行简单代码
除了函数名称和前 3 行代码是变量之外,这些函数是相同的。
如何将常用的 97 行代码拉出并放入一个单独的实例化块中,然后从 4 个函数中的每一个调用该块?
我尝试用这些常见的 97 行创建一个通用功能块,并从一个非常小的函数中调用它,但我无法让它工作。
这是顶部有 3 行独特行,下面是公共代码的函数
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
}
答案1
使用位置参数:
download() {
fdir_zz="$1"
pone_dir="$1"
downloads_z="$3"
...
}
download "/home/user/podcast_author" "podcast_author" ~/Downloads
当然,您也可以设置函数,这样您就不需要输入这些内容:
download_podcaster(){
download "/home/user/podcast_author" "podcast_author" ~/Downloads
}
download_something_else(){
download ...
}
选择:
仅使用一个位置参数并使用语句对其余参数进行硬编码case ... esac
:
download(){
case $1 in
podcaster)
fdir_zz="/home/user/podcast_author"
pone_dir="podcast_author"
downloads_z="$HOME/Downloads"
;;
something_else)
fdir_zz="..."
pone_dir="..."
downloads_z="..."
;;
esac
...
}
download podcaster
download something_else