Variables in other Variables...

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
StevenC21

Variables in other Variables...

Post by StevenC21 »

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Variables in other Variables...

Post by catweazel »

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.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
StevenC21

Re: Variables in other Variables...

Post by StevenC21 »

Yeah, thats part of it. It still doesn't work, though...
User avatar
smurphos
Level 18
Level 18
Posts: 8501
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: Variables in other Variables...

Post by smurphos »

Try

Code: Select all

#!/bin/bash

tarred="$1.tar"
echo $tarred
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
StevenC21

Re: Variables in other Variables...

Post by StevenC21 »

Nevermind I fixed it guys.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Variables in other Variables...

Post by rene »

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.
Locked

Return to “Scripts & Bash”