LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-16-2011, 03:49 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
How to use variable to replace '&' ?


I want to replace "&1" with $a, how to do it ?
Code:
[river@localhost te]$ a="&1"
[river@localhost te]$ echo "ls\n" >$a                     ??no display??
[river@localhost te]$ ls
&1
[river@localhost te]$ echo "ls\n" >&1                     !!normal now!!
ls\n
Thanks!!
 
Old 08-16-2011, 04:50 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
It't not clear to me what do you want to do. I'll explain some things that I think might be of any use to you.

First, to assign the output or a given command to a variable you should be using this syntax:

Code:
# those are backticks, not regular single quotation marks
var=`command`
# or
var=$(command)
> is used to redirect STDOUT to al alternate file, so when you use ">$a" what you are doing is redirecting the output of the command

Code:
echo "ls\n"
To a file called '&1', which is the contents of the variable '$a'. That in place will just put the sting 'ls\n' into the file called '&1'.

Redirecting something to &1 is redirecting it to STDOUT, so you are really doing nothing there. Your command, for most purposes is just the same than plainly doing

Code:
echo "ls\n"
Since &1 is the default anyway.
 
0 members found this post helpful.
Old 08-16-2011, 06:02 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks, I want to use $a to represent a output path, maybe a=&1, or a=&2, or $a is evaluated a file name.
Code:
[river@localhost te]$ a="&1"
[river@localhost te]$ echo "ls\n" >$a
I want to redirect "ls\n" to stdout, and I want $a is equal to "&1", but shell determine "&1" as a file, how to resolve it ?
 
Old 08-16-2011, 06:14 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
You might be confusing terms, so, instead of trying to give a technical description on what do you think you are doing, try to express what do you want to achieve in plain words.

Quote:
Originally Posted by 915086731 View Post
Thanks, I want to use $a to represent a output path, maybe a=&1, or a=&2, or $a is evaluated a file name.
Well, if you want it to hold a path then I still can't guess why are you assigning '&1' to it. Outside it's use with the redirection operator, the strings '&1', '&2'... have no meaning in shell scripting. If what you are after is parametrization, then use $1, $2, etc. That's the way to address positional parameters in a shell script.

Code:
[river@localhost te]$ a="&1"
[river@localhost te]$ echo "ls\n" >$a
Again, I don't think you want to do that. I already told you above that, what this command does, is to put the string 'ln\n' inside a file called '&1'.

Quote:
I want to redirect "ls\n" to stdout, and I want $a is equal to "&1", but shell determine "&1" as a file, how to resolve it ?
[/quote]

To get the output of 'ls' into stdout, all you need to run is 'ls', nothing else.

If you want to get the output of 'ls' into another file, then you just redirect the output of 'ls' (not the output of 'echo') to that file

Code:
ls > myfile.txt
If you want to give the file name as a parameter in command line you'd use something like:

Code:
#!/bin/bash
# This is myscript.sh
myfile="$1"
ls > "$myfile"
Then use it like this:

Code:
$ ./myscript.sh "/path/to/myfile.txt"

Last edited by i92guboj; 08-16-2011 at 06:15 AM.
 
Old 08-16-2011, 06:52 AM   #5
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Sorry , my English is very bad.
Here is the part of my code:
Code:
 [ "$1" = "stdout" ] && a="&1"
 [ "$1" = "stderr" ] && a="&2"
 a="$1"
echo "ls" >"$a"
I want to use $a to control where the echo redirect to .
 
Old 08-16-2011, 09:04 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Ok, but, do you want the string "ls" printed to STDOUT/STDERR? Or is it the output of the "ls" command what you want to print to STDOUT/STDERR?

Isn't it a simple
Code:
ls > "$a"
what you are after? If not, then try to describe why. I don't think your English is that bad, I am not a native speaker either.
 
Old 08-16-2011, 09:23 AM   #7
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks,
I just want to redirect some message to stdout, and let a="&1" to represent stdout.
Quote:
ls >"$a" !!shell regard &1 as a file, not stdout
How to let shell regard the value $a as stdout, not a file name ?
 
Old 08-16-2011, 09:37 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Ah, ok. Enclose it on eval. Like this:

Code:
eval "ls >$a"
This way the whole sentence is evaluated for var substitution before it's ran. Look into the bash man page for "eval", or even the standalone "eval" tool man page.
 
1 members found this post helpful.
Old 08-18-2011, 12:45 AM   #9
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks , I got it.
 
Old 08-18-2011, 02:30 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There's no need to use eval, as usual.

According to the bash manual, redirection duplications are in the following pattern:
Code:
 [n]>&word
...where word should evaluate into a file descriptor digit.

So to redirect to a different file descriptor, try this instead:

Code:
fd=1
command >&$fd
And to redirect to a file, simply define a file descriptor for it first.

Code:
exec 4>file.txt
fd=4
command >&$fd
It works in the tests I did.

BTW, sometime around bash 4.1 they seem to have added the ability to use variables on the left side too, in the form of {var}>fd (no "$"). But either I'm not understanding it right, or my version of bash (4.1.5(1)-release) doesn't have it yet, or something, because it doesn't seem to work for me.

~~~~~~
Edit: Aah, I've figured it out.

fd=4
exec {fd}>file.txt
command >&$fd
~~~~~~

Edit 2:
Stupid me...After getting some sleep, it suddenly dawned on me that I hadn't actually figured it out. I was still using $fd as the target descriptor too. I'd only managed to get something that didn't generate an error message.

So frankly, I can't quite understand what this new syntax it's supposed to. The Reference Guide says this:
Quote:
Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than 10 and assign it to {varname}. If >&- or <&- is preceded by {varname}, the value of varname defines the file descriptor to close.
And I found a changelog that puts it this way:
Quote:
If the optional left-hand-side of a redirection is of the form {var}, the
shell assigns the file descriptor used to $var or uses $var as the file
descriptor to move or close, depending on the redirection operator.
I'm still not sure what it really means. It seems to be a way for it to automatically generate an fd, that you can then easily close again afterwards. But it's not clear to me how it's supposed to be used, or whether this means you can also use it to assign an fd to the left side through the variable.

Well, at least the first part of my post is valid.

Last edited by David the H.; 08-18-2011 at 04:18 PM. Reason: as stated
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Find variable text between keywords & replace w/ "term_<variable_text>_term" dsayars Programming 6 07-14-2010 11:05 PM
replace filename with variable Siljrath Programming 2 11-12-2009 12:40 AM
search and replace with value of variable say_hi_ravi Programming 10 08-28-2009 02:39 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration