Variable in bash script exported in function - bash

I have a function in a script, in which I would like to expand a variable. How do I do it?
eg:
set_function.sh:
function test_funct {some_binary --configfile="$1"/folder/; }
export -f test_funct
I would like it so that if I call:
./set_function.sh /some/path/here
the passed variable /some/path/here gets expanded so that if I then call
test_funct --some-other-flag
I don't get:
some_binary --configfile=--some-other-flag/some_path/
but rather
some_binary --configfile=/some/path/here/folder/ --some-other-flag

Seems like you need to use variables, like this:
test_func() {
some_binary --config $CONFIG_PATH/folder/ "$#"
}
and call it like
CONFIG_PATH=~/config1 test_func --verbose
export CONFIG_PATH=~/config2
test_func --quiet
Also note that you cannot export things upwards, i.e. you cannot call set_function.sh so that test_func be exported to the calling shell. You need . set_function.sh or equally source set_function.sh instead.

Related

Using alias in bash function

I have defined an alias like so:
alias X="path/to/program"
and I have a function defined like this:
doX() { X -flag "$1"; }
I put these in my .bashrc file, and when I open bash, I get a syntax error near unexpected token '-flag'. At this point, the alias has been set, but the function has not, due to this error. If I run
doX() { X -flag "$1"; }
at this point, it works. I have tried putting this into a file and sourcing it after I set the alias in the .bashrc file, but it is giving me the same results.
How can I fix this? Is there a way to define the alias AND the function in the .bashrc so that they are both set when I open bash?
Aliases are not usually available in scripts. If you want to have a function use an alias, consider making the alias itself a function:
X() { path/to/program "$#"; }
doX() { X -flag "$1"; }

how to call a bash function providing environment variables stored in a Bash array?

I got two variables in a bash script. One contains the name of a function within the script while the other one is an array containing KEY=VALUE or KEY='VALUE WITH SPACES' pairs. They are the result of parsing a specific file, and I can't change this.
What I want to do is to invoke the function whose name I got. This is quite simple:
# get the value for the function
myfunc="some_function"
# invoke the function whose name is stored in $myfunc
$myfunc
Consider the function foo be defined as
function foo
{
echo "MYVAR: $MYVAR"
echo "MYVAR2: $MYVAR2"
}
If I get the variables
funcname="foo"
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
How would I use them to call foo with the pairs of funcenv being added to the environment? A (non-variable) invocation would look like
MYVAR=test MYVAR2='tes2 test3' foo
I tried to script it like
"${funcenv[#]}" "$funcname"
But this leads to an error (MYVAR=test: command not found).
How do I properly call the function with the arguments of the array put in its environment (I do not want to export them, they should just be available for the invoked function)?
You can do like this:
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
for pairs in "${funcenv[#]}"; do
eval "$pairs"
done
"$funcname"
Note however that the variables will be visible outside the function too.
If you want to avoid that, then you can wrap all the above in a (...) subshell.
why don't you pass them as arguments to your function?
function f() { echo "first: $1"; echo "second: $2"; }
fn=f; $fn oneword "two words"

Passing shell script parameters from one script to other

