Referencing variables in commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Referencing variables in commands
# 1  
Old 03-30-2005
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  
Old 03-30-2005
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  
Old 03-30-2005
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  
Old 03-31-2005
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 #

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to put variables commands in config files?

Hi All, Seeking for your assistance on how to put in variables all the commands in /bin config files: /home/test/config_file/config.cfg cat /home/test/config_file/config.cfg ECHO=/bin/echo LS=/bin/lsMain script cat test.sh source=/home/test/config_file/config.cfg ECHO=$ECHO LS=$LS#i... (3 Replies)
Discussion started by: znesotomayor
3 Replies

2. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

3. Shell Programming and Scripting

Send Remote Commands via SSH with variables

Hi there I found the Command to send commands to other servers like: sv01> ssh user@sv02 'ps -ef' But I cant use Variables from a script i want to execute on another server like: sv01> ssh user@sv02 'cd $SCRIPTHOME' although the variable is set on sv01. How can I run commands on sv02 with... (2 Replies)
Discussion started by: DarkSwiss
2 Replies

4. Shell Programming and Scripting

Piping through commands read as variables from file

This is going to be part of a longer script with more features, but I have boiled it down to the one thing that is presently stumping me. The goal is a script which checks for updates to web pages that can be run as a cron job. The script reads (from a tab-delim file) a URL, an MD5 digest, and an... (1 Reply)
Discussion started by: fitzwilliam
1 Replies

5. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies

6. Shell Programming and Scripting

Storing commands in $variables.

Hi I'm trying to store commands in variables... like so.. # lastcmd=" $t1 | $t2 | $t3 | $t4 | sort | uniq" t1="sed -e 's/http:/<li><a href=\"http:/'" t2="sed -e 's/http:.*/&\">&<\/a>Web Link<br>/'" t3="sed -e 's/.*. mailto:/<li><a href=\"mailto:/'" t4="sed -e... (7 Replies)
Discussion started by: Paulw0t
7 Replies

7. Shell Programming and Scripting

Using variables in sed commands

Hi there, I need to be able to put the hostid of my box into a file (replacing the text "enter_hostid_here" so i tried sed -e 's/enter_hostid_here/`hostid`/g' inputfile > outputfile but it takes the `hostid` literally as text .....how can I get this info into the file (ideally in a single... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Answers to Frequently Asked Questions

Passing variables/arguments/parameters to commands

A good place to start is simple variable passing.... Passing variables from one script to another The next level is passing a variable into a more complex command such as using a variable in a sed command. There are some simple quoting techniques that are very general. These are mentioned... (0 Replies)
Discussion started by: Perderabo
0 Replies

9. UNIX for Dummies Questions & Answers

Subtracting Variables which are commands

I have this idea. I have a variable for the start of someones log in time, (start=`who am i | cut -c30-34`) and a variable for the log out time or present time, (end=`date | cut -c12-16`) but how do I go about subtracting them to get the total log in time. I've tried adding a another variable... (2 Replies)
Discussion started by: Astudent
2 Replies

10. UNIX for Dummies Questions & Answers

Subing Variables with commands.

Hi, I'm back! Ok, I'm trying to use a variable with a value of a unix command. So when I try a=`ls`, then echo $a I get the correct answer. But when I try it with a pipe: a=`ls | wc -c`, then echo $a I get: filename: a: command not found I've tried so many ways I presumme it's the pipe... (1 Reply)
Discussion started by: Astudent
1 Replies
Login or Register to Ask a Question