Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether a function is defined

I have a simple script to check if rbenv is a function, when I type: $ type rbenv | grep "rbenv is a function" into the shell, I get positive result, shell response is rbenv is a function Then I want to do this in a script, so I write in my file:

#!/bin/bash
if  type rbenv | grep -q "rbenv is a function" ;
then
  echo "success!"
else
  echo "something went wrong."
fi

I can't get this working for me, the result of this script is something went wrong. I want to get this working properly, and maybe know if there is a better solution to do this task.

I have tried also this version of a script with no success.

if  [[ $(type rbenv | head -1) == "rbenv is a function" ]] ;
...

writing type rbenv | head -1 in a shell gives output rbenv is a function. I don't know what I am doing wrong, please point out my mistakes.

Partial solution to this problem is to run in your shell export -f rbenv before running a script, but this isn't the way to do this right?

FULL SOLUTION Apparently, what I need is to repeat my steps from installation of rbenv, as it is to put before if statement:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

I want to thank everyone participating in the discussion for guiding me through the solution :)

like image 784
Szymon Błaszczyński Avatar asked Oct 28 '25 15:10

Szymon Błaszczyński


2 Answers

My hunch is that you have not exported your function. Try

export -f rbenv

before running your script. This will make the function visible to sub-shells, such as those which are started when you run a shell script.

Depending on what you are trying to accomplish, a better solution might be to define your code as a function in your .bashrc or similar instead of as a separate script.

In addition, your code could be made more robust. The output format of the message could change in the next Bash upgrade, or you could end up using Bash localized to another language some day.

case $(type -t rbenv) in
    function) echo Success;;
    *) echo Something went wrong;;
esac

(The change from if test to case is just personal idiom / evangelism, not a crucial modification.)

like image 139
tripleee Avatar answered Oct 31 '25 06:10

tripleee


FULL SOLUTION

Apparently, what I need is to repeat my steps from installation of rbenv, as it is to put before the if statement lines I have in my ~/.bashrc on Ubuntu Desktop or ~/.zshrc in Zsh shell or ~/.bash_profile in any other Linux flavour with Bash

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

only then it will load full rbenv script into my rbenv installation script and I can check if it's a function with

if  type rbenv | grep -q "rbenv is a function" ;
then
  echo "success!"
else
  echo "something went wrong."
fi

better way to do this, as suggested by tripleee, is to use [ "$( type -t rbenv )" == "function" ] inside the if statement. I'm not the shell hacker, but it seems to somewhat more codish and reusable.

I want to thank everyone participating in the discussion for guiding me through the solution :)

like image 24
Szymon Błaszczyński Avatar answered Oct 31 '25 05:10

Szymon Błaszczyński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!