You are not logged in.
Hey guys. I'm trying to build a basic tar script so I don't have to memorize the options every time. Here it is:
#!/bin/bash
# tarer - tar files and folders
NAME=$1
LOCATION="$2"
if [[ -z $LOCATION ]]; then
echo "tarer <compressed-name>.tgz </location/to/start>"
exit;
fi
tar -czpvf "$NAME".tgz "$LOCATION"
The script works alright until I specify numerous (more than one) file. For example, if i do "tarer my-scripts ~/.bin" no problem but if i do "tarer couple-scripts ~/.bin/bgcmd ~/.bin/resizeimage" it'll only compress the first. I know enough of bash that $2 will only read the argument until a space so I tried $@ for location but that is reading the full argument:
tar: couple-scripts /home/todd/.bin/bgcmd /home/todd/.bin/resizeimage: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
How would I got about making everything after the $NAME variable part of the $LOCATION variable?
Last edited by Gen2ly (2009-03-26 09:33:59)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
You must assign the whole tail to LOCATION, e.g. (debug version)
#!/bin/bash
# tarer - tar files and folders
NAME=$1
shift
LOCATION="$*"
if [[ -z $LOCATION ]]; then
echo "tarer <compressed-name>.tgz </location/to/start>"
exit;
fi
echo "'$NAME' - '$LOCATION'"
#tar -czpvf "$NAME".tgz "$LOCATION"
./tarer couple-scripts ~/.bin/bgcmd ~/.bin/resizeimage
then produces on my system
'couple-scripts' - '/home/bp/.bin/bgcmd /home/bp/.bin/resizeimage'
To know or not to know ...
... the questions remain forever.
Offline
Shift as said, and also $@, but you also can't assign $@ to a variable and use it later. You have to put it in tar -czpvf "$NAME".tgz "$@"
Offline
First I heard of it. Really good tip bernarcher. I tried as echo it does work, oddly though tar doesn't recognize it:
tarer couple-scripts ~/.bin/bgcmd ~/.bin/resizeimage
tar: Removing leading `/' from member names
tar: /home/todd/.bin/bgcmd /home/todd/.bin/resizeimage: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
I did 'tar -czpvf couple-scripts.tgz ~/.bin/bgcmd ~/.bin/resizeimage' just to be sure I'm not losing it and it does just fine.
Reworked script is:
#!/bin/bash
# tarer - tar files and folders
NAME=$1
shift
LOCATION="$*"
if [[ -z $LOCATION ]]; then
echo "tarer <compressed-name>.tgz </fileorfolders>"
exit;
fi
#echo " '$NAME' - '$LOCATION' "
tar -czpvf "$NAME".tgz "$LOCATION"
Unsure just how echo can print it right but tar is unable to recognize it. Any ideas?
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
on thought is that when you tar ... "$LOCATION" (=$*) you're actually doing
tar -czpvf "couple-scripts.tgz" "~/.bin/whatever ~/.bin/whateverelse" which of course, would fail on tar but not echo.
try removing the quotes. one of the few cases where spaces are your friend
//github/
Offline
brisbin, hit the nail right on. Because spaces became protected in the LOCATION variable quotes became verbatim in the tar line. Got it fixed. Appreciate the help. Here's the finished bash script for any who'd like to use it:
#!/bin/bash
# dino - tar files and folders
NAME=$1
shift
LOCATION="$*"
if [[ -z $LOCATION ]]; then
echo "dino <compressed-name>.tgz </fileorfolders>"
exit;
fi
tar -czpvf "$NAME".tgz $LOCATION
Last edited by Gen2ly (2009-03-26 11:08:37)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
Why not:
alias mytar="tar -czvf"
(the -p option has no effect with -c)
Offline
lol tam, for some odd reasion i didn't think of it. At least now i don't have to type the .tgz.
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline