You are not logged in.
Pages: 1
Acording to the wiki "AUR helpers" are not supported. Looking for a way to easily update all my repositories i decided to try and write a simple script for it and im wondering if my script would be considered an acceptable way to do it and if not, how can I change it?
#! /bin/bash
CURRENTDIR="$(pwd)";
function update() {
WORKDIR="$(pwd)";
if [ "$WORKDIR" == "/home/$USER/git" ]
then
declare -a array;
i=0;
for d in */;
do
array[$i]=$d;
i=$((i + 1));
done
printf "Updating ${#array[@]} git repos\n"
for dir in "${array[@]}";
do
cd "$dir";
UPDATE=$(git pull);
if [ "$UPDATE" == "Already up to date." ]
then
printf "$(basename "$PWD") is up to date\n";
else
printf "$UPDATE\n";
printf "$(basename "$PWD") was updated and needs to be rebuilt\n";
fi
cd "$WORKDIR";
done
else
cd "/home/$USER/git";
update;
fi
}
update;
cd "$CURRENTDIR";
Offline
Anytime you have a if with a huge body and a tiny else, you may want to invert the logic and remove the main body from the conditional:
[[ "$WORKDIR" == $HOME/git ]] || cd $HOME/git || exit 1
declare -a array
#...
But that array construction is odd, instead replace those 8 lines with this:
array=(*/)
printf 'Updating %s git repos\n' ${#array[@]}
There's also no reason to encapsulate everything in a function for a script that does very little than call that function, and given that this is a script, there is no need to `cd` back to the starting directory at the end. So really the whole script could just be the following:
#!/bin/bash
for dir in $HOME/git/*/; do
cd $dir
if [[ "$(git pull)" == "Already up to date." ]]; then
printf '%s is up to date\n' $(basename $dir)
else
printf '%s was updated and needs to be rebuilt\n' $(basename $dir)
fi
done
But I'd also suggest - given that the git pull command will already give feedback on whether or not it did anything, there is no need for than remaining conditional. Instead:
for dir in $HOME/git/*/; do
cd $dir
git pull
done
(edit: you may also want to consider bash options like nullglob for a script like this.)
Last edited by Trilby (2018-06-24 10:39:52)
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Hey thank you, it seems i have a lot to learn. Using nullglob as I understand is to make sure $HOME/git/*/ returns 0 arguments if the folder is empty? Also do I need to unset nullglob like this:
#! /bin/bash
shopt -s nullglob
for dir in $HOME/git/*/; do
cd $dir
git pull
done
shopt -u nullglob
Offline
Yes, that is what nullglob is for. If you know that there will always be at least one subdirectory then it doesn't really matter, but it's good practice when looping over a glob like that.
But just like there is no need to `cd` back to the starting directory, there is no need to unset shell options at the end of a script. Settings within a script only apply to that script (or any child processes it spawns, but that's not relevant here) - so when that script end, those settings no longer apply to anything.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
I wish you much success with this small script. You are basically starting to write your own AUR helper I have my own written in luajit.
There is no problem with using an AUR helper, but you have to know how to build packages manually. If you have problems with an AUR helper, then don't run to the forum for help, but build the AUR packages manually. If that works, then send a bug report to the developer of the AUR helper. If you get an error during the manual build, then look for help in the forum or report a bug to the package maintainer if necessary. The helper should only be used to automate tedious tasks you are able to perform without it.
Last edited by progandy (2018-06-24 18:18:36)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Worth noting, the script isn't really doing anything that requires GNU bash, so you could use if [ -d "$i" ]; then cd "$i" ...
This would remove the non-POSIX requirement for nullglob, allowing you to use #!/bin/sh
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
the script isn't really doing anything that requires GNU bash
It was ... until I rewrote it
But I take baby steps in my mission to save the world by elimitating one BASHism at a time.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Requires vs. uses without strictly requiring. Alternatively, the current script does not require. Either one works.
Carry on, noble crusader!
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
Pages: 1