![.bashrc에서 반복되는 코드 블록을 사용하는 방법](https://linux55.com/image/196304/.bashrc%EC%97%90%EC%84%9C%20%EB%B0%98%EB%B3%B5%EB%90%98%EB%8A%94%20%EC%BD%94%EB%93%9C%20%EB%B8%94%EB%A1%9D%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
내 .bashrc에는 100줄의 간단한 코드인 4가지 기능이 있습니다.
함수 이름과 코드의 처음 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