linux alias to simplify a command line - linux

I am new to writing Linux scripts (in fact I'm not sure if the proper term is Linux script or baSH script). I do understand source-ing and the alias feature such as this:
alias l='ls -ltra'
What I want to accomplish is type the following shortcut statement
php ~/path/to/longProgram.php Argument1 -x -y -z --long-switch long-switch-value
as this:
lp Argument1 -x -y -z --long-switch long-switch-value
i.e. where I can call lp from any where, and where all of the arguments as-given get passed to longProgram.php. How would I do this as an alias?

To make an invocation of lp Argument1 -x -y -z --long-switch long-switch-value result in a call to php ~/path/to/longProgram.php Argument1 -x -y -z --long-switch long-switch-value, you have a few options. The simplest is to put this in your ~/.bashrc:
lp() { php "$HOME/path/to/longProgram.php" "$#"; }

alias lp='php ~/path/to/longProgram.php'
would normally work, even if it is not a good practice. An alias is simply a find and replace feature so I see no reason of this not working. However you need to be the exact user whose home contain the program.
PS: Also verify that lp is not already assigned to another command.

I just found a way to set an alias command and save it for next time.
nano ~/.bashrc or ~/.bash_aliases
add your aliases in this file
Ex:
alias lp='php ~/path/to/longProgram.php
ctrl+x to save the file, then
source ~/.bashrc or ~/.bash_aliases
You will be able to use the alias command everytime you login as the target user.
Note:
If you want to use ~/.bash_aliases, be sure that the ~/.bashrc file has following commands.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
I prefer using bash_aliases to keep your custom alias clean and management in a centrilized file.
Referece:
https://www.cyberciti.biz/faq/create-permanent-bash-alias-linux-unix/

Related

Aliasing a command and using it in the same line of code

I was wondering if you could alias a command, and use it in the same line of code, see this example:
alias php=/opt/plesk/php/5.6/bin/php; php -v;
I want this to output PHP 5.6.
alias php=/opt/plesk/php/7.3/bin/php; php -v;
and I want this to output PHP 7.3. However, what I get is this:
php -v
# outputs 5.6
alias php=/opt/plesk/php/5.6/bin/php; php -v;
# outputs 5.6
alias php=/opt/plesk/php/7.3/bin/php; php -v;
# outputs 5.6
php -v
# outputs 7.3
I've tried the && operator but it has the same outcome.
I'm wanting to use this in a gitlab continuous integration script, which executes a script through ssh -t by passing a string. However I am calling several php functions and I dont want to paste the full php path every time:
ssh -v -tt $SSH_HOST_NAME "__my_php_commands_here__"
I think the command line is being parsed, and aliases applied, before anything is executed. However, you can do it with shell functions. I don't have PHP, but I have several Perl versions to test with:
$ perl -v |grep version # V
This is perl 5, version 26, subversion 2 (v5.26.2) built for x86_64-cygwin-threads-multi
$ perl(){ /usr/bin/perl "$#" ; } ; perl -v |grep version
This is perl 5, version 26, subversion 3 (v5.26.3) built for x86_64-cygwin-threads-multi
# ^
So defining the pass-through function
perl(){ /usr/bin/perl "$#" ; }
changes how the word perl is interpreted later in the command line. Note that you do need the ; before } — see this answer.
For your use case, I would recommend using a different name to avoid confusion. E.g.:
currphp(){ /opt/plesk/php/5.6/bin/php "$#" ; } ; currphp -v
currphp(){ /opt/plesk/php/7.3/bin/php "$#" ; } ; currphp -v
in a gitlab continuous integration script
Batch non-interactive shells don't support aliases (by default). And that's a good think. alias should be just used as your own, custom shorthand, not in batch scrips.
You could
a) define a function with the same name and use full path for command resolution:
php() { /opt/plesk/php/5.6/bin/php "$#"; }
php -v
Downsides: function are not exported, unless you add export -f php, it is a shell function. Something like xargs php will work incorrectly.
b) use a varaible.
php=/opt/plesk/php/5.6/bin/php
"$php" -v
Downside: you have to modify all scripts and always check out for the $php.
c) Modify the path, so your php is found first. You could create a temporary directory and add it to path:
tmpd=$(mktemp -d)
trap 'rm -r "$tmpd"' EXIT
ln -s /opt/plesk/php/5.6/bin/php "$tmpd"/php
export PATH="$tmpd"/php:$PATH
php -v
If you export PATH correctly, it will work everywhere. Remember to remove the folder tho.
Side note: You don't "alias a variable", you "alias a command". alias allows you to substitute the first (and only the first) word in a simple command. php is a command.
I've went with a .bashrc solution. I initially started with this but for some reason the aliases werent being picked up. It appears you need to set a custom expand_aliases setting.
My .bashrc ended up looking like this:
shopt -s expand_aliases;
alias php=/opt/plesk/php/7.3/bin/php;
alias composer=/usr/lib64/plesk-9.0/composer.phar;
This seemed to do the trick and gave me the correct PHP version while using ssh xx#1.2.3.4 "php -v".

Bash script to go to directory and perform commands gives "Command not found" [duplicate]

This question already has answers here:
How to use aliases defined in .bashrc in other scripts?
(6 answers)
Closed 2 years ago.
My alias defined in a sample shell script is not working. And I am new to Linux Shell Scripting.
Below is the sample shell file
#!/bin/sh
echo "Setting Sample aliases ..."
alias xyz="cd /home/usr/src/xyz"
echo "Setting done ..."
On executing this script, I can see the echo messages. But if I execute the alias command, I see the below error
xyz: command not found
am I missing something ?
source your script, don't execute it like ./foo.sh or sh foo.sh
If you execute your script like that, it is running in sub-shell, not your current.
source foo.sh
would work for you.
You need to set a specific option to do so, expand_aliases:
shopt -s expand_aliases
Example:
# With option
$ cat a
#!/bin/bash
shopt -s expand_aliases
alias a="echo b"
type a
a
$ ./a
# a is aliased to 'echo b'
b
# Without option
$ cat a
#!/bin/bash
alias a="echo b"
type a
a
$ ./a
./a: line 3: type: a: not found
./a: line 4: a: command not found
reference: https://unix.stackexchange.com/a/1498/27031 and https://askubuntu.com/a/98786/127746
sourcing the script source script.sh
./script.sh will be executed in a sub-shell and the changes made apply only the to sub-shell. Once the command terminates, the sub-shell goes and so do the changes.
OR
HACK: Simply run following command on shell and then execute the script.
alias xyz="cd /home/usr/src/xyz"
./script.sh
To unalias use following on shell prompt
unalias xyz
If you execute it in a script, the alias will be over by the time the script finishes executing.
In case you want it to be permanent:
Your alias is well defined, but you have to store it in ~/.bashrc, not in a shell script.
Add it to that file and then source it with . .bashrc - it will load the file so that alias will be possible to use.
In case you want it to be used just in current session:
Just write it in your console prompt.
$ aa
The program 'aa' is currently not installed. ...
$
$ alias aa="echo hello"
$
$ aa
hello
$
Also: From Kent answer we can see that you can also source it by source your_file. In that case you do not need to use a shell script, just a normal file will make it.
You may use the below command.
shopt -s expand_aliases
source ~/.bashrc
eval $command
Your alias has to be in your .profile file not in your script if you are calling it on the prompt.
If you put an alias in your script then you have to call it within your script.
Source the file is the correct answer when trying to run a script that inside has an alias.
source yourscript.sh
Put your alias in a file call ~/.bash_aliases and then, on many distributions, it will get loaded automatically, no need to manually run the source command to load it.

Assigning integer values on the command line terminal to be commands

I'm looking for a way of assigning integers to commands to be run. For example when I type '1' within the command line this launches file x. A '2' launches file y etc. Is there any way of doing this?
I'm using Ubuntu 16.04 :) I'm also looking to make these assignments permament.
You can achieve that with alias. For example:
$ alias 1='echo hi'
1
$ 1
echo hi
Add a command you want to be executed after =. In your case it would be something like this:
alias 1='<PATH_TO_FILE>'
To make aliases permanent add them to your ~/.bashrc, or even
better make your setup more modular and add this to your ~/.bashrc:
############################################################
# alias definitions
############################################################
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
And add your alias definitions to ~/.bash_aliases.

linux: how to execute profile file [duplicate]

This question already has answers here:
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 3 years ago.
My colleague gave me a file containing lots of configs such as
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
How can I use(execute) this profile?
You can load the profile using source command:
source <profile-filename>
eg:
source ~/.bash_profile
For your custom aliases, the best place should be at ~/.bash_aliases. You must just be sure the ~/.bashrc file already contains the following lines:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
or in a more concise manner:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
To load them immediately, source it. Otherwise, aliases will be loaded at every terminal opening. To check, use the alias command without argument.
You can put it in your local .bashrc (or appropriate shell rc file) file if you want it permanently.
cat fileNameProfile >> ~/.bashrc
And for current session:
source ~/.bashrc
If you want it just now, then:
source fileNameProfile
For one time use, copy paste commands to your terminal:-
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
For everytime use, add it in .bashrc.
echo 'alias ll="ls -l"' >> ~/.bashrc
echo 'alias lr="ls -lrt"' >> ~/.bashrc
echo 'alias gv="vim -g"' >> ~/.bashrc
and then source ~/.bashrc
To autoload these alias commands, create a .sample_profile file and add the alias on the file.
after that add this text in .bashrc file
.bashrc
if [ -f ~/.sample_profile ]; then
. ~/.sample_profile
fi

alias in a script

In linux, if I put a command in a script that contains an alias, I see that it doesn't get expanded. How can I fix that? I am using bash.
According the the TLDP page about aliases, you need to use the line shopt -s expand_aliases in your code to expand aliases. The example below produced the expected output, but without the shopt line it just printed "my_ls: command not found":
#!/bin/bash
shopt -s expand_aliases
alias my_ls='ls -lrt'
echo "Trying my_ls"
my_ls
exit
If you want your shell aliases to be available in a script, you must manually include them. If they are defined in ~/.bashrc, put a line
. ~/.bashrc
after the #!/bin/sh line in your script. This will execute the contents of .bashrc in the context of your script.
Enabling posix mode (such as by calling bash as sh or with the command (set -o posix) 2>/dev/null && set -o posix should do the trick.
Even then, be aware that aliases are expanded as they are parsed, and the ordering between parsing and execution is poorly defined. For example
alias foo=echo; foo bar
or
{
alias foo=echo
foo bar
}
will try to run foo as the alias is not defined at parse time yet. Also, some shells parse the whole input of an eval or . (source) before executing any of it.
So the only portable and reliable way to use aliases in scripts is to define them and then eval or . the code that uses them.

Resources