![sudo 액세스를 기반으로 하는 스크립트에서 중복 코드 방지](https://linux55.com/image/185202/sudo%20%EC%95%A1%EC%84%B8%EC%8A%A4%EB%A5%BC%20%EA%B8%B0%EB%B0%98%EC%9C%BC%EB%A1%9C%20%ED%95%98%EB%8A%94%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%EC%A4%91%EB%B3%B5%20%EC%BD%94%EB%93%9C%20%EB%B0%A9%EC%A7%80.png)
여기서 중복 코드를 줄이는 방법이 있나요?
if have_sudo_access; then
echo "Installing system wide"
FONTS_PATH="/usr/share/fonts/.local/share/fonts/"
sudo mkdir -p "$FONTS_PATH"
SauceCodePro="https://github.com/ryanoasis/nerd-fonts/blob/master/patched-fonts/SourceCodePro"
sudo curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Regular.ttf" "$SauceCodePro/Regular/complete/Sauce%20Code%20Pro%20Nerd%20Font%20Complete.ttf"
sudo curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Bold.ttf" "$SauceCodePro/Bold/complete/Sauce%20Code%20Pro%20Bold%20Nerd%20Font%20Complete.ttf"
sudo curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Italic.ttf" "$SauceCodePro/Italic/complete/Sauce%20Code%20Pro%20Italic%20Nerd%20Font%20Complete.ttf"
else
echo "Installing font for local user"
FONTS_PATH="$HOME/.local/share/fonts/"
mkdir -p "$FONTS_PATH"
SauceCodePro="https://github.com/ryanoasis/nerd-fonts/blob/master/patched-fonts/SourceCodePro"
curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Regular.ttf" "$SauceCodePro/Regular/complete/Sauce%20Code%20Pro%20Nerd%20Font%20Complete.ttf"
curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Bold.ttf" "$SauceCodePro/Bold/complete/Sauce%20Code%20Pro%20Bold%20Nerd%20Font%20Complete.ttf"
curl -L -o "$FONTS_PATH/Sauce Code Pro Nerd Font Italic.ttf" "$SauceCodePro/Italic/complete/Sauce%20Code%20Pro%20Italic%20Nerd%20Font%20Complete.ttf"
fi
나는 다음과 같은 것들을 생각해 보았습니다.
if have_sudo_access; then
alias mysudo = "sudo "
else
alias mysudo = ""
fi
그런 다음 sudo가 필요한 모든 명령에 mysudo를 사용하십시오.
답변1
한 가지 방법은 다음과 같습니다.
#!/bin/bash
## make a temp dir
tmpDir=$(mktemp -d)
## Download the files to it
SauceCodePro="https://github.com/ryanoasis/nerd-fonts/blob/master/patched-fonts/SourceCodePro"
curl -L -o "$tmpDir/Sauce Code Pro Nerd Font Regular.ttf" "$SauceCodePro/Regular/complete/Sauce%20Code%20Pro%20Nerd%20Font%20Complete.ttf"
curl -L -o "$tmpDir/Sauce Code Pro Nerd Font Bold.ttf" "$SauceCodePro/Bold/complete/Sauce%20Code%20Pro%20Bold%20Nerd%20Font%20Complete.ttf"
curl -L -o "$tmpDir/Sauce Code Pro Nerd Font Italic.ttf" "$SauceCodePro/Italic/complete/Sauce%20Code%20Pro%20Italic%20Nerd%20Font%20Complete.ttf"
## Check for sudo access using whatever "have_sudo_access" is supposed to be
if have_sudo_access; then
echo "Installing system wide"
sudo mkdir -p "/usr/share/fonts/.local/share/fonts/"
sudo mv "$tmpDir"/* "/usr/share/fonts/.local/share/fonts/"
else
echo "Installing font for local user"
mkdir -p "$HOME/.local/share/fonts/"
mv "$tmpDir"/* "$HOME/.local/share/fonts/"
fi
## delete the now empty tmp dir
rmdir "$tmpDir"