variable used as filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable used as filename
# 1  
Old 05-14-2009
Question variable used as filename

Hello, i'm fairly new to scripting, so please bear with me (I did try looking this up first, i figured it had to have been asked already).

Code:
#!/bin/bash
fileName=`date | sed -n 's/ /_/g p' | sed -n 's/^/Backup_/p' | sed -n 's/$/\.tar/p'`; #THIS SETS BACKUP_DATE
echo $fileName #TEST OF VALUE

tar -cf $fileName tmp/*;
#gzip tmp/$fileName;

the test of the value (4th line) works, so i know its not something silly like my permissions screwed up for the script. I commented out the last line because for some reason when the script gets to the tar part (2nd to last line) it just hangs. hitting <ctrl>c the script ends, and there's no tar in my directory. what am i doing wrong?

thanks a bajillion, in advance for your help!
# 2  
Old 05-14-2009
tar sometimes uses
tar cf [tarfile name] [directory]
rather than
tar -cf [tarfile name] [directory]

also add double quotes around the
Code:
tar -cf "$fileName"  tmp/*;

in case you created a file name with spaces in it.

Last edited by jim mcnamara; 05-14-2009 at 01:56 PM..
# 3  
Old 05-14-2009
I always like to use

Code:
tar -cvf "$filename" tmp/*

keeping in mind that tmp is in the current directory or else give the absolutE PATH.


cheers,
Devaraj Takhellambam
# 4  
Old 05-14-2009
Question still not working

Quote:
Originally Posted by devtakh
I always like to use

Code:
tar -cvf "$filename" tmp/*

keeping in mind that tmp is in the current directory or else give the absolutE PATH.
thanks, i changed it to ./tmp/* just to be clear here - the v option is a good idea, but nothing shows still - just the hang

Quote:
Originally Posted by jim mcnamara
tar sometimes uses
tar cf [tarfile name] [directory]
rather than
tar -cf [tarfile name] [directory]

also add double quotes around the
Code:
tar -cf "$fileName"  tmp/*;

in case you created a file name with spaces in it.
thanks, sed command (line 2) takes care of the white space output with the date program. I tried doing it without the dash infront of the options, and it still does exactly the same thing.

any other ideas? [i thought this would be a simple script, this is kind of frustrating Smilie ]
# 5  
Old 05-14-2009
interesting realization. on the Ubuntu 9.04 machine i tried this script with today (while writing it) i got the diagnostic-less hang I've mentioned above. with my macintosh Darwin terminal (OS X 10.5) i got some nasty errors:
Code:
rcmd: getaddrinfo: nodename nor servname provided, or not known
tar: Backup_Thu_May_14_15\:50\:11_EDT_2009.tar: Cannot open: Input/output error
tar: Error is not recoverable: exiting now

with the Free BSD server I've been telnet-ing into today (my college's server) this script works perfectly.

i even made sure (when the scripts run in the different systems) to check:
Code:
$ type bash
bash blah blah /proper/file/path/bash

so as to properly change the first line in my script.

i'm not sure why it only works in my school's server, but in case anyone has the experience/know-how... here's some info (don't know if its useful):
uname -a
FreeBSD school.domain.edu 7.0-RELEASE-p5 FreeBSD 7.0-RELEASE-p5 #1: Sun Oct 5 13:25:04 EDT 2008 bbroder@school.domain.edu:/usr/src/sys/amd64/compile/SCHOOL amd64
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting variable values in filename

Hi All, I have a directory containing multiple files. and also a txt file which contains the list of all filenames and certain values. I want to read the text file line by line and if its 2nd column is matched with the filename in directory, then it must insert the values in 7th column to... (14 Replies)
Discussion started by: CAch
14 Replies

2. Shell Programming and Scripting

Passing gunzipped filename to a variable

Hi , Am trying to gunzip a file and pass the gunzipped file name to a variable , but its not taking up the value. Am trying to execute the command f=`gunzip <filename>`;echo $f. Here the file is getting gunzipped but the file name is not assigned to the variable. Any help on this will be useful.... (5 Replies)
Discussion started by: rogerben
5 Replies

3. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

4. Shell Programming and Scripting

Set the last 4 letter in the filename as a variable

Hi all, I want to set the last 4 letter in the filename as a variable. For example, i have AB1234.txt file and i need to have last 4 letter as a variable. It should be like ; get last four letter set var = 1234 How can i write this in C shell?? Thanks, zibi (3 Replies)
Discussion started by: zibi
3 Replies

5. Shell Programming and Scripting

Inserting variable value into filename

Greetings, people of UNIX/Linux forums. I am having a problem with a script, where I am trying to create a new variable. The value of this variable would be dependent on the value in a couple other previous variables (all variables are 2-digit integers). Here is my code: #set the stations... (3 Replies)
Discussion started by: TheSMan5
3 Replies

6. UNIX for Dummies Questions & Answers

assingn a variable a filename and then reading it in

Im trying to set a filename to a variable and then read the file in using the variable but im getting a syntax error. any ideas? #!/bin/bash function scanFile() { while read $1 do echo $filename done } file1=report.log scanFile() $file1 (3 Replies)
Discussion started by: magnia
3 Replies

7. UNIX for Dummies Questions & Answers

'Crop' date from filename into a variable

Hi all, I have a scenario as below. I created a file - $touch myfile.onlytesting.20090227.txt I want to assign the date only - 20090227 into a variable x. How do I do this in unix command? Please help. Thanks;) (4 Replies)
Discussion started by: luna_soleil
4 Replies

8. Shell Programming and Scripting

Variable value not being fetched in the filename..

Hi, In the code below, the $DD1, $DD2, $MM1, $MM2 are not fetching the values. Can anybody tell me where have i made wrong. Shift_date.sh is a script which gives the previous date if we give the current date in the format YYYYMMDD and +/- how many days past/future.. Like Shift_date.sh... (3 Replies)
Discussion started by: RRVARMA
3 Replies

9. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies

10. UNIX for Dummies Questions & Answers

AWK Filename as variable

Need some help. I need to load data into some Oracle tables and one of the pieces of data that I need to load is the filename. This filename is distinct every single time. Basically the last 6 characters will be different with no pattern. ex. testfile_041504_003567 To load the filename... (4 Replies)
Discussion started by: firkus
4 Replies
Login or Register to Ask a Question