C like preprocessor macros in Bash - bash

I'm not really sure a good name for this question so please rename it if you can think of a better one.
In Bash I have a function that I am using to store certain functions. Consider:
menv_funcs=()
function menv_function {
menv_funcs+=($1)
}
I am then using it in this manner:
menv_function fetch
function fetch {
...
}
I would like to use it like this though:
menv_function fetch {
...
}
Essentially I'm looking for something like the preprocessor macros in C would do but I have been unable to find a way. Any ideas?

As far as I'm aware, you can't directly achieve this. However, I can think of two solutions that may be of interest to you.
First of all, you could just declare the functions as usual, and then obtain the list of declared functions through declare -F. This could be done like:
function fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
menv_funcs+=${l#declare -f }
done < <(declare -F)
Which will cause menv_funcs[#] to list all the functions declared at the point of calling the snippet. Of course, this may catch unwanted functions as well.
To avoid this, you may add some prefix to function names and filter the list:
function menv_fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
if [[ ${l} == 'declare -f menv_'* ]]; then
menv_funcs+=${l#declare -f menv_}
fi
done < <(declare -F)
And if you really want to achieve something like macros, you may try to play with eval:
menv_funcs=()
function menv_function {
local name=${1}
local body=${2}
menv_funcs+=( ${name} )
eval "function ${name} ${body}"
}
menv_function fetch "{
:
}"
But note that you will actually need to quote the whole function body and escape everything appropriately.

Related

Variable substitution in Shell script

I have declared one variable IS_abc=false, on basis of certain condition I am changing value to IS_abc=true
IS_abc=false
declare -a my_arr
my_arr = ('abc' 'pqr' 'xyz')
....
.... // some operation
IS_abc=true
for i in "${my_arr[#]}"
do
//here i want to access value of $IS_abc as true
//how to do this
done
I have tried accessing using $IS_'$i' , but it raising error as invalid substitution
Tell me if I am doing anything wrong here?
You can use indirect var reference:
my_arr=('abc' 'pqr' 'xyz')
IS_abc=true
var="IS_${my_arr[0]}"
echo "${!var}"
Output:
true
I'm doing it like this:
value=`eval echo \\${IS_${i}}`
There's probably a better way but this should work.

Adding extra lines to document using awk

I have a jsp page and a fucntion in it.
function foo() {
if(confirm("bar") {
// do something
}
I want to edit the exsiting or add a new if condtion to the jsp page with a shell script to make it like below
function foo() {
if(confirm("new text"+bar) {
//do something
}
I'm trying to use awk like this to a new if condition when one doesnt exist already.
awk '/foo/{print;print "if(confirm(Hey) {";next}1' myjsp.jsp
awk '/foo2/{print "}";}1' myjsp.jsp //foo2 is fucntion after foo. using this add closing }
The problem is I'm seeing a duplicate lines printing I guess because option 1 print out everything after current line. How do I stop this.
Try this simple method
sed -r '/foo()/{n;/if\(confirm/s/([^"]+)"(.*)".*/\1"new text"+\2)\{/}' fileName
OutPut:
function foo() {
if(confirm("new text"+bar){
// do something
}

How to use a function as a string while using matching pattern

I want to make use of functions to get the full path and directory name of a script.
For this I made two functions :
function _jb-get-script-path ()
{
#returns full path to current working directory
# + path to the script + name of the script file
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
return ${(_jb-get-script-path)##*/}
}
as $(_jb-get-script-path) should be replaced by the result of the function called.
However, I get an error: ${(_jb-get-script-path)##*/}: bad substitution
therefore i tried another way :
function _jb-get-script-path ()
{
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
local temp=$(_jb-get-script-path);
return ${temp##*/}
}
but in this case, the first functions causes an error : numeric argument required. I tried to run local temp=$(_jb-get-script-path $0) in case the $0 wasn't provided through function call (or i don't really know why) but it didn't change anything
I don't want to copy the content of the second fonction as i don't want to replicate code for no good reason.
If you know why those errors happen, I really would like to know why, and of course, if you have a better solution, i'd gladely hear it. But I'm really interessed in the resolution of this problem.
You need to use echo instead of return which is used for returning a numeric status:
_jb-get-script-path() {
#returns full path to current working directory
# + path to the script + name of the script file
echo "$PWD/${0#./*}"
}
_jb-get-script-dirname() {
local p="$(_jb-get-script-path)"
echo "${p##*/}"
}
_jb-get-script-dirname

In bash -- storing method return value

I'm trying to store a value returned from a method like this: var=$(methodName), but the program never enters the method... It's weird because I do the same thing a few lines earlier (alreadyExists-variable in code sample), and it works fine. I had to do this: var='methodName' to make the program enter the method.
It works, so why care? I'm probably making a mistake, and I need to know what it is and learn from it. Let me know if you need more info to answer the question. Thanks!
overwriteOrNot()
{
echo DEBUG
# This debug string does not print if method is called from "local overwrite=$(overwriteOrNot)"
# but prints if method is called from "local overwrite='overwriteOrNot'"
...
}
local alreadyExists=$(studentNumberExists studentNumber)
if $alreadyExists ; then
# local overwrite=$(overwriteOrNot)
local overwrite='overwriteOrNot'
...
If you're using return, then you need to either directly branch on its result:
if overwriteOrNot; then
: "the function returned 0"
else
: "the function returned something other than 0"
fi
...or store the value of $? immediately after running the function:
overwriteOrNot
local overwrite=$?
Note that return can only return a single-byte integer. If you need to pass content which doesn't fit that type, it needs to be either passed on stdout or in a global variable.
The following:
local overwrite='overwriteOrNot'
assigns a string; it doesn't invoke a function. Instead:
local overwrite=$(overwriteOrNot)
You can check the return value from calling overwriteOrNot with the $? variable, or by checking its numeric return value directly in a conditional statement like:
if overwriteOrNot; then
:
fi
If you assign to overwrite, you can also check its value with any valid test condition such as equality, regular expression match, or emptiness. For example:
if [[ "$overwrite" == "foo" ]]; then
:
fi

Bash alias name from a variable

Is there a way to make a bash alias (or function) with its name coming from a variable?
For instance, is it possible to do something along these lines:
create_alias_with_name() {
alias $1="echo a custom alias"
}
Or something along these lines:
create_func_with_name() {
$1() {
"echo inside a function with a variable name"
}
}
In other words, I would prefer to have some kind of function "factory" that can register functions for me. Is this possible or beyond the capabilities of Bash?
Did you even try it? Your first example works fine.
You can make the second work by adding an eval:
create_func_with_name() {
eval "$1() {
echo inside a function with a variable name
}"
}
just in case, one may use a variable both as a part of the alias name and as a part of the alias command:
alias foo${var1}="bar${var2}"

Resources