AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / ubuntu / Perguntas / 1490238
Accepted
keldorn
keldorn
Asked: 2023-10-25 00:18:55 +0800 CST2023-10-25 00:18:55 +0800 CST 2023-10-25 00:18:55 +0800 CST

Função Bash que verifica se o argumento de string é um nome de variável existente (definida) ou não existente (não definida)

  • 772

Eu gostaria de escrever uma função bash no Ubuntu 22.04 que funcione assim

Var1=100

is_variable_set "Var1" //Returns 0 (variable exists ie is set)

unset Var1

is_variable_set "Var1" //Returns 1 (variable does not exist ie is not set)

Minha tentativa atual não está funcionando:


is_variable_set()
{
    if [ -z "${1+some_string}" ]
    then
        echo "${1} is unset!"
        return 1
    fi

   return 0
}

Tentei usar a abordagem desta questão: https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash

Mas sem sucesso :(

command-line
  • 2 2 respostas
  • 30 Views

2 respostas

  • Voted
  1. Best Answer
    steeldriver
    2023-10-25T00:48:31+08:002023-10-25T00:48:31+08:00

    O shell bash, na verdade, fornece um teste integrado para isso. De help test:

    Other operators:
    
      -o OPTION      True if the shell option OPTION is enabled.
      -v VAR         True if the shell variable VAR is set.
      -R VAR         True if the shell variable VAR is set and is a name
                     reference.
    

    Então por exemplo

    $ var=
    $ [[ -v var ]] && echo set || echo not set
    set
    $ 
    $ unset var
    $ [[ -v var ]] && echo set || echo not set
    not set
    
    • 2
  2. keldorn
    2023-10-25T00:44:04+08:002023-10-25T00:44:04+08:00

    Acabei de pesquisar um pouco mais fundo no Google e vi este código:

    is_variable_set()
    {
        local -n refToCheck=$1
    
        if [ -z "${refToCheck+some_string}" ]
        then
            echo "${1} is unset!"
            return 1
        else
            echo "${1} is SET!"
            return 0
        fi
    }
    

    Parece funcionar bem :)

    • 0

relate perguntas

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Existe um comando para listar todos os usuários? Também para adicionar, excluir, modificar usuários, no terminal?

    • 9 respostas
  • Marko Smith

    Como excluir um diretório não vazio no Terminal?

    • 4 respostas
  • Marko Smith

    Como descompactar um arquivo zip do Terminal?

    • 9 respostas
  • Marko Smith

    Como instalo um arquivo .deb por meio da linha de comando?

    • 11 respostas
  • Marko Smith

    Como instalo um arquivo .tar.gz (ou .tar.bz2)?

    • 14 respostas
  • Marko Smith

    Como listar todos os pacotes instalados

    • 24 respostas
  • Martin Hope
    Flimm Como posso usar o docker sem sudo? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    led-Zepp Como faço para salvar a saída do terminal em um arquivo? 2014-02-15 11:49:07 +0800 CST
  • Martin Hope
    ubuntu-nerd Como descompactar um arquivo zip do Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    TheXed Como instalo um arquivo .deb por meio da linha de comando? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    Ivan Como listar todos os pacotes instalados 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    David Barry Como determino o tamanho total de um diretório (pasta) na linha de comando? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "Os seguintes pacotes foram retidos:" Por que e como resolvo isso? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford Como os PPAs podem ser removidos? 2010-07-30 01:09:42 +0800 CST

Hot tag

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve