(SOLVED)Help creating alias

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,567
Reaction score
1,284
Credits
11,462
Hi everyone! Hope you're ll having a nice life! :)

I have a bunch of aliases, I know how to create an alias. Thing is, I've been trying to have an alias for
Code:
apt-cache search pkg_name | sort | less
I tried different ones but none work as expected. For example, I tried
Code:
alias cearch="apt-cache search $2 | sort | less"
where $2 is pkg_name but this search the entire cache not only pkg_name ... hmmm ... I tried a few other variants but can't remember now, however, none of them work as expected. Is there a way to get it done? It can be a function too, which I also tried with the same results. Thanks in advance for your answers.
 


I don't think you can do it with an alias.
But it should work with a function:
Bash:
function cearch()
{
    apt-cache search "$1" | sort | less
}

But that uses apt-cache search to search in package names AND descriptions.
Also note, we're using $1 - NOT $2. Because the package-name/search-pattern will be the first parameter to the function. $0 would be the function name, $1 would be the parameter.

If you want to limit the search to package names, then add the --names-only parameter to the apt-cache search command.
e.g.:
Bash:
function cearch()
{
    apt-cache search --names-only "$1" | sort | less
}

And to use the function, simply:
Bash:
cearch patternToSearch
e.g.
Bash:
cearch vim

And depending on which version of the function you decided to use that would output either:
Version 1 - The names of ALL packages which include the string "vim" in the package name OR the description
Version 2 - The names of ALL packages which include the string "vim" in the package name.


OR - another option would be to do this:
Bash:
function cearch()
{
    apt-cache search "$@" | sort | less
}
That will pass ALL parameters that were passed to the cearch function to the apt-cache search command.

That way, you can use it like this to do a search in package names AND descriptions:
Bash:
cearch vim

Or to only search in package names:
Bash:
cearch --names-only vim

So you could send different parameters to cearch, depending on what you want apt-cache search to do.
 
Last edited:
I don't think you can do it with an alias.
But it should work with a function:
Thanks but it doesn't work, I get "E: at least one search pattern must be provided", however, someone in another forum told me to use it in a script, and it works:
Code:
#!/bin/bash
apt-cache search $1 | sort | less
Not sure why but it works as expected. Do you know why? Also, if I try calling the script from a function, I get the same "one pattern" error mentioned above.
EDIT: I tried placing the script in /usr/local/bin, but got the "one patter" error and "no such file or dir" error too. So it seems I have to keep it in $HOME and run it like ./cearch every time I want to use it(sight).
 
Last edited:
Thanks but it doesn't work, I get "E: at least one search pattern must be provided", however, someone in another forum told me to use it in a script, and it works:
Code:
#!/bin/bash
apt-cache search $1 | sort | less
Not sure why but it works as expected. Do you know why? Also, if I try calling the script from a function, I get the same "one pattern" error mentioned above.
EDIT: I tried placing the script in /usr/local/bin, but got the "one patter" error and "no such file or dir" error too. So it seems I have to keep it in $HOME and run it like ./cearch every time I want to use it(sight).
Well, that's very odd.
All of the functions I posted work perfectly well for me in bash on Debian.
So if you put one of those functions into your .bashrc it should work!
Also in my functions, I double quoted the variables. This is important. If you didn't double quote the variables in the functions, that might possibly be part of the problem.
But the functions definitely work!

What search patterns were you using when you called the function? Maybe there is something else that is going on? Or perhaps something that I've missed?
 
So if you put one of those functions into your .bashrc it should work!
Hmmm... I have a functions.sh file in $HOME in a bin folder with all the functions to run my VMs. I use qemu from the command line so it's easier and more efficient doing it this way rather than typing the same commands over and over, so I put the function there, however, just tried with .bashrc and got the same "E: you must provide at least one search parm" error. I'm in Debian testing by the way, don't know it that matters or makes any difference, does it? I don't think it does, I can't get this done in stable either. :(
 
Placing the script in $HOME/bin does work, as long as I don't remove the .sh extension, if I do, then I get the "one parm" error, otherwise running
Code:
cearch.sh pkg_name
works as expected.
 
Placing the script in $HOME/bin does work, as long as I don't remove the .sh extension, if I do, then I get the "one parm" error, otherwise running
Code:
cearch.sh pkg_name
works as expected.
Again - that's really weird. If you have the shebang there, you don't need to use the .sh at the end either.
Maybe it's something to do with your environment?!
 
Again - that's really weird. If you have the shebang there, you don't need to use the .sh at the end either.
Maybe it's something to do with your environment?!
Don't know. This is the output from
Code:
echo $PATH
/home/tolkem/bin:/home/tolkem/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Does that look ok? I've never messed with that so, except for $HOME/bin, it's the default I think.

EDIT: I tried something; re-wrote the script but this time I didn't add the .sh extension, place it in $HOME/bin, and now it works. It occurred to me that might work since I have a few scripts in there with no .sh extension and they all work. :)
 
Last edited:

Members online


Latest posts

Top