LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-07-2009, 10:28 AM   #1
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Rep: Reputation: 16
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!
 
Old 10-07-2009, 10:32 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Code:
cat "$(locate sqlnet.ora)"
 
Old 10-07-2009, 10:35 AM   #3
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thank you. Could you explain that? When do I consider that I should use redirection or piping or in this case expressions?
 
Old 10-07-2009, 10:56 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mortonmorton View Post
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.

Last edited by catkin; 10-07-2009 at 11:02 AM.
 
Old 10-07-2009, 11:05 AM   #5
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
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?
 
Old 10-07-2009, 11:39 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mortonmorton View Post
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
 
Old 10-07-2009, 12:07 PM   #7
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
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 =)
 
Old 10-07-2009, 12:55 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mortonmorton View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Redirection question jcllings General 5 08-26-2009 10:10 AM
Redirection question ShaqDiesel Linux - General 3 01-06-2009 05:41 PM
Question in process redirection syfung Programming 1 09-20-2008 06:56 PM
Apache redirection question... WeNdeL Linux - Software 2 04-01-2004 10:48 AM
Redirection question... Zeracles *BSD 0 02-21-2004 11:11 AM


All times are GMT -5. The time now is 09:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration