How to create an alias with multiple commands that accepts parameters in Cmder? - alias

I would like to know how to create one alias with multiple commands in Cmder, that accepts parameters and injects them into the commands.

To separate commands in a single alias $t would do the trick.
To retrieve all the parameters passed to an alias $* would do the trick.
Examples:
Multiple Commands: alias serveApp=cd "C:\app" $t grunt serve
Parameters: alias nav=cd $*, usage: nav "C:\app"
Combination: alias servePath=cd $* $t grunt serve

Related

How do I access a module's symbol table dynamically at runtime in Raku?

I want to be able to pass my script the path to a .rakumod file, say <blah>/Mod.rakumod, and be able to access the symbol table as a hash as if I'd used the module instead:
The module:
$ cat Mod.rakumod
unit module Mod;
sub bag is export { ... }
use lib <dir-containing-Mod>
use Mod;
say Mod::EXPORT::.keys
works, as expected, returning (ALL DEFAULT).
On the other hand:
use lib <dir-containing-Mod>
require Mod;
say Mod::EXPORT::.keys
fails with
Could not find symbol '&EXPORT' in 'Mod'
in block <unit> at <blah>
This is despite the fact that even with require, say Mod::.keys does see EXPORT:
use lib <dir-containing-Mod>
require Mod;
say Mod::.keys
---
(EXPORT Mod)
I need to use require to make this dynamic, as I don't know which module I'll want.
I can actually think of one thing to do, but it is absolutely disgusting:
save my module name into a variable $mod
have my script write another script using that module:
my $mod = <whatever>
my $cd = qq:to/END/;
use v6;
use lib qq\|\$\*CWD\|;
use $mod;
say {$mod}::EXPORT::ALL::.keys;
END
'aux.p6'.IO.spurt($cd);
and then have the initial script call the auxiliary one:
shell("raku aux.p6")
What worked, per raiph's answer (which I've accepted and am here paraphrasing):
pass the path to the module and save it as $path;
use lib the relevant directory:
my $dir = $path.IO.dirname;
use lib $dir;
extract the plain file name:
my $modFile = S/(.*)\..*/$0/ with $path.IO.basename;
finally, require that and pull the .WHO trick from raiph's answer, slightly adapted:
require ::($modFile);
say ::("{$modFile}::EXPORT::ALL").WHO.keys;
Run with <script> <path> that returned (&bag) all right.
Actually, the above doesn't quite work: use lib $dir will fail saying $dir is empty, because use lib isn't dynamic.
So instead I am now resorting to the unappealing solution of
copying the module file to a temporary directory ./TMP
having called use './TMP';
and then removing that directory when done.
TL;DR Use dynamic symbol lookup to get the symbol of the package whose symbols you want at run-time; then .WHO to get its stash; then .keys to get that stash's symbols.
For example:
use lib '.';
require Mod;
say ::Mod::EXPORT::('ALL').WHO.keys; # (&bag)
I'm going to make breakfast. I'll elaborate later.

zsh aliases don't expand even with option `complete_aliases`

How do I expand an alias defined when using zsh -c?
% zsh -c 'setopt aliases complete_aliases; alias d=date; d'
zsh:1: command not found: d
Use eval to force the zsh to parse the alias after the code has been read in:
zsh -c 'setopt aliases complete_aliases; alias d=date; eval d'
In this case, d will never expand, so doesn't need to be quoted.
In general you should properly quote all arguments to eval.
man zshmisc says:
There is a commonly encountered problem with aliases illustrated by the
following code:
alias echobar='echo bar'; echobar
This prints a message that the command echobar could not be found.
This happens because aliases are expanded when the code is read in; the
entire line is read in one go, so that when echobar is executed it is
too late to expand the newly defined alias. This is often a problem in
shell scripts, functions, and code executed with `source' or `.'.
Consequently, use of functions rather than aliases is recommended in
non-interactive code.

tcsh shell and variable argument alias

I use tcsh shell and I cannot change to another shell.
I'm trying to create an alias to quickly cd into my project.
My home directory looks like:
/home/projects/proj1
/home/projects/proj2
...
/home/stuff/stuff1
If i am working in stuff 1 directory and want to quickly navigate to my projects, i created an alias as follows:
alias P 'cd /home/projects && cd ./\!:1'
so that I can type 'P proj1' to get there
However, sometimes i want to navigate to /home/projects directory instead of a particular project and i thought just typing 'P' (without arguments) will get me there but i'm getting a "Bad ! arg selector". How do i create an aliases to handle zero or more arguments?
Thanks
This works:
alias P 'cd /home/projects/\!*'
If you pass an argument, it is appended to the end of the path, else !* is replaced by nothing.

Call a powershell script with variables declared which are to used by multiple powershell scripts

I have 10 scripts which are using common variables and functions .
I wanted to have two common scripts one with variables and other with all common functions.
How to call these scripts in my main script?
Pls. help!
I am too new to this :(
You have to create your .ps1 script that contains variables and functions then in your other scripts you have to call that file with a "."
EX:
Script with functions:
# function.ps1
function test($text)
{
write-host $text
}
Script you want to run:
# Script.ps1
."Z:\function.ps1"
Test "this is a test"

An alias that just prints the named directory?

I'm kind of just realizing how powerful the terminal can be. My question is essentially if I can create an alias that just prints the name of a directory. For example, I could easily make an alias such as "alias sitename="cd ~/sites/path/to/my/site/". But what I want is an alias that only prints the directory name so that I can use it for several things. So that, for example, if I wanted I could just say cd "alias", or mv from-dir "alias".
Is there a way to do this? I've tried and it seems to recognize the alias if I just type it in: it will report "alias" is a directory. But if I try to couple it with another command, it fails.
You don't want to use alias, what you are after is an environment variable
$ export SITENAME="~/sites/path/to/my/site/"
$ cd $SITENAME
Bash is quite picky over syntax - note the lack of spaces in the export and the $ when you use it.
Use a variable, simply
d=/path/to/some/directory
echo $d
cd $d
mv somedir $d/
You don't need to use an alias here, a variable is sufficient.
It sounds like you want to set a variable, not an alias. Such as,
sitename=/home/jimbo/. Then, cd $sitename would put you at /home/jimbo/.
If you want this variable to have permanence (i.e. you don't have to set it every time you open a new session), then you can make it an environmental variable using the export command or add it to your .bashrc file (typically located at $HOME/.bashrc) using the line: sitename=/home/jimbo/.
FYI, $HOME is another environmental variable that's equivalent to ~/.
Using a variable is the simplest solution. You could get fancy and use an array:
mydir() { echo "/my/directory"; }
To display the value
mydir
To use the value, you need some extra puncuation
cd $(mydir)
cd `mydir`