LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Redirection question (http://www.linuxquestions.org/questions/linux-newbie-8/redirection-question-760332/)

mortonmorton 10-07-2009 10:28 AM

Redirection question
 
Hi,
What I'm trying to do is locate the sqlnet.ora file and display its contents, but so far not having success in doing this =)

Code:

[oracle@mortonlx dbs]$ locate sqlnet.ora
/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/network/admin/samples/sqlnet.ora
[oracle@mortonlx dbs]$ cat < locate sqlnet.ora
-bash: locate: No such file or directory
[oracle@mortonlx dbs]$

Could anyone give some tips =) Thanks!

catkin 10-07-2009 10:32 AM

Code:

cat "$(locate sqlnet.ora)"

mortonmorton 10-07-2009 10:35 AM

Thank you. Could you explain that? When do I consider that I should use redirection or piping or in this case expressions?

catkin 10-07-2009 10:56 AM

Quote:

Originally Posted by mortonmorton (Post 3711100)
Thank you. Could you explain that? When do I consider that I should use redirection or piping or in this case expressions?

Surely. The cat command is choice, to copy the contents of a file to stdout (the terminal, by default). Normally you give it the name of the file to copy. In this case you want to generate the name via the locate command. How to pass that name as an argument to cat? By using $( ) which is the evolved version of ` `. When bash finds $( ) it runs the command(s) inside $( ) and replaces the $( ) with the stdout from those commands.

In case the full path of sqlnet.ora contains spaces, it is safest to put it in double quotes: "$( )". This keeps it as a "single word" for cat. Otherwise, if sqlnet.ora was "/ora1/some daft directory name with spaces in/sqlnet.ora" then cat would receive each space-delimited word as a separate argument and would not be able to find /ora/some, daft, directory ... etc.

EDIT:

Redirection is used to read from or write to files (in a broad sense).

Piping is used to send the output of a command to the input of another command. That's an over-simplification but the commonest way to use a pipeline.

mortonmorton 10-07-2009 11:05 AM

Thank you very much for that. So in this case, the use of $() is the appropriate way, and not the use of redirections? Is it not possible using redirections? I'm trying to understand when is redirection applicable over the $() expression. Or will it be just a case to case/ trial and error?

catkin 10-07-2009 11:39 AM

Quote:

Originally Posted by mortonmorton (Post 3711136)
Thank you very much for that. So in this case, the use of $() is the appropriate way, and not the use of redirections? Is it not possible using redirections? I'm trying to understand when is redirection applicable over the $() expression. Or will it be just a case to case/ trial and error?

It depends on the command, whether it is written to receive information in its command line (its argument list) or on stdin.

In this case, cat requires the name of the file(s) in its arguments (ignoring the special case where the file name is not specified and it defaults to reading data from stdin) so, if you are generating the file name with locate, the $( ) is the only solution.

Other commands take the names of the file(s) to operate on from stdin. They are usually designed that way because the argument list might otherwise be greater than the system limit (4096 bytes?). The cpio command is an example/ It expects to receive file paths, one per line, on its stdin; in that case you would use a pipe, generating the path names with find or locate (for example).

Redirections simply read or write to files, most commonly being used to take stdin from a file or to write stdout and/or stderr to a file. You might have a list of files (in a file) that you want cpio to operate on. You could do it with a pipe
Code:

cat my_file_list | cpio ...
but more neatly with redirection
Code:

cpio ... < cat my_file_list
Output redirection is often used for logging
Code:

some commands >> my.log

mortonmorton 10-07-2009 12:07 PM

Thank you very much for the nice explanation. I liked the cat/cpio example, thats just one of the things I get confused on which to consider. I guess I need to read thoroughly first on scripting to have a better understanding =)

catkin 10-07-2009 12:55 PM

Quote:

Originally Posted by mortonmorton (Post 3711189)
Thank you very much for the nice explanation. I liked the cat/cpio example, thats just one of the things I get confused on which to consider. I guess I need to read thoroughly first on scripting to have a better understanding =)

Try the links here.


All times are GMT -5. The time now is 01:14 AM.