Piping to a file and setting filename using a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Piping to a file and setting filename using a variable
# 1  
Old 09-13-2008
Piping to a file and setting filename using a variable

Hi all,

I would like to send the output of a line in a ksh script to a file, but I need to name the file using a predefined variable:

ls -l > $MYVAR.arc

But what is the correct syntax for achieving this? I can't seem to find the correct syntax for giving the file an extension.

Any assistance is much appreciated.

Cheers
# 2  
Old 09-13-2008
what you have should work assuming that MYVAR is set.

Code:
MYVAR=myfilename
ls -l > $MYVAR.arc

This will create myfilename.arc
# 3  
Old 09-13-2008
Hi Frank, thanks for the reply.

The variable is set, but the file gets created without the extension
# 4  
Old 09-13-2008
Try
Code:
ls -l > ${MYVAR}.arc

or
Code:
ls -l > $MYVAR".arc"

Regards
# 5  
Old 09-13-2008
You might want to ensure that you don't have a hidden newline character at the end of your variable...
# 6  
Old 09-15-2008
hi guys, thanks for the responses.

The below works fine with a simple command such as ls -l:

Code:
ls -l > ${MYVAR}.arc

But when I attempt to send the output of an awk command to a similarly named file, the file is created, but it is empty:

Code:
 
awk -v s=$string 'BEGIN{FS=OFS="|"}!/T00:00:00/{$NF=s}1' *.arc > ${FILENAME}.arc

The same happens using $FILENAME".arc"

Any ideas why this is happening?

Thanks all
# 7  
Old 09-15-2008
do you get any output of your remove the redirection?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help piping a value to a variable

Hey Guys. Below is the code I am using to pipe a value into a variable. When I tried it says the following xargs: :HEX:: No such file or directory Not sure what it means. Can anyone please help me figure this out. Below is the code I am using. (It echo's out just fine) sed -n... (1 Reply)
Discussion started by: eldan88
1 Replies

2. UNIX for Advanced & Expert Users

Problem piping find output to awk, 1st line filename is truncated, other lines are fine.

Today I needed to take a look through a load of large backup files, so I wrote the following line to find them, order them by size, and print the file sizes in GB along with the filename. What happened was odd, the output was all as expected except for the first output line which had the filename... (4 Replies)
Discussion started by: gencon
4 Replies

3. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

4. Homework & Coursework Questions

Matlab help! Reading in a file with a variable filename

1. The problem statement, all variables and given/known data: I want to read in a file, and plot the data in matlab. However, I do not like hardwiring filenames into my codes, so I always give the user the option to specify what the filename is. I am pretty inexperienced with matlab, so I have no... (0 Replies)
Discussion started by: ds7202
0 Replies

5. Shell Programming and Scripting

Setting Environment variable from value in file

I've searched Google and now this forum. Best guess is my search fu is not good (and it probably isn't). The Google search did bring me here. Background I have a number of Korn Shell scripts who all use one of 3 values for an environment variable used in the backup system. On occasion one or... (8 Replies)
Discussion started by: WolfBrother
8 Replies

6. Shell Programming and Scripting

Help setting variable from file contents with spaces

I suppose the easiest way to explain my problem is to show you some code and then show you what the code outputs: -------------------------------------------------- #!/bin/bash echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt" export Foo=$(cat "/tmp/This Filename Has... (4 Replies)
Discussion started by: nrogers64
4 Replies

7. Shell Programming and Scripting

setting a variable by searching within a file

Hi, I am trying to set a variable to be used in later scripting, and am having some difficulty. I want to look in a file called scan.info and find the line that says "variable ok". Then I want to cut the number at the beginning of that line and assign that number as a variable so that later... (4 Replies)
Discussion started by: garth6@hotmail.
4 Replies

8. Shell Programming and Scripting

Setting Variable to oldest file in a directory

I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using $sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this $./script.sh <directory>, it... (5 Replies)
Discussion started by: spaceherpe61
5 Replies

9. Shell Programming and Scripting

piping output from PHP file into variable

Hi. I have a script like so: #!/bin/bash download='php /var/www/last.php' echo $download if $downloadHow do I pipe the output of the php file into a variable, as when i run the if statement, it just echos the file output to the screen and does not actually consider the output (it will be... (2 Replies)
Discussion started by: daydreamer
2 Replies

10. Shell Programming and Scripting

setting file count to a variable

Hey guys. My goal here is to count the number of .dat files in in a directory(28 files). If 28 files exist I am ok. Having trouble doing this. Any help would b e greatly appreciated. #!/usr/bin/ksh #============================================================================= ### Define local... (3 Replies)
Discussion started by: ecupirate1998
3 Replies
Login or Register to Ask a Question