Answer the question
In order to leave comments, you need to log in
How to get parse and return string from bash function?
Good afternoon!
Please advise how to write a function in bash
so that it takes one argument, and inside you can parse this argument (string) and return a number from it
к примеру:
function myFunc {
return +arg.split('-', 2)
}
if
under the appropriate condition myFunc $1
bash
it doesn’t work like that and I can’t start this business. elif ; then
...прочий конфиг --mark myFunc $2
-- p-4
Answer the question
In order to leave comments, you need to log in
oh, and you explain confusingly, I meditated for half an hour before roughly understanding what you need, although I’m not sure even that
how to write a function in bash so that it takes one argument, and inside you can parse this argument (string) and return a number from it
foo() {
foo_arg1="$1"
echo "$foo_arg1" | awk -F- '/^p-{1,}$/{printf $2}'
}
foo p-4 # вернет 4
then call already in the if block with the appropriate condition myFunc $1
code in if
elif ; then ...прочий конфиг --mark myFunc $2
#!/usr/bin/env bash
arg1="$1"
arg2="$2"
foo() {
foo_arg1="$1"
echo "$foo_arg1" | awk -F- '/^p-{1,}$/{printf $2}'
}
if ; then
echo --mark $(foo $arg1)
fi
exit
./0.sh p-4 4
--mark 4
./0.sh p-4 5 # ничего не выведет так как p-4 неравно p-5
I run this command from the command line after the --p-4 flag
and I need to get 4-ku to process it further.
#! /bin/bash
foo()
{
RES="${1#p-}";
}
foo "$1"
echo "$RES"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question