there is a function for example
succes () { echo "OK" } succes can I do it like this?
succes (var3) { echo $var3 } succes("OK") You can pass parameters.
The call will be:
succes "OK" And appeal to the parameters
succes () { echo $1 } The function refers to the arguments passed to it based on their position. Those. these are positional parameters
In general, the information in the function (and the script itself as well) can be transferred (at least) in four ways:
Here is a script showing the first three paths:
f1() { echo "вызов '$FUNCNAME' номер $1, второй аргумент равен '$2', переменные: var1='$var1', var2='$var2'" } f2() { read var1; echo "вызов '$FUNCNAME' номер $1, в stdin передан '$var1'" } var1="общая" f1 один var1="переопределённая" var2="непосредственная" f1 два echo "текст" | f2 один var2="непосредственная" f1 три 42 here is the result of its launch:
вызов 'f1' номер один, второй аргумент равен '', переменные: var1='общая', var2='' вызов 'f1' номер два, второй аргумент равен '', переменные: var1='переопределённая', var2='непосредственная' вызов 'f2' номер один, в stdin передан 'текст' вызов 'f1' номер три, второй аргумент равен '42', переменные: var1='общая', var2='непосредственная' No, well, passing the arguments to the function actually refers to passing them in the call list, and not somehow. External arguments, interactive reading from stdin or another file is not a transfer of data to a function, but a way to get data no matter where this code is written. The author apparently does not understand what it is - passing arguments to a function ... but oh well! So, of his 4 methods, only 1 positional parameters remain. The question was whether it is possible to pass named parameters. The answer is no. This is not provided in the modern syntax of the bash script. Maybe in later versions.
The number of parameters is also not too much. This is not exactly milen ... But you can transfer 20 pieces. I haven't tried it anymore. You can check. But I'm afraid this is not a stable option. The variables $ # are also defined - the number of arguments and the list $ @. The $ 0 parameter, however, does not mean the name of the function, as it might seem, but the name of the script. This is the difference! These arguments are not readonly. Those. you can change them, shift the list using shift . the truth on the external environment will not affect it ... It will be difficult to even syntactically change the positional parameter, because according to the rules of the bash syntax you will have to write something like 1=новое_значение , which immediately causes an error, because 1 will be taken not as a variable name, but as number. It is strange that it is not embedded in the syntax. But if you use eval , you can. But it is not necessary, except for the case if you do not use another way of passing an argument to a function: an indirect reference with a change in its value within the function.
In general, such a mechanism bash does not provide, but there is no prohibition. Therefore, by manipulation, you can do this. Although this is more likely not like a normal code, but a kind of shamanism. But it still works. The code is poorly understood, but this can be done. This is not for beginners, I say right away. It would be funny if the construction of type 1=verry worked, in the sense that we managed to change the number 1 itself ... I don’t know how the program would work after that, probably in any way, or would start terribly incomprehensible behavior. This is of course unacceptable, the unit can not be changed. And this has already happened in programming practice. I remember in the first versions of FORTRAN it was possible to change even zero ... The program just crashed! And it is completely incomprehensible why ... As they say, it all happens.
Source: https://ru.stackoverflow.com/questions/606431/
All Articles