Hi all. Suppose I have the following function in an executable file named "HOLA":
------------------------
function hola { echo "Hola ${@}."; }
------------------------
In addition, suppose that I want to execute the file so I can input my name next to ./HOLA. I mean,
--------------------------
$ ./HOLA hugo esquivel
--------------------------
According to the function hola, the output would be:
--------------------------
Hola hugo esquivel.
--------------------------
My question is: how can I achieve this behaviour? Is possible?
Any help will be greatly appreciated!
PS: What I want is to create the command hola on a BASH shell (but through the executable).
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags
```text
and
```
by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
You are missing the () behind the function name. Also you have to call a function when you want to use it. Also you have to give the function all the parameters you give to the shell script too. I changed $@ to $* since $@ is some thing like
"jupp" "selters"
# while $* is
"jupp selters"
Rest is formatting. It is also usually good to have a shebang in the 1st line (that #!...) to tell which shell/interpreter you want to use for this script, and also a "exit 0" can't harm if you are going to check exit stati.
#!/bin/bash
# or ksh
# call function like any other command type (builtin, external, alias, function, ...)
# = function get arguments from function call line
function hola
{
echo "Hola $@."
}
##main##
# save script arguments to the variable
CmdLineArgs=$@
# run command - in this case command is function
hola $CmdLineArgs
Function, how to define ?
Old bsh style, which is also Posix-standard =
the most generic.
[ function ] name () compound-command [redirection]
This defines a function named name. The reserved word function is optional. If the function reserved word is supplied, the parentheses
are optional. The body of the function is the compound command compound-command (see Compound Commands above). That command is usually
a list of commands between { and }, but may be any command listed under Compound Commands above. compound-command is executed whenever
name is specified as the name of a simple command. Any redirections (see REDIRECTION below) specified when a function is defined are
performed when the function is executed. The exit status of a function definition is zero unless a syntax error occurs or a readonly
function with the same name already exists. When executed, the exit status of a function is the exit status of the last command exe-
cuted in the body. (See FUNCTIONS below.)
Tbh, I usually write them even without "function" in front of them.