You are not logged in.

#1 2022-07-18 04:17:13

randomxusr
Member
Registered: 2021-08-04
Posts: 29

Help with First Bash Scripts (Downloading AUR packages)

Hi All,

I'm trying to write a script to download packages from the AUR. It's a WIP to be sure.

Anyhow, I have a bunch of functions and some custom variables, and looking for some advice.

The current incantation is at http://ix.io/44GE

Suggestions are welcome.

Offline

#2 2022-07-18 12:00:05

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,477

Re: Help with First Bash Scripts (Downloading AUR packages)

I did notice (OC) I need to input a user .. there are easier ways like;

user=$(logname)

It wasn't clear immediately I only needed to input the Name of the build dir. not the full path...
Other than that it works..

I myself use two functions to search and optionally clone a AUR (git) package.
This may make it easier to maintain because you wouldn't need much more than 'git fetch' to check for 'updates.'
This would not be possible using your script.
Here are two functions (as example;) that do about the same as yours, but using the search API and cloning a git repo!

builddir=/some/build/dir
prog=$1
search() {
    search_api="https://aur.archlinux.org/rpc/?v=5&type=search&arg="
    local aurfile
    aurfile=$(curl -s "${search_api}${prog}" | jq '.results[] | .Name' | sed 's/"//g')

    [ -z "${aurfile}" ] && printf '%s\n' "${prog} not found" && exit 1

    PS3="clone one of the results or exit: "
    # shellcheck disable=2015
    select prog in $aurfile 'exit search' ; do
        case $prog in
        'exit search') exit;;
                    *) clone; return;;
        esac
    done
}

clone() {
    if [ -d "${buildir}"/"${prog}" ]; then
        printf '%s\n' "no reason to clone ${prog}, dir excists" && return
    fi

    cd "${buildir}" || return

    git clone https://aur.archlinux.org/"${prog}"
}

Offline

#3 2022-07-18 12:08:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,080
Website

Re: Help with First Bash Scripts (Downloading AUR packages)

1. Why ask for the user's name when there are already built in shell variables for USER and HOME?  What if the user gives their actual name rather than their username which is what you actually need?  Just use HOME.

2. Do you really want to be asked for the download directory each time this runs?  It looks like it's intended to be (re)used on the same directory, so just make a default - and optionally allow it to be overridden by a command line parameter:

buildDir=${1:-aurbuilds}

3. Use `mkdir -p` to get rid of lots of loops and testing.  And if these functions can be replaced with a single command, there's no point in them being functions.  Just inline the command.  For example, most of this script can be reduced to the following (note I replaced the array with a string as the array serves no purpose other than to include a needless bashism):

pkgNames='android-studio kalarmcal-git libkipi pcurses stellarium ttf-ms-fonts visual-studio-code-bin'
buildDir=${1:-aurbuilds}

mkdir -p $(printf '$HOME/$BuildDir/%s ' $pkgNames)

...

4. Why use curl when you can just `git clone`?  Using git has two added benefits, it will create the directory as needed, and will allow for later updates with just a `git pull` `git fetch` in each subdirectory.

Last edited by Trilby (2022-07-18 13:43:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2022-07-18 12:28:16

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,477

Re: Help with First Bash Scripts (Downloading AUR packages)

Trilby wrote:

.. and will allow for later updates with just a `git pull` in each subdirectory.

I'm very much in favor of 'git fetch' due to it's harmless nature, you could fetch all day;)
Using 'git pull' 'could/would' get you into trouble if there are local changes that are not committed...

Offline

#5 2022-07-18 13:46:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,080
Website

Re: Help with First Bash Scripts (Downloading AUR packages)

Thanks for the correction - I've edited my post.  I'll gladly admit almost complete ignorance of effective usage of git - I avoid using it as a VCS and only use it to clone other people's repos which I remove as soon as I've got what I needed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2022-07-18 14:44:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,477

Re: Help with First Bash Scripts (Downloading AUR packages)

I don't do that much with git myself either, but do need it..
Some official/local packages that I want to keep in sync with remote repos, so I can't ditch them;)
Git AUR certainly has made live easier for the user and a AUR helper is easy scripted without the need for 'classy' Uhm AUR-helpers like Yay or yoghurt;) which I didn't use anyway.

Offline

#7 2022-07-18 21:13:57

randomxusr
Member
Registered: 2021-08-04
Posts: 29

Re: Help with First Bash Scripts (Downloading AUR packages)

The are all great points.

I was thinking in terms of adding some checking for directories, and may need to review some of the options in a few commands.

Case in point, as was mentioned, I could use "mkdir -p" to create the parent directory if needed.

My thought about requesting the username, was so that a regular user could run the script and not have the script attempt to save in Root's home directory.

Granted, I could use subshells, command string/substitution, along with relative paths instead of absolute.

I chose to use curl, because I didn't want to download large repos to begin with. I'm not terribly familiar with git just yet, and may switch over to that later.


Thanks for all of the suggestions. I'm going back to the drawing board.

I may remove some of the functions in favor of straight built in commands.

Offline

#8 2022-07-18 21:27:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,080
Website

Re: Help with First Bash Scripts (Downloading AUR packages)

randomxusr wrote:

My thought about requesting the username, was so that a regular user could run the script and not have the script attempt to save in Root's home directory.

That does not follow ... at all.

If the script is run as a regular user $USER is already set to that user's name and $HOME is set to that user's home directory.  On top of that, there'd be no way to save anything in root's home directory even if you tried.

Also note that `mkdir -p` isn't so much for making parent directories (at least in practice) but rather for creating the directory if it doesn't already exist.  So there's no need to test if it exists - if you are going to work with a directory and are not yet sure if it exists, just `mkdir -p` it.

Last edited by Trilby (2022-07-18 21:28:26)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB