This is the command i want to execute
watchify files/[filename].js -t hbsfy -o out/[filename].js
i tried this
javascriptCompile() {
foo="src/js/files/$1.js -t hbsfy -o src/js/out/$1.js"
watchify "$foo"
}
alias jc=javascriptCompile
jc main
I get this error
You MUST specify an outfile with -o.
It needs to be an array:
javascriptCompile () {
foo=(src/js/files/"$1".js -t hbsfy -o src/js/out/"$1".js)
watchify "${foo[#]}"
}
alias jc=javascriptCompile
jc main
Related
I added
n() { $EDITOR ~/notes/"$*".txt } nls() { ls -c ~/notes/ | grep "$*" }
to ".bashrc"
but terminal answer me
-bash: n: command not found
reference
https://lifehacker.com/turn-your-command-line-into-a-fast-and-simple-note-taki-5592047
How can I add use terminal as note?
If you put everything in one line, you need more semicolons
n() { $EDITOR ~/notes/"$*".txt; }; nls() { ls -c ~/notes/ | grep "$*"; }
Alternatively, for more readability, break up the lines
n() {
$EDITOR ~/notes/"$*".txt
}
nls() {
ls -c ~/notes/ | grep "$*"
}
Also, when modifying your .bashrc, changes will only take effect after it has been re-sourced. Either restart your terminal (which will load it again), or do it manually by entering . ~/.bashrc or source ~/.bashrc.
I am having problem to run simple code below:
#!/bin/ksh
set -x
function test_me
{
set -x
date
}
function check_me
{
set -x
ssh ${HST2} "$(typeset -f test_me); test_me"
}
ssh ${HST1} "$(typeset -f); check_me"
Fails with syntax error at line 5: `;;' unexpected
Though I can't explain why this gives you the particular error message you see, at least I see that your code can't work:
First you run in one process (on HST1) the commands
function check_me
{
set -x
ssh ${HST2} "$(typeset -f test_me); test_me"
};check_me
On HST1 defines only the function check_me, nothing else. Then you run this check_me.
Inside this function, you refer to two things: A variable HST2, and a function test_me. There is nothing in your code which would define these entities. They are only defined in that initial process, where you do the SSH, but they are not defined on the remote process on the host $HST1.
Moreover, you don't even run a ksh on $HST1. At least I don't see and ksh invocation in your code. Actually, you just pass a function .... to $HST1, and function is not an executable.
#!/bin/ksh
set -x
function second_me
{
set -x
date
}
function first_me
{
set -x
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"
above Fails with syntax error at line X: `;;' unexpected
But If I add extra dummy function third_me, things work fine, looks like ksh bug?
#!/bin/ksh
set -x
function third_me
{
set -x
date
}
function second_me
{
set -x
date
}
function first_me
{
set -x
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"
Code works fine
Another work around using sub-function:
#!/bin/ksh
set -x
function first_me
{
set -x
function second_me
{
set -x
date
}
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"
Say I have two bash functions:
dock() { sudo docker $# ;}
and
dock-ip() { sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $# ;}
How to get bash auto-completion working with the second function?
With the first one, it is as easy as adding:
_completion_loader docker; complete -F _docker dock
This will not work for the second one. The autocomplete source for Docker is in /usr/share/bash-completion/completions/docker on Debian Stretch. I have more functions like dock-run, dock-exec, etc. so I don't want to write a custom completion function for each of them.
Also, complete -F _docker_container_inspect dock-ip only partially works; tab only lists containers, not completes partial strings.
Research:
How do I autocomplete nested, multi-level subcommands? <-- needs custom functions
https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases <-- automated for top commands only
With a combined hour of bash completion experience, I took apart the docker completion script (/usr/share/bash-completion/completions/docker) and the bash_completion.sh script to come up with a wrapper function:
# Usage:
# docker_alias_completion_wrapper <completion function> <alias/function name>
#
# Example:
# dock-ip() { docker inspect --format '{{ .NetworkSettings.IPAddress }}' $# ;}
# docker_alias_completion_wrapper __docker_complete_containers_running dock-ip
function docker_alias_completion_wrapper {
local completion_function="$1";
local alias_name="$2";
local func=$(cat <<EOT
# Generate a new completion function name
function _$alias_name() {
# Start off like _docker()
local previous_extglob_setting=\$(shopt -p extglob);
shopt -s extglob;
# Populate \$cur, \$prev, \$words, \$cword
_get_comp_words_by_ref -n : cur prev words cword;
# Declare and execute
declare -F $completion_function >/dev/null && $completion_function;
eval "\$previous_extglob_setting";
return 0;
};
EOT
);
eval "$func";
# Register the alias completion function
complete -F _$alias_name $alias_name
}
export -f docker_alias_completion_wrapper
I then created my alias/functions like this:
# Get container IP
dock-ip() { docker inspect --format '{{ .NetworkSettings.IPAddress }}' $# ;}
docker_alias_completion_wrapper __docker_complete_containers_running dock-ip
# Execute interactive container
dock-exec() { docker exec -i -t --privileged $# ;}
docker_alias_completion_wrapper __docker_complete_containers_all dock-exec
...
Be sure to call _completion_loader docker; at the top of your profile aliases script to load the main Docker completion scripts. I invite more skilled bash programmers to improve this answer, please.
[In my .bashrc]
Basically I try to make an alias:
alias e='su -c'
But when I write in a terminal:
~$ e ls -goFha /root
I (obviously) get the error:
su: group oFha does not exist
If $str were replaced by the rest of the command, the herebelow code would work:
alias e='su -c "$str"'
But alias don't work this way. Therefore, I thought to a function.
Replacing $str by the whole argument string, it could be something like:
e () {
"su -c '$str'"
}
How to get the whole argument string in a function?
How would you write my function?
Thanks
Here is another solution:
e() {
su -c "$*"
}
You can try this:
e () {
CMD="$#"
su -c "$CMD"
}
I am trying to define a bash function, mycd. This function uses an associative array mycdar. If the key exists in the array the function will change directory to the corresponding value of the key. If the key doesn't exist it will change to the dir provided in the command line.
What I would like to do is to have completion for this function, from both they keys of the associated array or from the folders existing in the current directory.
Thank you.
Building my own cd function with completion
Using an associative array for storing some paths.
First the command:
mycd() { [ -v mycdar["$1"] ] && cd "${mycdar[$1]}" || cd "$1"; }
Second the completion command
_mycd() {
local cur;
_cd ;
_get_comp_words_by_ref cur;
COMPREPLY=($(
printf "%s\n" "${!mycdar[#]}" |
grep ^$cur)
${COMPREPLY[#]});
}
One array:
declare -A mycdar='(
["docs"]="/usr/share/doc"
["home"]="$HOME"
["logs"]="/var/log"
["proc"]="/proc"
["root"]="/"
["tmp"]="/tmp"
)'
Than finaly the bind:
complete -F _mycd -o nospace mycd
Or to permit standard path building behaviour:
complete -F _mycd -o nospace -o plusdirs mycd
It turns out that there is an option that to the complete function that does exactly what is asked:
complete -o plusdirs -o nospace -F _mycd mycd
In this case _mycd just returns matching elements from the keys of the associative array.