Help!

self extracting tar archive

 
  

Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Genreal Discussions RSS
Next:  [Samba] CTDB Node unnecessarily banning other nod..  
Author Message
cerr
External


Since: Mar 30, 2009
Posts: 5



PostPosted: Fri Jul 31, 2009 10:12 am    Post subject: self extracting tar archive
Archived from groups: alt>os>linux (more info?)

Hi There,

I want to compile a self extracting tar file and found following:
http://www.stuartwells.net/slides/selfextract.htm which is very
helpful!
But I have three files in my archive:
prg (bin)
cksum (bin)
copy_file.sh (shell script)
and i made changes to the script from the website to make it look like
this:
[script]
#!/bin/bash

echo ""
echo "Self Extracting Tar File"
echo ""
echo "Example by Stuart Wells"
echo ""
echo "Extracting file into `pwd`"
SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
echo 'SKIP: '$SKIP;
#remember our file name
THIS=`/usr/share/working/`$0

echo 'THIS: '$THIS;

# take the tarfile and pipe it into tar
tail +$SKIP $THIS | tar -xz

#
# place any bash script here you need.
# Any script here will happen after the tar file extract.
echo "Finished"
exit 0


# NOTE: Don't place any newline characters after the last line below.
__TARFILE_FOLLOWS__
[script]
Now my script is gonna be called "prg" and upon execution(as root) i
get following error messages printed:
Extracting file into /usr/share/working
../prg: line 12: /usr/share/working: is a directory
THIS: ./prg
tar: Cannot create directory 'prg': File exists
tar:Couldn't remove old file: Not a directory
Finished

Can anyone direct me into the right direction? I've been playing
around with it already but I couldn't really get further a whole
lot... Sad

Thanks a lot!
Ron
Back to top
J.O. Aho
External


Since: Mar 26, 2004
Posts: 2216



