So I basically am trying to overwrite my ssh
command so I only have to type ssh
and by default it would connect to my main server. Then if I passed it an argument, say username@server_port
it would then run the basic command.
# Fast SSH (a working progress) TODO: make work without naming the function `fssh`
function fssh() {
ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT
# if the `ssh` argument is not set
if [ -z "${1+xxx}" ]; then
# echo "ALEX_SERVER_CONNECTION is not set at all";
ssh $ALEX_SERVER_CONNECTION
fi
# if the `ssh` argument is set
if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then
ssh $1
fi
}
How do I get it to work without the f
in front of the ssh
?
So basically this is how it looks when properly done:
# Fast SSH
function ssh() {
ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT
# if the `ssh` argument is not set
if [ -z "${1+xxx}" ]; then # ssh to the default server
command ssh $ALEX_SERVER_CONNECTION
fi
# if the `ssh` argument is set
if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then # ssh using a different server
command ssh $1
fi
}
To answer your question, yes and no. The commands in the script are loaded and executed, and can delete the source file of the script. This will make impossible to execute the same file again, of course, but will not affect the first execution. It's both.
In Bash, the ${parameter:=word} construct says that if $parameter is set, use the value of $parameter . However, if $parameter is null or unset, use word instead. Now the variable $COMMANDLINE_FOO is set and readable for your shell scripts.
Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.
You need to specify the absolute path to ssh
command in your function otherwise it will be recursive. For instance, instead of:
function ssh() { ssh $USER@localhost; } # WRONG!
You should write:
function ssh() { command ssh $USER@localhost; }
Use command
built-in to get the ssh
from the PATH
(as suggested by @chepner):
command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function lookup. **Only builtin commands or commands found in the PATH are executed**.
Using a function is the correct pattern, read the Aliases and Functions sections from the man page.
ALIASES
There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).
When naming your custom function ssh
do the following:
source ~/.bashrc
ssh
with: which ssh
or type ssh
type
and which
Prior to declaring a custom function I got:
type ssh # → ssh is /usr/bin/ssh
which ssh # → /usr/bin/ssh
After declaring function ssh() { ssh my-vm; }
, I got:
which ssh # → ssh () { ssh my-vm; }
type ssh # → ssh is a shell function
Either use sh
or bash
syntax:
sh
: test is done with [ … ]
, portable but not powerful ;bash
test is done [[ … ]]
and function
keyword, less portable but dev-friendly.How do I get it to work without the f in front of the ssh?
Set an alias in ~/.bashrc
:
alias ssh='fssh'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With