The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing commands in $variables. Paulw0t Shell Programming and Scripting 7 08-08-2008 05:42 PM
Using variables in sed commands hcclnoodles Shell Programming and Scripting 2 01-02-2007 08:56 AM
Passing variables/arguments/parameters to commands Perderabo Answers to Frequently Asked Questions 0 06-13-2004 07:37 PM
Subtracting Variables which are commands Astudent UNIX for Dummies Questions & Answers 2 03-30-2001 09:55 AM
Subing Variables with commands. Astudent UNIX for Dummies Questions & Answers 1 02-26-2001 05:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2005
mharley mharley is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 14
Referencing variables in commands

The script I am writing must be able to run several commands (tar, gzip etc) on filenames that are supplied by variables. I am unsure as to what syntax is required/ideal when referencing variables in filenames. The following is a sample command that I would like the script to execute:
Code:
tar cvf bk{fileName}.${DATEyear}${DATEmonth}.tar {fileName}.${DATEyear}${DATEmonth}* >> /ebccsBackupReport.txt
Is this really poor form? The tar filename format must be bkFILENAME.YEARMONTH.tar, where the filename, year and month are variables set earlier in the script.

Also, is there a way to verify where one or more files bearing a variation on a filename exist within the directory? For example, all files under filename.*? I have been using
Code:
if [ -f filename.* ]
but I've been told that this statement is intended more for verifying the existance of a single file, not a range of files.

I'm using Ksh 88 under Solaris 8.

Thanks for all the help.
  #2 (permalink)  
Old 03-30-2005
moxxx68's Avatar
moxxx68 moxxx68 is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 301
var=`date +%m-%Y`
touch filename.$var


tar -cvzf --backup bk.filename.$var.$var.tgz ./filename.$var.$var* >> ebccsbackureport.txt......


file=`find -name "filename" -print`
if [ -e $file ]
then
echo $file
fi

somewhat of a quick reply hope it helps...
  #3 (permalink)  
Old 03-30-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by mharley
The script I am writing must be able to run several commands (tar, gzip etc) on filenames that are supplied by variables. I am unsure as to what syntax is required/ideal when referencing variables in filenames. The following is a sample command that I would like the script to execute:
Code:
tar cvf bk{fileName}.${DATEyear}${DATEmonth}.tar {fileName}.${DATEyear}${DATEmonth}* >> /ebccsBackupReport.txt
Almost there, but you'd need a $ before {filename} so that the variable's value is evaluated, like you've done with the DATEyear and DATEmonth variables. Apart from that, spot on.
Quote:
Originally Posted by mharley
Also, is there a way to verify where one or more files bearing a variation on a filename exist within the directory? For example, all files under filename.*? I have been using
Code:
if [ -f filename.* ]
but I've been told that this statement is intended more for verifying the existance of a single file, not a range of files.
This really depends on exactly what you're looking for... say if I had some files in my current directory named file1, file2, file3, filen, etc.... you could check how many files match a certain pattern (i.e. which contain the string "file") using find and piping through wc.... Then check that value...
Code:
number=`find . -name "*file*" -prune -print | wc -l`
[[ "$number" -gt "0" ]] && echo "Files found"
Cheers
ZB
  #4 (permalink)  
Old 03-31-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
Quote:
Originally Posted by mharley
Also, is there a way to verify where one or more files bearing a variation on a filename exist within the directory? For example, all files under filename.*? I have been using
Code:
if [ -f filename.* ]
but I've been told that this statement is intended more for verifying the existance of a single file, not a range of files.
... this line will hold true if any file in the directory starts with "filename." so you will actually see what you're looking for if at least one of them exists in the directory ... verify this with ...

Code:
root_box:/tmp # touch filename.1 filen.1 filename.3 file.3 file4
root_box:/tmp # ls file*
file.3      file4       filen.1     filename.1  filename.3
root_box:/tmp # if [ -f filename.* ]
> then
>     ls filename.*
> else
>     ls           
> fi
filename.1  filename.3
root_box:/tmp # rm filename.1 filename.3
root_box:/tmp # ls
file.3          filen.1         orbit-root      speckeysd.lock
file4           mpHyay4c        plugtmp
root_box:/tmp # ls file*
file.3   file4    filen.1
root_box:/tmp # if [ -f filename.* ]
> then
>     ls filename.*
> else
>     ls           
> fi
file.3          filen.1         orbit-root      speckeysd.lock
file4           mpHyay4c        plugtmp
root_box:/tmp #
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0