In the Bash function: how to check if an argument is a given variable?

I want to implement the bash function, which is the 1st argument, is actually a variable defined somewhere.

For example, in my .bashrc:

customPrompt='yes';
syntaxOn='no';
[...]
function my_func {
    [...]
    # I want to test if the string $1 is the name of a variable defined up above
    # so something like: 
    if [[ $$1 == 'yes' ]];then 
         echo "$1 is set to yes";
    else
         echo "$1 is not set or != to yes";
    fi
    # but of course $$1 doesn't work
}
Is required

:

$ my_func customPrompt
> customPrompt is set to yes
$ my_func syntaxOn
> syntaxOn is set but != to yes
$ my_func foobar
> foobar is not set

I tried a lot of tests, for example -v "$1", -z "$1", -n "$1"but they all check $ 1 as a string, not as a variable. (please correct me if I don't make it clear enough)

+4
source share
4 answers

In bashyou can use the indirect variable subtituion.

t1=some
t2=yes
fufu() {
    case "${!1}" in
        yes) echo "$1: set to yes. Value: ${!1}";;
        '')  echo "$1: not set. Value: ${!1:-UNDEF}";;
        *)   echo "$1: set to something other than yes. Value: ${!1}";;
    esac
}

fufu t1
fufu t2
fufu t3

prints

t1: set to something other than yes. Value: some
t2: set to yes. Value: yes
t3: not set. Value: UNDEF

${!variablename} bash indirect variable expansion. , , https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Whrere:

${parameter}. . , , .

(!), . bash , , ; , , . . ${! prefix} ${! name [@]}, . , .

, : fooobar.com/questions/1153903/... , .

+2

,

if [[  $var ]]
then
    echo "Sorry First set variable"
else
    echo $var  
fi

- script

customPrompt='yes';
syntaxOn='no';
function my_func 
{
      if [[ ${!1} ]];then 
          echo "$1 is set to ${!1}";
      else
         echo "$1 is not set";
      fi
}
my_func customPrompt
my_func syntaxOn
my_func foobar

:

customPrompt is set to yes
syntaxOn is set to no
foobar is not set

, .

+2

, ​​ ( ), :

function my_func {
    if [[ -z ${!1+.} ]]; then 
         echo "$1 is not set."
    elif [[ ${!1} == yes ]]; then
         echo "$1 is set to yes"
    else
         echo "$1 is set to \"${!1}\"."
    fi
}
0

...

Bash - . , -, Bash . script , .

$ set -x
set -x
$ foo=bar
+ foo=bar
$ echo "$foo"
+ echo bar
bar
$ set +x

set -x . , . , foo=bar, echo $foo. echo $foo. echo $foo bar. echo , , bar ( $foo).

. , . echo *.txt, echo *.txt, .

, script:

#! /bin/sh
if [[ $1 = "*" ]]
then
    echo "The first argument was '*'"
else
    "I was passed in $# parameters"
fi

script:

$ test.sh *
I was passed in 24 parameters

? script a *? . * , . script *. :

$ test.sh '*'
The first argument was '*'

-. ( , ).

, , , :

$ test.sh '$foo'

:

if [[ $1 != ${1#$} ]]
then
    echo "The first parameter is the variable '$1'"
fi

${1#$} , ${var#pattern}. pattern $var. $1 $, . :

if [[ $foo != foo ]]

.

, :

  • First, you must stop shell interpretation from your variable. This means that you need to use single quotes around the name.
  • You need to use pattern matching to make sure that the first parameter starts with $.
  • Once you do this, you can use your c variable ${$1}in the script.
0
source

All Articles