LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2012, 01:48 PM   #1
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Rep: Reputation: 64
bash redirection help


The 'unrar' program has an option to list files in an archive, and print those filenames to stdout:

Code:
unrar lb archive.rar
I want to redirect the stdout (the filename's) to a text file, which isnt the problem.

The problem is that the 'unrar lb' and redirection need to go into an alias, but the archive name appended to the alias wont send the filenames to the text file.

Last edited by clifford227; 07-27-2012 at 01:50 PM.
 
Old 07-27-2012, 02:18 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Maybe try using arguements? I'm not sure if I understand completely but I think what you are trying to do is this:

Code:
function listarchive() { echo "Listing Contents Of Archive $1" >> /tmp/archivelist.txt;  /usr/bin/unrar lb "$1" >> /tmp/archivelist.txt ;}
Put that in your .bashrc file and then you can just call "listarchive archivename"
 
Old 07-27-2012, 04:16 PM   #3
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
Yes that works,

Thanks
 
Old 07-28-2012, 09:57 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
The best way to create arbitrary commands is with a shell function or stand-alone script. But actually this should be possible with an alias as well, within certain limitations.

To start with we need to understand that redirections are among the first things parsed by the shell, and are set up and removed from the line before any parsing is done on the actual command. This means that you can insert them pretty much anywhere in the command line, not just at the end.

So if you used a fixed filename for the redirection, then your alias could include it anywhere with the same outcome. These two example aliases act the same if you run the command as "lstofile *.txt", for example.

Code:
alias lstofile='ls >outfile.txt'
alias lstofile='>outfile.txt ls'
But this of course doesn't allow you to set up a dynamic name for the output file. For that, you also have to understand exactly how aliases work.

The secret is that an alias is simply a command name substitution. The shell looks at the first word of the command, and if it matches a preset alias name, the alias contents are substituted at that point. Anything after that in the command just remains tacked on to the end.

So to include a filename in the redirection, you could do something like this:

Code:
lstofile='ls >'
And run the command with the output file as the first argument:

Code:
lstofile outfile.txt *.txt
The first word, "lstofile", will be replaced with "ls >", and your command will be executed as "ls > outfile.txt *.txt", which should work just fine, due to the first point above.


But really, just use a function or script for something like this.

Last edited by David the H.; 07-28-2012 at 10:00 AM. Reason: small rewording
 
2 members found this post helpful.
Old 07-29-2012, 05:28 AM   #5
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
Thanks for that explaination David
 
Old 07-30-2012, 11:39 AM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
I don't think this could be achieved through an alias, can you pass multiple commands and arguments to an alias?
 
Old 07-30-2012, 02:36 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Umm...in case you missed it, I wrote a long post just above explaining exactly how aliases and redirections function, and how it could be done, as long as the requirements aren't too complicated and you're willing to accept the restricted syntax.

Again, an alias is not a command. You don't "pass" arguments or commands "to" it. It's just a shell feature that modifies the commands you type, by substituting one text string for another before executing them.

If you're clever it's possible to design and execute quite intricate compound commands that way. But what you can't do is arbitrarily rearrange arguments on the line, since nothing except the substituted command word is ever touched.


I did also say that, for complex commands it's usually better to use a function or stand-alone script. Then you can define your inputs and outputs to work the way you want them to.
 
2 members found this post helpful.
  


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
BASH I/O Redirection on Solaris? jcllings Solaris / OpenSolaris 7 08-24-2009 02:03 PM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 07:50 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 12:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 06:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration