Standard output redirection from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Standard output redirection from a variable
# 1  
Old 10-31-2008
Standard output redirection from a variable

Hi, trying to store a comand involving a redirection in a variable and then run this variable. But the redirection gets lost.

Ex:
#!ksh
MYCMD="ls -l > dirlist.txt"
$MYCMD

This runs the command but displays the result in the terminal instead of redirecting it to the text file.

Can this be done?
# 2  
Old 10-31-2008
MYCMD="ls -l"
$MYCMD > dirlist.txt

Last edited by Bandit390; 10-31-2008 at 11:58 PM..
# 3  
Old 11-01-2008

Code:
MYCMD="ls -l > dirlist.txt"
eval "$MYCMD"

# 4  
Old 11-02-2008
here's what I'm trying to do:
Quote:
#!/bin/bash
rootFolder="/cygdrive/d/users/xxx/tmp" #
folders="OutputDir1 OutputDir2 OutputDir3" #
count=0 #
countBcp=0 #
for folder in $folders #
do #
count=`expr $count + 1` #
echo "Folder $count: $folder" #
cd "$rootFolder/$folder" #
pwd #
countBcp="ls -l -d ./bcpFiles* | wc -l" #
eval $countBcp #

i = 0
while $i -lt $countBcp << Problem I can't really compare against $countBcp
do
# do something
done
done
#
# 5  
Old 11-03-2008
To Bandit390: Simple and effective - sometimes simple things are just too obvious to be seen.
To cfajohnson: This does the trick. I never used the 'eval' command. I just learned something.
Now my biggest problem is to decide which solution I will use Smilie
Thank you very much to both of you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Capture Multiple Lines Into Variable As Of Standard Output

Hello All, I have the below script and output. cat test.sh #!/bin/bash -x logit() { echo " - ${*}" > ${LOG_FILE} } LOG_FILE=/home/infrmtca/bin/findtest.log VAR=`find . -type f -name "*sql"` logit $VAR Output: cat /home/infrmtca/bin/findtest.log -... (9 Replies)
Discussion started by: Ariean
9 Replies

2. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

4. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

5. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

6. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

7. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

8. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

9. Shell Programming and Scripting

permanent redirection of standard input

while running a user inter-active program how can we get the commands from a file instead of the user? is there anyway to permanently redirect content of a file to standard input? (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

10. Shell Programming and Scripting

Standard output and redirection

Hello, Is is possible to redirect stdout to a file as well as to the console/screen or display in ksh. any thoughts suggestions/input is appreciated. Thanks. (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question