You are not logged in.

#1 2011-04-16 18:59:16

spoonman
Member
Registered: 2011-04-09
Posts: 13

Simple shell script utility to copy abs tree for build

Hi,
    I'm learning shell script and decided to make a simple script to copy the folder i want from /var/abs to
    a new folder so i can build the package. here it is: (remember, i'm new to shell scripting, if you know a better way to do it, you can modify my script
    and post here a better solution, so, i (and others) can learn with my errors)

#!/bin/bash
#Looks for the abs tree of the software you want
#and copy it to your build path

ABSTREE=/var/abs

echo -n "What software do you want? "
read absname

result=$(find $ABSTREE -name $absname)

for i in $result; do
    echo -n "$i, is this what you want? [y/n] "
    read opt
    if [ $opt = "y" ]; then
    echo -n "Copy to... "
        read buildpath
        relative=${i#$ABSTREE}
        absolute=$buildpath${relative%$absname}
        mkdir -p $absolute
        cp -r $i $absolute
        echo "$i successfully copied to $absolute"
        exit 0 
    fi
done
exit 1

Offline

#2 2011-04-16 23:42:39

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Simple shell script utility to copy abs tree for build

spoonman wrote:
        mkdir -p $absolute
        cp -r $i $absolute
        echo "$i successfully copied to $absolute"

You are assuming `mkdir` and `cp` were successful. You should test them to make sure, and exit with failure if not:

        mkdir -p $absolute || exit 1
        cp -r $i $absolute || exit 1
        echo "$i successfully copied to $absolute"

You could also include your own error message, but mkdir and cp would throw their own if something fails...

        mkdir -p $absolute || { echo "mkdir failed"; exit 1; }
        cp -r $i $absolute || { echo "cp failed"; exit 1; }
        echo "$i successfully copied to $absolute"

Another way is to use `set -e` which will exit the script on any failure without explicit testing:

        set -e
        mkdir -p $absolute
        cp -r $i $absolute
        set +e
        echo "$i successfully copied to $absolute"

Offline

Board footer

Powered by FluxBB