Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Help creating an alias

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
Moltke
Posts: 41
Joined: 2017-03-16 18:10

Help creating an alias

#1 Post by Moltke »

Hi everyone! Hope you're ll having a nice life! :)

First, sorry if this isn't the right place to post this. 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: Select all

apt-cache search pkg_name | sort | less
I tried different ones but none work as expected. For example, I tried

Code: Select all

alias cearch="apt-cache search $2 | sort | less"
Where $2 is pkg_name is this ok? This returns "no such pkg or dir" ... 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: don't work. Thanks in advance for your answers.

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Help creating an alias

#2 Post by Dai_trying »

create a bash script (called "cearch" for your example) and place it in /usr/local/bin/ and it will work.

example bash script

Code: Select all

#!/bin/bash
apt-cache search $1 | sort | less
P.s. dont forget to make it executable (chmod +x)

Moltke
Posts: 41
Joined: 2017-03-16 18:10

Re: Help creating an alias

#3 Post by Moltke »

Dai_trying wrote:create a bash script (called "cearch" for your example) and place it in /usr/local/bin/ and it will work.

example bash script

Code: Select all

#!/bin/bash
apt-cache search $1 | sort | less
P.s. dont forget to make it executable (chmod +x)
Thanks, that works. Do you know why it works as a script but not as a bash function or alias? If I used that as an alias or function, which I did, I get this error "need to provide at least one search parm", and using $2 instead of $1 search the entire cache not only pkg_name, however, the script just works.

EDIT: If I place the script in /usr/local/bin, it doesn't work.

peter_irich
Posts: 1405
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Help creating an alias

#4 Post by peter_irich »

Write all commands in the script with full path, for example, not apt-cache but /usr/bin/apt-cache.
All commands except builtin, for example, cd is the builtin command.

Peter.

Moltke
Posts: 41
Joined: 2017-03-16 18:10

Re: Help creating an alias

#5 Post by Moltke »

peter_irich wrote:Write all commands in the script with full path, for example, not apt-cache but /usr/bin/apt-cache.
All commands except builtin, for example, cd is the builtin command.

Peter.
Thanks for the reply. I followed your suggestion, but it doesn't work if I move the script to /usr/local/bin, get the same errors, it does work if run from $HOME.

peter_irich
Posts: 1405
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Help creating an alias

#6 Post by peter_irich »

I don't know. On my PC this script with full pathes works from /usr/local/bin.

Peter.

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Help creating an alias

#7 Post by Dai_trying »

Moltke wrote:Thanks, that works. Do you know why it works as a script but not as a bash function or alias?
I think it is either something to do with passing a parameter or a problem with pipes, not too sure really, either way it's just much easier (IMO) to throw a script in a local bin. I usually have one in my $HOME folder that I use for personal scripts.
Moltke wrote:EDIT: If I place the script in /usr/local/bin, it doesn't work.
I am guessing this would be a problem with the way you have your $PATH configured (or not as the case may be), but using the full path to the command like @peter_irich suggested should solve that for you. You could check (and edit if needed) your $PATH to see where the most suitable place for your personal scripts would be if you don't want to use full paths in them.

Moltke
Posts: 41
Joined: 2017-03-16 18:10

Re: Help creating an alias

#8 Post by Moltke »

Dai_trying wrote:I usually have one in my $HOME folder that I use for personal scripts.
So do I and 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: Select all

cearch.sh pkg_name
works as expected.

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Help creating an alias

#9 Post by Dai_trying »

Moltke wrote: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: Select all

cearch.sh pkg_name
works as expected.
I can't see why you get any error by changing the name of the file from cearch to cearch.sh, when I tested it I didn't use any extension as I figured that a script which is going to be used as a command shouldn't really have one (IMO), thinking as I type, I guess it is because still have the alias in force and it is being called first.

Moltke
Posts: 41
Joined: 2017-03-16 18:10

Re: Help creating an alias

#10 Post by Moltke »

Dai_trying wrote:
Moltke wrote: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: Select all

cearch.sh pkg_name
works as expected.
I can't see why you get any error by changing the name of the file from cearch to cearch.sh, when I tested it I didn't use any extension as I figured that a script which is going to be used as a command shouldn't really have one (IMO), thinking as I type, I guess it is because still have the alias in force and it is being called first.
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. :)

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Help creating an alias

#11 Post by Head_on_a_Stick »

Moltke wrote:I tried different ones but none work as expected. For example, I tried

Code: Select all

alias cearch="apt-cache search $2 | sort | less"
Where $2 is pkg_name is this ok?
No, a positional parameter doesn't work with an alias.

Try a function instead:

Code: Select all

cearch() {
   apt-cache search "$@" | sort | less
}
Note also that the script contains no bashisms so a POSIX sh shebang (ie, #!/bin/sh) should probably be preferred because it's lighter, faster and less buggy than bash. Not particularly important for such a simple wrapper but the principle stands nevertheless.
deadbang

peter_irich
Posts: 1405
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Help creating an alias

#12 Post by peter_irich »

Or by alias call the script, for example, in my .bashrc

Code: Select all

alias dict="/usr/local/bin/dict_p"
and dict_p is

Code: Select all

/usr/bin/dict $1 | /usr/bin/less -n
Peter.

shep
Posts: 423
Joined: 2011-03-15 15:22

Re: Help creating an alias

#13 Post by shep »

If you have a series of scripts, I find YAD to be very helpful in organizing the scripts:
https://packages.debian.org/buster/yad

Example that organizes abook folders:
http://daemonforums.org/showpost.php?p= ... ostcount=4

Here is another one that organizes Weather products from my local Airport
http://daemonforums.org/showpost.php?p= ... ostcount=6

Post Reply