LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-12-2007, 05:47 AM   #1
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Rep: Reputation: 50
.bashrc is there an escape character for alias?


Code:
alias fmt="perl -i -MText::Autoformat -n0777 -e 'print autoformat $_, {all=>1}' $*"
That snippet is copied from:

perldoc -q format

Found in /usr/lib/perl5/5.8.8/pod/perlfaq4.pod
How do I reformat a paragraph?

But it doesn't work in bash, .bashrc

But from the commandline I can run:

Code:
perl -i -MText::Autoformat -n0777 -e 'print autoformat $_, {all=>1}' source_file.txt
which does work. source_file.txt gets formatted, wrapped at 72 chars. per line.

So my question is: Is there a way to get the working command into an alias that works? All of my existing alias on the right of the = begins with ' and ends with '

examples aliases that works:
alias adf='df --human-readable'
alias cmx='chmod u+x'

Thanks.

--
Alan.
 
Old 01-12-2007, 06:07 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
well the $* part doesn't make sense. as you can see in your correct examples, "cmx" doesn't alias to "chmod u+x $*" does it? that might be all you need, if not, you should be able to escape any nasties with a leading \ but if you just run the alias command without any options it'll show you the current alias so you can see if any synax issues have arisen during the alias assignment.
 
Old 01-12-2007, 07:03 AM   #3
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Original Poster
Rep: Reputation: 50
Code:
alias fmt="perl -i -MText::Autoformat -n0777 -e 'print autoformat \$_, {all=>1}' \$*"
That (in .bashrc) works. I'm not sure if it's totally right. But it works.

With Perl and the Text::Autoformat from CPAN Perl module installed that above alias allows me to do:

al@AB60R:~$ fmt junk.txt
al@AB60R:~$

where junk.txt had really long lines in it and now the lines have been formatted and wrapped at 72 characters per line.

Thanks. And, any more help or info on what's going on here with this alias in .bashrc is appreciated.

--
Alan.
 
Old 01-12-2007, 08:16 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
well it'll just be bash itlsef trying to be clever, nothing specific to alias itself. again, the \$* is superfluous.
 
Old 01-12-2007, 01:59 PM   #5
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
Does it need to be an alias? Couldn't you put it into a script called fmt and make fmt executable?
 
Old 01-12-2007, 04:15 PM   #6
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Original Poster
Rep: Reputation: 50
Quote:
Couldn't you put it into a script called fmt and make fmt executable?
Hadn't thought of that. It works.

#!/bin/sh
perl -i -MText::Autoformat -n0777 -e 'print autoformat $_, {all=>1}' $*

That, as a shell script named fmt, works.

al@AB60R:~$ fmt junk.txt

The above command gets junk.txt formatted.

BTW the $* likely may be a Perl special variable so that the junk.txt parameter on the command line provides_ability/is what the Perl command then runs on the file junk.txt

Thanks.

--
Alan.
 
Old 01-12-2007, 07:11 PM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I think if you use single quotes instead of double quotes it'll work.
 
Old 01-12-2007, 07:55 PM   #8
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
Quote:
Originally Posted by acummings
BTW the $* likely may be a Perl special variable so that the junk.txt parameter on the command line provides_ability/is what the Perl command then runs on the file junk.txt
$* in bash contains the current argument list. So, if you pass fmt junk.txt as its argument, $* is junk.txt. I believe perl continued the use of this convention.

Somewhere (I think in the O'Reilly bash book), it says, "$* contains the current argument list. By itself, $* is equivalent to $1, $2, and so on up to the number of arguments. The construct "$*" is equivalent to "$1, $2, ..." which glues all the arguments into a single argument."
 
Old 01-13-2007, 05:58 AM   #9
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Original Poster
Rep: Reputation: 50
Quote:
$* in bash contains the current argument list. <snip> I believe perl continued the use of this convention.
Oh, OK, I get it now. Since is using a bash shell, that is the bash argument list that is passed to Perl in this case.

It's a "command liner" Perl script (meant to be run from the command line -- in a shell, bash in this case).

That's not Perl's argument list. @ARGV is Perl's argument list. Do a www search for: @ARGV

For instance, a different case scenario: if we have a Perl script and in a shell we call_up/execute that Perl_file/script and also at the same time in the shell, call_up/pass an argument list of files then this argument list goes into Perl's @ARGV

Now, whether or not bash's $* gets used in this latter to pass to Perl's @ARGV, I don't know.

But in a Perl file (shebang line: #!/usr/bin/perl) $* I've not ever seen used. Here it is @ARGV

But of course the shell can be used to execute just such a Perl file as well as pass args to the Perl_file/script. But in this case, Perl's argument list is @ARGV

--
Alan.
 
Old 01-13-2007, 06:05 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
$* would be used in a script file. alias is NOT a script, so it means nothing at best.

alias just swaps one string for another before execution. no parameters are passed anywhere, as alias has nothign to do with parameters at all.
 
Old 01-13-2007, 10:20 AM   #11
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
Quote:
Originally Posted by acummings
Oh, OK, I get it now. Since is using a bash shell, that is the bash argument list that is passed to Perl in this case.

It's a "command liner" Perl script (meant to be run from the command line -- in a shell, bash in this case).

That's not Perl's argument list. @ARGV is Perl's argument list. Do a www search for: @ARGV.

[snip]
Yep, you're right. Bash uses $*; perl uses @ARGV, the array of parameters passed in. I guess you can tell I'm more familiar with bash.
 
Old 01-13-2007, 11:56 AM   #12
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
enclosing the commands in back-tics should make it work:
alias fmt="`perl -i -MText::Autoformat -n0777 -e 'print autoformat $_, {all=>1}' $*`"
 
  


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
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 05:57 PM
Escape character ? juanb Linux - Newbie 2 08-31-2004 11:03 AM
Escape character not working in shell script philipz Programming 1 04-29-2004 10:58 AM
alias and .bashrc whaase Linux - General 3 11-02-2003 08:03 PM
alias and bashrc Haldir Linux - Newbie 3 06-14-2002 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:41 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