You are not logged in.
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
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"
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline