Κάτι που το κάνεις με 6 γραμμές, γιατί να το κάνεις με … 116;
Παρακάτω είναι ένα παράδειγμα χρήσης κάποιων συναρτήσεων που έχω φτιάξει με τα χρόνια για να κάνω ευκολότερη την ζωή μετά από κάποιο format.
Μια χρήσιμη και πλήρης συνάρτηση είναι η pkg_install
που εγκαθιστά ένα πακέτο με χρήση της apt
.
function pkg_install() {
pkg=$1
cmd=$2
set +e
# Check if package exists
pkg_name_exist=`${APT_CACHE} pkgnames | grep "^${pkg}$"`
if [[ "${pkg_name_exist}" == "${pkg}" ]]; then
# Check if package allready installed
${DPKG} --get-selections | grep -q "^${pkg}[[:space:]]*install$"
if [[ "$?" == "1" ]]; then
# Warning if command exists on path
if [[ ! "${cmd}" == "" ]]; then
if [ -x "$(command -v ${cmd})" ] ; then
echo "${magenta}${cmd} is allready installed in '${blue}$(command -v ${cmd})${reset}'."
fi
fi
# Do the installation
echo "${green}Installing package '${bGreen}${pkg}${green}'...${reset}"
sudo ${APT} install -y ${pkg}
# Check if package failed to install
if [ $? != 0 ]; then
echo "${bRed}ERROR:${red}package ${pkg} failed do installl.${reset}"
else
# If cmd show the binary
if [[ ! "${cmd}" == "" ]]; then
echo "${green}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
fi
fi
else
echo "${yellow}Skipping installation of package '${bold}${pkg}${reset}${yellow}'...${reset}"
# If cmd show the binary
if [[ ! "${cmd}" == "" ]]; then
echo "${yellow}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
fi
fi
else
echo "${red}${bold}ERROR:${reset}${red}package ${pkg} is not at the repositories${reset}"
fi
}
'Όπως βλέπετε είναι αρκετά έξυπνη και ομολογουμένως ολίγον παρδαλή και ολίγον θορυβώδεις. Αν την μελετήσετε ίσως κλέψετε μερικές τεχνικές για καλύτερα bash scripts. Η απλά πάρτε την όπως είναι.
Επίσης εγκαθιστά κάνοντας compile κάποια εργαλεία που χρησιμοποιώ και είναι γραμμένα σε rust.
Αν έχετε απορία σε κάποια εντολή ή τεχνική, ρωτήστε. Αν το φτιάξετε καλύτερο ή βρείτε κάποιο λάθος θα με κάνετε πολύ χαρούμενο