Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
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"
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.