Variables in other Variables...

Forum rules
Before you post please read how to get help
Post Reply
StevenC21
Level 2
Level 2
Posts: 89
Joined: Mon Mar 19, 2018 11:02 pm

Variables in other Variables...

Post by StevenC21 » Wed Aug 01, 2018 1:20 am

Sorry that is such a dumb question. Basically, I have a variable $1, that part is fine. But, I need a variable $tarred which needs to be whatever $1 is, with the extension .tar added in. This is what I was typing, please tell me what I did wrong and what would be right. Thank you.

Code: Select all

#!/bin/bash

${1}.tar=$tarred
echo $tarred
When I execute it, it just says:

Code: Select all

tarzip: 3: tarzip: .tar=: not found
Thank you for your time.

Update: Fixed it, and completed script. Here it is for your own use, if you find it helpful.
Attachments
tarzip.sh.bz2
(170 Bytes) Downloaded 13 times
Last edited by StevenC21 on Wed Aug 01, 2018 1:59 am, edited 1 time in total.
"A computer is like air conditioning. It becomes useless when you open Windows."
- Linus Torvalds

User avatar
catweazel
Level 18
Level 18
Posts: 8177
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Variables in other Variables...

Post by catweazel » Wed Aug 01, 2018 1:23 am

StevenC21 wrote:
Wed Aug 01, 2018 1:20 am

Code: Select all

${1}.tar=$tarred
Hi @StevenC21

$tarred is empty. I think you have the assignment round the wrong way.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

StevenC21
Level 2
Level 2
Posts: 89
Joined: Mon Mar 19, 2018 11:02 pm

Re: Variables in other Variables...

Post by StevenC21 » Wed Aug 01, 2018 1:27 am

Yeah, thats part of it. It still doesn't work, though...
"A computer is like air conditioning. It becomes useless when you open Windows."
- Linus Torvalds

User avatar
smurphos
Level 10
Level 10
Posts: 3032
Joined: Fri Sep 05, 2014 12:18 am
Location: Britisher...

Re: Variables in other Variables...

Post by smurphos » Wed Aug 01, 2018 1:48 am

Try

Code: Select all

#!/bin/bash

tarred="$1.tar"
echo $tarred
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html

StevenC21
Level 2
Level 2
Posts: 89
Joined: Mon Mar 19, 2018 11:02 pm

Re: Variables in other Variables...

Post by StevenC21 » Wed Aug 01, 2018 1:57 am

Nevermind I fixed it guys.
"A computer is like air conditioning. It becomes useless when you open Windows."
- Linus Torvalds

rene
Level 9
Level 9
Posts: 2827
Joined: Sun Mar 27, 2016 6:58 pm

Re: Variables in other Variables...

Post by rene » Wed Aug 01, 2018 1:53 pm

Seeing as how you use the "-k" flag to bzip2 you may want to explicitly keep the .tar around, but just in case... note that GNU tar has builtin support for bzip2 as well:

Code: Select all

tar -cjf dir.tar.bz2 dir
--best is default for bzip2 so you can leave out, but if you'd insist on it or the "-v" option for bzip2 that you use you could pass them with

Code: Select all

BZIP2="--best -v" tar -cjf dir.tar.bz2 dir
as well as

Code: Select all

tar -I "bzip2 --best -v" -cf dir.tar.bz2 dir
or, the generic option,

Code: Select all

tar -c dir | bzip2 --best -v >dir.tar.bz2
You can then in fact tweak this last one to also keep the tar itself around as you seem to want to do by saying

Code: Select all

tar -c dir | tee dir.tar | bzip2 --best -v >dir.tar.bz2
but this may not be considered an improvement over the two-liner.

Post Reply

Return to “Scripts & Bash”