Pular para o conteúdo

Manipulação de variáveis em shell

Responder tópico
  • Denunciar
  • Indicar

1. Manipulação de variáveis em shell

Enviado em 14/07/2024 - 15:48h

Boa tarde

Criei uma variável com o seguinte conteúdo:
nomesif=$(ip -br link | awk '{print $1}').
Como resultado, a variável nomesif recebeu os conteúdos: lo enp6s0, que são os nomes das minhas interfaces de redes.
Agora eu quero separar a variável nomesif em duas:
loif, que deve receber o nome da interface lo
E
ethif, que deve receber o nome da interface enp6s0.

As palavras não estão em linhas separadas mas sim em apenas uma linha.

É como se eu tivesse feito a declaração nomesif="lo enp6s0"

Diretamente

Responder tópico

2. Re: Manipulação de variáveis em shell

Enviado em 14/07/2024 - 15:48h

{
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

3. Re: Manipulação de variáveis em shell

Enviado em 14/07/2024 - 16:42h

Agora que já obteve a variável "nomesif", pode separar elas com:

echo "$nomesif" | awk 'NR==1'

e com

echo "$nomesif" | awk 'NR==2'

Aí, se quiser armazenar nas variáveis que citou, seria:

loif=$(echo "$nomesif" | awk 'NR==1')

e

ethif=$(echo "$nomesif" | awk 'NR==2')

Pode testar depois com:

echo "$loif"
echo "$ethif"



4. Re: Manipulação de variáveis em shell

Enviado em 14/07/2024 - 18:12h

#!/bin/bash

nomesif="$(ip -br link | awk '{print $1}')"
loif=$(echo "${nomesif}" | cut -d" " -f1)
ethif=$(echo "${nomesif}" | cut -d" " -f2)

echo "${nomesif}
${loif}
${ethif}"

5. Script

Enviado em 14/07/2024 - 18:34h


#!/bin/bash

nomesif=$(ip -br link | cut -d" " -f1)


Ioif=$(echo $nomesif | cut -d" " -f1)
ethif=$(echo $nomesif | cut -d" " -f2)

echo "Placa : $Ioif"
echo "Placa : $ethif"



Com array


#!/bin/bash

nomesif=($(ip -br link | cut -d" " -f1))

Ioif=${nomesif[0]}
ethif=${nomesif[1]}

echo "Com echo."
echo "Placa : $Ioif"
echo "Placa : $ethif"

# Cria 20 traços.
echo $(seq 0 20 | xargs | tr -d [0-9] | tr " " "-")

echo "Com for."
for i in ${nomesif[@]} ; do
echo "Placa : $i"
done

Responder tópico

Responder tópico

Entre na sua conta para responder.

Fazer login para responder