PostPosted: Fri Jul 31, 2009 2:10 pm    Post subject: Re: self extracting tar archive [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

cerr wrote:

> # NOTE: Don't place any newline characters after the last line below.
> __TARFILE_FOLLOWS__
> [script]
> Now my script is gonna be called "prg" and upon execution(as root) i
> get following error messages printed:
> Extracting file into /usr/share/working
> ./prg: line 12: /usr/share/working: is a directory
> THIS: ./prg
> tar: Cannot create directory 'prg': File exists
> tar:Couldn't remove old file: Not a directory
> Finished
>
> Can anyone direct me into the right direction? I've been playing
> around with it already but I couldn't really get further a whole
> lot... Sad

The file in question you are trying to replace is open by a process, quick fix
is to rename the script to something else.


--

//Aho
Back to top
Robert Newson
External


Since: Mar 13, 2009
Posts: 4



PostPosted: Fri Jul 31, 2009 6:10 pm    Post subject: Re: self extracting tar archive [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

cerr wrote:
> Hi There,
>
> I want to compile a self extracting tar file and found following:
> http://www.stuartwells.net/slides/selfextract.htm which is very
> helpful!
> But I have three files in my archive:
> prg (bin)
> cksum (bin)
> copy_file.sh (shell script)
> and i made changes to the script from the website to make it look like
> this:
> [script]
> [01]#!/bin/bash
> [02]
> [03]echo ""
> [04]echo "Self Extracting Tar File"
> [05]echo ""
> [06]echo "Example by Stuart Wells"
> [07]echo ""
> [08]echo "Extracting file into `pwd`"
> [09]SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
> [10]echo 'SKIP: '$SKIP;
> [11]#remember our file name
> [12]THIS=`/usr/share/working/`$0
....
I've numbered you script lines so that we can interpret the error messages:
....
> Now my script is gonna be called "prg" and upon execution(as root) i
> get following error messages printed:
> Extracting file into /usr/share/working
> ./prg: line 12: /usr/share/working: is a directory
Ok, what does line 12 say?

> [12]THIS=`/usr/share/working/`$0

What kind of quotes do you have there?

They look like back quotes (`) and not single quotes/apostrophes(') to me.
What's the difference? Back quotes replace what is between them by the
stdout of the command given, so you're trying to run the command:

/usr/share/working

no wonder it comes up with the error message it does. Mefinx you should
try single quotes/apostrophes instead; or, better, stick with the original:

THIS=`pwd`/$0
....
> tar: Cannot create directory 'prg': File exists
Not surprising as tar is trying to create prg in the current directory
but prg already exists there - you're running it!
Problem occurs as you're trying to extract a file from the archive with
the same name as the shell script.

Suggest you again follow the original:

call your script: prg.sh

and not just prg which also happens to be one of the files in the archive.
....
> Can anyone direct me into the right direction? I've been playing
> around with it already but I couldn't really get further a whole
> lot... Sad
Be care with what you play if you do not understand what it does. If it
breaks when you play, put it back.

The .sh suffix on extract.sh in the web page is there for a couple of
reasons:

1) it reminds a human user looking at the directory that the file is
a shell script (the kernel does NOT need it named as such)
2) It allows you to have a binary, or other file, in the archive
without the .sh suffix - as you want with your prg.

If you want it so that prg will self extract on first run and then be
replaced by the extracted file, change your script slightly:

1) in the tar archive call the file that you want to become prg
something like: prg.new
2) in the script just before the ``echo "Finished"'' line add a line
like: mv $0.new $0
Back to top
Jasen Betts
External


Since: Feb 26, 2009
Posts: 15



PostPosted: Sat Aug 01, 2009 7:10 am    Post subject: Re: self extracting tar archive [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-07-31, Robert Newson <nothanks DeleteThis @bullet3.fsnet.oc.ku> wrote:
> cerr wrote:
>> Hi There,
>>
>> I want to compile a self extracting tar file and found following:
>> http://www.stuartwells.net/slides/selfextract.htm which is very
>> helpful!
>> But I have three files in my archive:
>> prg (bin)
>> cksum (bin)
>> copy_file.sh (shell script)
>> and i made changes to the script from the website to make it look like
>> this:
>> [script]
>> [01]#!/bin/bash
>> [02]
>> [03]echo ""
>> [04]echo "Self Extracting Tar File"
>> [05]echo ""
>> [06]echo "Example by Stuart Wells"
>> [07]echo ""
>> [08]echo "Extracting file into `pwd`"
>> [09]SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
>> [10]echo 'SKIP: '$SKIP;
>> [11]#remember our file name
>> [12]THIS=`/usr/share/working/`$0
> ...
> I've numbered you script lines so that we can interpret the error messages:
> ...
>> Now my script is gonna be called "prg" and upon execution(as root) i
>> get following error messages printed:
>> Extracting file into /usr/share/working
>> ./prg: line 12: /usr/share/working: is a directory
> Ok, what does line 12 say?
>
> > [12]THIS=`/usr/share/working/`$0
>
> What kind of quotes do you have there?
>
> They look like back quotes (`) and not single quotes/apostrophes(') to me.
> What's the difference? Back quotes replace what is between them by the
> stdout of the command given, so you're trying to run the command:
>
> /usr/share/working
>
> no wonder it comes up with the error message it does. Mefinx you should
> try single quotes/apostrophes instead; or, better, stick with the original:
>
> THIS=`pwd`/$0

both are potentially dangerous..

for a fairly harmless example imagine the archive was called "safe reboot"

(try it as root sometime when you have the system to yourself)

the correct way to this sort of thing would be

THIS=`pwd`"/$0"

but whist safe this is still wrong headed.

but what's the point of this? $0 holds a valid name for the file,

suppose I download the file and save it to /tmp and then run it from
my home directory. (eg. using "/tmp/prg") gluing names on the front
is just going to break it.

if you really must have the full path to $0 do

THIS=`readlink -f "$0"`

but in most cases, since $0 was sufficient for bash to find the file by
it will also good enough for your script,

if your script changes directory though you may need to use the above.
Back to top
Jasen Betts
External


Since: Feb 26, 2009
Posts: 15



PostPosted: Sat Aug 01, 2009 7:10 am    Post subject: Re: self extracting tar archive [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-08-01, Jasen Betts <jasen.TakeThisOut@xnet.co.nz> wrote:
>
> sed '1,/^__TARFILE_FOLLOWS__/ d'<$0 | tar xz "$@"
>

whist the above will work in most cases, I really meant

sed '1,/^__TARFILE_FOLLOWS__/ d'< "$0" | tar xz "$@"
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Genreal Discussions All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum