Using exec command in puppet to create alias in Linux - puppet

Let's say I want to create alias for some command (for example ls command)
So far I've done:
class alias{
exec { "alias1":
command => "alias aliasname='ls'",
user => root,
}
}
I added these lines to site.pp:
Exec { path => "/usr/bin"}
include alias
It says "Could not find command 'alias'". So I'm not sure why it doesn't work. Am I doing this totally wrong? Or is there any other way to make such alias ?And yes, I'm really new at this :)

Alias is a shell builtin and does not live in PATH. An alias is active for the current session only, so even if your command worked it would only set the alias for the executed shell and then exit (and thus unset the alias again). The way you persist aliases is by writing them to a file that the shell reads in on startup.
You should rather use a file/template or file_line from the puppet stdlib.
/etc/profile.d/ can be a good place to put something if you want to make the aliases global (available for everyone). If not .bashrc works well enough (though there is a convention of using a separate file .alias / .bash_alias ).

it is wrong. require add alias definition to ~/.bashrc or /etc/profile.d/youname.sh
And alias is command for bash so require call command => "bash -c\"alias aliasname='ls'\"",

Related

How to make shortcut for frequently used terminal commands in ubuntu

I have this command :
$ anbox session-manager --single-window --window-size=400,650
and i need to run it frequently, is there any way to avoid rewriting it every time I need it ?
For permanently use
run this
echo 'alias customcommand="anbox session-manager --single-window --window-size=400,650"' >> /home/$(USER)/.bashrc
open new terminal
now you can run customcommand and get that response
You can set an alias like this,
alias sessmgr = "anbox session-manager --single-window --window-size=400,650"
Later, you can directly use sessmgr.
You can use the alias command, and if you want to make the change permanent you can append the alias command in your .bashrc file, so every time you launch a terminal you will have by default the defined alias that you set before in your .bashrc file.
e.g.
alias <name_of_the_alias>="<command>".
You can find more info regarding alias here
There are several ways you can do this, the most common is probably using an alias
A common alias is using 'll' for 'ls -al', one way you could do that is with the following command:
alias ll='ls-al'

linux: passing arguments to the .bashrc file

I want to be able to run bash in different predefined configurations. for example: When i'm at work i'll do something like:
bash work
And it will load the appropriate environment variables.
The way i see it, the best way of doing it is passing the configuration name to the .bashrc command and than just configure it according to the name.
To do that, i need to pass the name to the .bashrc file.. How do i pass arguments to the .bashrc file from the bash command?
Of course alternatives for doing what i want will be appreciated!
Thank you!
From the man page:
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).
I don't think you can pass arguments to your individual bashrc directly, but you can call a common bashrc from your customized rc files and then either use arguments or global variables.
If you just want to set some environment variables, you can of course just manually call your customised rc file. To make this a little nicer from your command line, you can define a respective alias:
alias load_work='~/.workrc'
or with a new bash
alias load_work='bash --rcfile ~/.workrc'

Execute alias in cshrc

I have alias which contain small script. We generally call alias with username argument.
For small enhancement,we need to call from shell script. I have write into shell script but it looks like that it does not work
/usr/local/buildpkgs/latest/TclDevKit2.6/bin/tclsh
$VDK_PATH/scripts/ves.tcl /bin/csh $vdktmpfile !*;source
$vdktmpfile;/bin/rm $vdktmpfile;
/usr/local/buildpkgs/latest/TclDevKit2.6/bin/tclsh
$VDK_PATH/scripts/vep.tcl $vdktmpfile $SHELL "$prompt:q";
source $vdktmpfile; /bin/rm $vdktmpfile = sample
we generally invoke alias from command line like sample <username>.We need to call this alias from cshell scrip similar way sample <username>.
Does anyone help me to figure out this issue.
Note: I never seen such a long alias if anyone provide me detail it will be great help.
Where is the alias specified? If not in .bashrc but in a general shell script, then you will have to inside of your current shell script:
a) first invoke shell script containing the alias then
b) call alias as you normally do i.e sample <username>
Other alternative is you might want to call your shell script like this:
. ./<script-name>.sh <args>*
as opposed to just
./<script-name>.sh <args>*
the extra period is to execute script in current shell where alias already defined and not create its own process

Creating permanent executable aliases

I have MySQL installed (MAMP, Mac OS X) but need to call it by the full path each time I access it from the shell. I created an alias: alias mysql='/Applications/MAMP/Library/Bin/mysql, but this only lasts as long as my terminal/Bash session.
What is an effective way for establishing permanent aliases that will work across users? (I need to be able to execute commands from PHP). Should I set up aliases in the Bash start up script (how is that done?), or is it better to edit the sudoers file? (Could use an example of that as well..)
Thanks--
EDIT- Based on answer:
I just tried creating a ~/.bashrc and wrote the following:
alias mysql='/Applications/MAMP/Library/bin/mysql'
But this doesn't seem to have any effect. Is there a special syntax for this file?
Add the command to your ~/.bashrc file.
To make it available to all users, add it to /etc/profile.
Different shell uses different dot file to store aliases.
For mac, the bash shell uses .bash_profile or .profile
For ubuntu, the bash shell uses.bashrc
If you are using zsh shell and ohmyzsh plugin, the dot file is .zshrc
Traditionally, to add a permanent alias, you need to open the dot file and write alias manually like:
alias hello="echo helloworld"
And remember to source the dot file to have it take effect. To source the dot file on ubuntu's bash, type source .bashrc To make the alias available to all users, write to /etc/profile instead of the dot file. Remember to type source /etc/profile for the new alias to take effect.
If you simply want a temporary alias, you do not need to write to dot file. Simply type that same command (alias hello="echo helloworld) on the terminal.
Note that a temporary alias created through the alias command will disappear once the shell is closed.
If you are looking for a single command to generate aliases without open the text editor, read on.
If you have ruby installed on ubuntu, you can create permanent alias with a single command using aka.
gem install aka2
For example:
aka generate hello="echo helloworld" #will generate a alias hello="echo helloworld"
aka destroy hello #will destroy the alias hello
aka edit hello #will prompt you to edit the alias.
With aka, there is no need to write to the dot file with a text editor. And no need to source the dot file too.
You're going about this the wrong way.
Either add /Applications/MAMP/Library/bin/ to your path, or create a script to invoke MySQL and place it in a bin directory which is already in your path.
On a mac the .bashrc file does not get sourced unless you put
source ~/.bashrc in the /etc/profile or /etc/bashrc.
Just thought I would mention that.

How can I define a bash alias as a sequence of multiple commands? [duplicate]

This question already has answers here:
Multiple commands in an alias for bash
(10 answers)
Closed 4 years ago.
I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?
I.e say I want one command to change to a particular directory, then run another command.
In addition, is there a way to setup a command that runs "sudo mycommand", then enters the password? In the MS-DOS days I'd be looking for a .bat file but I'm unsure of the linux (or in this case Mac OSX) equivalent.
For chaining a sequence of commands, try this:
alias x='command1;command2;command3;'
Or you can do this:
alias x='command1 && command2 && command3'
The && makes it only execute subsequent commands if the previous returns successful.
Also for entering passwords interactively, or interfacing with other programs like that, check out expect. (http://expect.nist.gov/)
You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:
command1
command2
and ask bash to execute the file:
bash myscript.sh
If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:
#! /bin/bash
command1
command2
Then mark the file as executable:
chmod 755 myscript.sh
Now you can run it just like any other executable:
./myscript.sh
Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:
#! /usr/bin/perl
That first line tells your shell what interpreter to invoke to execute your script.
Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:
myscript.sh
Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.
it's probably easier to define functions for these types of things than aliases, keeps things more readable if you want to do more than a command or two:
In your .bashrc
perform_my_command() {
pushd /some_dir
my_command "$#"
popd
}
Then on the command line you can simply do:
perform_my_command my_parameter my_other_parameter "my quoted parameter"
You could do anything you like in a function, call other functions, etc.
You may want to have a look at the Advanced Bash Scripting Guide for in depth knowledge.
For the alias you can use this:
alias sequence='command1 -args; command2 -args;'
or if the second command must be executed only if the first one succeeds use:
alias sequence='command1 -args && command2 -args'
Your best bet is probably a shell function instead of an alias if the logic becomes more complex or if you need to add parameters (though bash supports aliases parameters).
This function can be defined in your .profile or .bashrc. The subshell is to avoid changing your working directory.
function myfunc {
( cd /tmp; command )
}
then from your command prompt
$ myfunc
For your second question you can just add your command to /etc/sudoers (if you are completely sure of what you are doing)
myuser ALL = NOPASSWD: \
/bin/mycommand
Apropos multiple commands in a single alias, you can use one of the logical operators to combine them. Here's one to switch to a directory and do an ls on it
alias x="cd /tmp && ls -al"
Another option is to use a shell function. These are sh/zsh/bash commands. I don't know enough of other shells to be sure if they work.
As for the sudo thing, if you want that (although I don't think it's a good idea), the right way to go is to alter the /etc/sudoers file to get what you want.
You can embed the function declaration followed by the function in the alias itself, like so:
alias my_alias='f() { do_stuff_with "$#" (arguments)" ...; }; f'
The benefit of this approach over just declaring the function by itself is that you can have a peace of mind that your function is not going to be overriden by some other script you're sourcing (or using .), which might use its own helper under the same name.
E.g., Suppose you have a script init-my-workspace.sh that you're calling like . init-my-workspace.sh or source init-my-workspace.sh whose purpose is to set or export a bunch of environment variables (e.g., JAVA_HOME, PYTHON_PATH etc.). If you happen to have a function my_alias inside there, as well, then you're out of luck as the latest function declaration withing the same shell instance wins.
Conversely, aliases have separate namespace and even in case of name clash, they are looked up first. Therefore, for customization relevant to interactive usage, you should only ever use aliases.
Finally, note that the practice of putting all the aliases in the same place (e.g., ~/.bash_aliases) enables you to easily spot any name clashes.
you can also write a shell function; example for " cd " and "ls " combo here

Resources