LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-29-2011, 06:22 PM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
BASH: 'ls' alias gives the opposite result.


I've tried it as an alias (the 'x' is for 'except')
Code:
alias lx='ls --ignore='$1''

alias lx='ls --ignore=*$1'

alias lx='ls --ignore="*\$@"'

alias lx='ls --ignore="*$1"'
and as a function with this and other details
Code:
function lx() { 
ls --ignore='$1' 
;}
When I type in a command such as
Quote:
lx *.jpg
in a folder that contains more JPEG files than files of other types, I get a stdout that's no different than if I'd entered ls *jpg.

This is not what I'm looking to make a shortcut for.

What could I be doing wrong?

BZT
 
Old 10-29-2011, 11:27 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267
Your problem is that an alias is not a shell function or program invocation, and does not perform parameter substitution. Your "*.jpg" argument gets expanded by the shell and appended to the command line after the alias substitution is done. You can get what you want if you write the alias definition and invocation as
Code:
alias lx="ls --ignore"
lx \*.jpg
which will become
Code:
ls --ignore \*.jpg
Note that the "*" is escaped so that it gets passed literally to the 'ls' command, and the "--ignore" flag and its argument are passed as two separate arguments rather than in a single argument with an "=" separator.

When you are pushing against the limits of what you can do with "alias", you are better off coding what you want as a simple shell function. You could get the behavior you wanted by adding this to your .bashrc file
Code:
function lx() { ls --ignore=$1; }
You do still have to escape any shell meta-characters in the ignore pattern. You can generally get away without that when invoking the 'ls' command directly because the pattern "ignore=*.jpg" is unlikely to match any files in the current directory (unless you are in the habit of creating files with names that begin with "ignore=").
 
1 members found this post helpful.
Old 10-30-2011, 02:33 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You can also use single quotes instead of escaping.
 
1 members found this post helpful.
Old 11-01-2011, 05:47 PM   #4
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Original Poster
Rep: Reputation: 15
I tried your suggestion, and added lx as a function to my .bashrc. It works.

I guess I was hoping to create an alias, or command shortcut of some kind, that didn't require any special treatment of the pattern to be hidden or ignored. That would probably take a whole script (and probably not in bash) or something more sophisticated, like writing and compiling a variation of ls that had something similar to grep's "-v" option built into it.

I also played around a bit with the "--hide" option of ls, after I made the first post to this thread, but as the results were just about the same -- and now I know the reasons why -- I figured I'd just wait for replies here before continuing.

Thanks for the help so far, folks.

BZT
 
Old 11-03-2011, 05:08 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
function lx() { 
ls --ignore='$1' 
;}

$ lx *.jpg
This fails for two reasons. First, you hard-quoted the variable, so ls sees only the literal pattern '$1'. You need to use double-quotes to let the variable expand.

Second, the shell expands globbing patterns before passing them to the command. so your function (after fixing the above) would actually execute as:

Code:
ls --ignore="file1.jpg file2.jpg file3.jpg"
You have to quote or escape the globbing pattern on the command line so that it gets passed literally.

( By the way, you should use either function lx, or lx(), but not both together. Bash ignores the error, but it's still bad syntax. Posix specifies funcname() only. Also, the ; at the end is only needed to separate the closing bracket from a previous command on the same line. When the bracket is on a line of its own, the semicolon before it causes a syntax error. )

Code:
lx() { 
	ls --ignore="$1" 
}

$ lx "*.jpg"
Another possibility would be to build the globbing pattern into the function itself.

Code:
lx() { 
	ls --ignore="*$1*" 
}

$ lx .jpg
Note that the first option is more flexible, as it allows you to pass any globbing pattern to the ignore list, whereas the second one will only work reliably for literal text patterns. Also note that the function as written will only accept a single argument, and that you can't pass any other options to it.

A more robust function would look something like this:

Code:
lx() {
	local x="$1" ; shift
	ls --ignore="*$x*" "$@"
}
This allows other options to be passed to the command too. Note though that the ignored pattern must still always be the first argument given.

It might do you better in the long run to just learn how to use globs more effectively

http://mywiki.wooledge.org/glob
http://www.linuxjournal.com/content/...ended-globbing

Last edited by David the H.; 11-03-2011 at 05:14 AM. Reason: made x local, plus added last comment
 
  


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 -D (or bash --dump-strings) unexpected result Ubx Linux - General 2 02-24-2011 07:41 PM
[SOLVED] grep result for if clause (bash) CRUDE Programming 3 10-31-2009 12:51 PM
bash result to PHP eco Programming 3 05-18-2009 07:50 PM
[bash] how to put nslookup result next to ip in file kriezo Programming 5 02-25-2008 01:48 AM
BASH Command Result redhatnoob Programming 2 08-19-2004 07:25 PM


All times are GMT -5. The time now is 12:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration