You are not logged in.

#1 2009-03-25 16:33:58

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] Basic Tar Script - Needs Help

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

#2 2009-03-25 16:56:15

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: [Solved] Basic Tar Script - Needs Help

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

#3 2009-03-25 17:01:27

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Basic Tar Script - Needs Help

edit.

i take it back... shift and $* is better.

Last edited by brisbin33 (2009-03-25 17:02:12)

Offline

#4 2009-03-25 17:03:54

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Basic Tar Script - Needs Help

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

#5 2009-03-25 18:09:38

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Basic Tar Script - Needs Help

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  big_smile 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

#6 2009-03-25 18:15:39

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Basic Tar Script - Needs Help

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

Offline

#7 2009-03-26 09:32:44

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Basic Tar Script - Needs Help

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

#8 2009-03-26 18:03:32

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: [Solved] Basic Tar Script - Needs Help

Why not:

alias mytar="tar -czvf"

(the -p option has no effect with -c)

Offline

#9 2009-03-26 18:36:15

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Basic Tar Script - Needs Help

lol tam, for some odd reasion i didn't think of it.  At least now i don't have to type the .tgz. smile


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB