You are not logged in.

#1 2010-05-07 11:01:56

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

help learning bash

Hi All,

i've been trying to help out with the Archbang Project and come up with this little script only problem is it doesn't work as inteneded.
What i'm trying to-do is make sure the script can access the files it needs where ever it's run from .


#!/bin/bash
        if [ "$1" = "clean" ]; then
        echo $(pwd)
        rm -v $(pwd)/../overlay/src/pkgs/*.tar.*
        exit 1
        fi

        exec < $(pwd)/../overlay/src/pkgs/packages.extra
        while read package    
        do
        pkgs=$(echo "$pkgs $package")
        done

        echo "fetching $pkgs"
        pacman -d -Sw --noconfirm --cachedir $(pwd)/../overlay/src/pkgs $pkgs

i know about pwd and $0 for getting paths but wasn't sure the best way round making sure the script can find the files.

Anyone got any suggestions or links to some helpfull material i have done a google search but wasn't quiet sure how to word what i was doing.

Regards
Matthew


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#2 2010-05-07 11:31:48

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: help learning bash

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ

I can recommend that the central while/read loop be rewritten as:

IFS=$'\n' read -r -d $'\0' -a pkgs < "$PWD/../overlay/src/pkgs/packages.extra"

This leaves you with an array, so you'll then need to change the pacman execution slightly to:

pacman -Sdw --noconfirm --cachedir "$PWD/../overlay/src/pkgs" "${pkgs[@]}"

Relative paths is probably a bad idea. If you have a way to confirm at some point that a relative path is pointing to a correct location, save that. In other words, suppose at some point you're sure that the path "$PWD/../overlay/src" is known good. Save that as, for example, $absrcroot and then you can reference $absrcroot/pkgs instead of banking on your working directory not changing.

Also, as a general rule of thumb for variables: always double quote them unless you're without a doubt absolutely sure that no white space could ever exist in them. If you're not sure, quote it.

Last edited by falconindy (2010-05-07 11:33:50)

Offline

#3 2010-05-07 13:24:29

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: help learning bash

thank you i will try that shortly smile

Learning something new today wink


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#4 2010-05-07 16:42:45

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: help learning bash

kk just so i understand this i had an idea of checking the out of $0 and PWD to work out where the script is
one of the 2 will have to contain the path to the script and therfore the project.

very crude but something like

if len $0 <1 then 
   check and use PWD instead

is that kinda what you where getting at?

Regards
Matthew

Last edited by genisis300 (2010-05-07 16:43:34)


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

Board footer

Powered by FluxBB