Tar with variable date in separate shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tar with variable date in separate shell
# 1  
Old 11-28-2017
Tar with variable date in separate shell

Hi,

I am trying to Zip a folder inside a shell script like below. It successfully taring but it only returns Null inside the variables. Searched a lot but no clue to finding the root cause.
Code:
testno=1; 
date=20171128; 
DIR=/root/ sh -c 'cd $DIR; tar cvf "AWS.${testno.${date}.tar" "./AWS"'

please help..

BR
PD
# 2  
Old 11-28-2017
I think that the problem you have is that you have single quoted the whole process. This will prevent the shell from interpolating your variables to build the command it executes. It will literally run what is shown.

Try the following very similar variation:-
Code:
DIR=/root/ sh -c "cd $DIR; tar cvf AWS.${testno.${date}.tar ./AWS

You could also try:-
Code:
DIR=/root
cd $DIR
tar cvf AWS.${testno.${date}.tar ./AWS

....or if you need to get back to where you started (and that could be anywhere)
Code:
DIR=/root
pushd $DIR
tar cvf AWS.${testno.${date}.tar ./AWS
popd



I hope that one of these helps,
Robin
# 3  
Old 11-28-2017
Why do you need a sh command at all?
# 4  
Old 11-28-2017
Thank you all for your kind time and reply .
I ve searched for my requirement and found somthing like below hence tried to modify that command.
Code:
#DIR=~/test/ sh -c 'cd $DIR; du -a | cut -d/ -f2 | sort | uniq -c | sort -nr'

But its seems Pushd and Popd is much simpler.I will go with it.
Code:
DIR=/root
pushd $DIR
tar cvf AWS.${testno}.${date}.tar ./AWS
popd

Thank you again for all your time.. Love unix.com
This User Gave Thanks to pradyumnajpn10 For This Post:
# 5  
Old 11-28-2017
If you're using a shell that doesn't provide pushd and popd, you can also use:
Code:
DIR=/root
if cd "$DIR"
then	tar cvf AWS.${testno}.${date}.tar ./AWS
	cd -
fi

# 6  
Old 11-29-2017
Quote:
Originally Posted by Don Cragun
If you're using a shell that doesn't provide pushd and popd, you can also use:
Code:
DIR=/root
if cd "$DIR"
then	tar cvf AWS.${testno}.${date}.tar ./AWS
	cd -
fi

And I tend to use a subshell:
Code:
DIR=/root
(cd "${DIR}" && tar cvf AWS.${testno}.${date}.tar ./AWS)

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate variable value

Friends, I have the following problem var=30|500; I need to keep those values in the variable other unique pattern that I have is | so should be var2=30; var3=500; I tried to use my split does not work Example failed var2=split("|",$var); var3=split("|",$var); (4 Replies)
Discussion started by: tricampeon81
4 Replies

2. Shell Programming and Scripting

Separate a hash variable into 2 parts in Perl

Dear Perl users/experts, Could somebody help me how to solve my problem, I have a hash variable that I want to convert into dot file (graphviz). I know how to convert it to dot file but I need some modification on the output of the hash variable before convert it to dot file. Eeach key of... (1 Reply)
Discussion started by: askari
1 Replies

3. Shell Programming and Scripting

How to separate a statement based on some delimiter and store each field in a variable?

Hi, Variable1 = MKT1,MKT2,MKT3,MKT4 Now i want to store each of these value seperated by comma to a array and access each of the values. Also find out number of such values seperated by comma. Variable1 can have any number of values seperated by comma. Thanks :) (3 Replies)
Discussion started by: arghadeep adity
3 Replies

4. Shell Programming and Scripting

Awk variable in a separate file

Hi all, I have a requirement to put all the varibles used in an awk command in a separate file. This is because i have arround 100 variables used in an awk command. So i want to put all the variables used for the awk command in a separate file. Please help me on this. Thanks in adv. (6 Replies)
Discussion started by: gani_85
6 Replies

5. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

6. Shell Programming and Scripting

Date variable in shell script

Hello guys n girls, I am using ksh script on AIX platform. I am getting a DATE from input file in "YYYYMMDDHHMISS" format. Now I want the same date in "YYYYMMDD" format into another variable. For example, if the DATE is 20101018121445 then I need 20101018 in another variable. Please... (6 Replies)
Discussion started by: Poonamol
6 Replies

7. Shell Programming and Scripting

Separate date timestamp use awk or sed command ?

Hi, I have logfile like this : Actually the format is date format : yyyymmddHHMMSS and i want the log become this format yyyy-mm-dd HH:MM:SS for example 2009-07-19 11:46:52 Can somebody help me ? Thanks in advance (3 Replies)
Discussion started by: justbow
3 Replies

8. UNIX for Dummies Questions & Answers

tar, zip multiple separate directories and move the results to another volume

TIA, I'm using FreeBSD 6 I have a series of Directories (A,B,C,...Z). Each directory has files and other directories within it. I want to compress the contents of each top directory into a single file so that I get an archive of each directory (for example, A.gzip) AND and want to move... (5 Replies)
Discussion started by: jccbin
5 Replies

9. Shell Programming and Scripting

shell script to selectively tar directory based on date

hye everybody :) , i'm new to the scripting world.. hope you guys can help me out with this one.. i'm trying to identify any directory under /tmp/saya that is created more than one day from the current date.. e.g, today is March 14, so any directory that has time stamp March 13 backwards, i... (2 Replies)
Discussion started by: fara_aris
2 Replies

10. Shell Programming and Scripting

How to separate a variable

Hi, This is what happened after I came back from vacation: I don't remember how to do some simple things anymore. I have a variable being passed in Kshell script. I only know the variable is like string: "str1 str2 str3 ....", and they are separated with blank spaces. I need to separate each... (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question