I have two shell scripts say 1.ksh & 2.ksh
1.ksh contains functions which has variables in it.
e.g.
func1() {
test=$DIRPATH/$FILENAME/$SCRIPTNAME
}
2.ksh contains
DIRPATH="/ABC/DEF/GHI" .... {PATH}
FILENAME="ABC.txt"
I want to invoke 1.ksh inside 2.ksh & pass these parameters to respective functions.
Also, I want to run both the script in a single instance.
How can I achieve this?
You might be looking for . dot command
The . fileNmae can be used to export all varibles function onto the bash script involving the statements
To use the function func1 from 2.ksh use
``2.ksh` script edited as
#2.ksh contains
. ./1.ksh #full path must be something like /home/....
DIRPATH="/ABC/DEF/GHI" .... {PATH}
FILENAME="ABC.txt"
func1 #calls the function within the second, 2.ksh
For example consider the following
$ cat one
func1() {
echo "hello $1"
}
$ cat two
. ./one
func1 world
$ ksh two
hello world

How to alias an export in bash

I am trying to set an env variable which I can use to do relative directory chains. I am trying to do it the following way but cant get it to work. How do I do it?
alias sroot="export SROOT="$PWD""
alias drumit="cd $SROOT/abc/def/drumit"
If I type sroot, it takes the alias but when i type drumit, it gives me an error saying
bash: cd: /abc/def/drumit: No such file or directory
Looks like when the shell was launched it takes $SROOT as .
Appreciate any help.
Thanks
Your $PWD and $SROOT variables are being expanded at the time you define the aliases, not when you are using them. Put a \ in front of them to escape them while they are defined.
alias sroot="export SROOT="\$PWD""
alias drumit="cd \$SROOT/abc/def/drumit"
When you initially set the alias, it expands $PWD instead of keeping it as the variable form. Try using function instead like this:
$ function sroot {
> export SROOT="$PWD"
> }
$ export -f sroot
$ function drumit {
> cd $SROOT/cron
> }
$ export -f drumit
$ declare -f sroot
sroot()
{
export SROOT="$PWD"
}
$ declare -f drumit
drumit ()
{
cd $SROOT/abc/def/drumit
}
This is what is currently happening when you alias like in your question (variable expanding):
$ alias sroot="export SROOT="$PWD""
$ alias drumit="cd $SROOT/abc/def/drumit"
$ alias
alias SROOT='/home/jon'
alias drumit='cd /home/jon/abc/def/drumit'
alias sroot='export SROOT=/home/jon'
Escaping would work too:
$ alias sroot="export SROOT="\$PWD""
$ alias drumit="cd \$SROOT/abc/def/drumit"
$ alias
alias drumit='cd $SROOT/abc/def/drumit'
alias sroot='export SROOT=$PWD'

Passing argument to alias in bash [duplicate]

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 5 years ago.
Is it possible to do the following:
I want to run the following:
mongodb bin/mongod
In my bash_profile I have
alias = "./path/to/mongodb/$1"
An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1).
$ alias foo='/path/to/bar'
$ foo some args
will get expanded to
$ /path/to/bar some args
If you want to use explicit arguments, you'll need to use a function
$ foo () { /path/to/bar "$#" fixed args; }
$ foo abc 123
will be executed as if you had done
$ /path/to/bar abc 123 fixed args
To undefine an alias:
unalias foo
To undefine a function:
unset -f foo
To see the type and definition (for each defined alias, keyword, function, builtin or executable file):
type -a foo
Or type only (for the highest precedence occurrence):
type -t foo
to use parameters in aliases, i use this method:
alias myalias='function __myalias() { echo "Hello $*"; unset -f __myalias; }; __myalias'
its a self-destructive function wrapped in an alias, so it pretty much is the best of both worlds, and doesnt take up an extra line(s) in your definitions... which i hate, oh yeah and if you need that return value, you'll have to store it before calling unset, and then return the value using the "return" keyword in that self destructive function there:
alias myalias='function __myalias() { echo "Hello $*"; myresult=$?; unset -f __myalias; return $myresult; }; __myalias'
so..
you could, if you need to have that variable in there
alias mongodb='function __mongodb() { ./path/to/mongodb/$1; unset -f __mongodb; }; __mongodb'
of course...
alias mongodb='./path/to/mongodb/'
would actually do the same thing without the need for parameters, but like i said, if you wanted or needed them for some reason (for example, you needed $2 instead of $1), you would need to use a wrapper like that. If it is bigger than one line you might consider just writing a function outright since it would become more of an eyesore as it grew larger. Functions are great since you get all the perks that functions give (see completion, traps, bind, etc for the goodies that functions can provide, in the bash manpage).
I hope that helps you out :)
Usually when I want to pass arguments to an alias in Bash, I use a combination of an alias and a function like this, for instance:
function __t2d {
if [ "$1x" != 'x' ]; then
date -d "#$1"
fi
}
alias t2d='__t2d'
This is the solution which can avoid using function:
alias addone='{ num=$(cat -); echo "input: $num"; echo "result:$(($num+1))"; }<<<'
test result
addone 200
input: 200
result:201
In csh (as opposed to bash) you can do exactly what you want.
alias print 'lpr \!^ -Pps5'
print memo.txt
The notation \!^ causes the argument to be inserted in the command at this point.
The ! character is preceeded by a \ to prevent it being interpreted as a history command.
You can also pass multiple arguments:
alias print 'lpr \!* -Pps5'
print part1.ps glossary.ps figure.ps
(Examples taken from http://unixhelp.ed.ac.uk/shell/alias_csh2.1.html .)
To simplify leed25d's answer, use a combination of an alias and a function. For example:
function __GetIt {
cp ./path/to/stuff/$* .
}
alias GetIt='__GetIt'

Resources