Shell Bash, How to pass data from shel as parameter

0

I need to run a file1.sh file, but I also need to get the data that comes after it, for example: file1.sh 171.55.8.45, that ip that comes after, on the same line as the file, before pressing enter , I need this ip to be read and passed as a parameter to another action within the code, but without using read .

    
asked by anonymous 15.12.2017 / 13:43

2 answers

1

There are standard variables that hold the parameters that you pass to a script. If you run

$ ./myscript.sh param1 param2

So,

$0 = myscript.sh 
$1 = param1
$2 = param2
    
15.12.2017 / 13:52
0

Your script getting the IP as a parameter looks like this:

#!/bin/bash

VARIAVEL_IP="$1"
# $1 se refere ao primeiro parâmetro passado na command line

# TESTE
echo "IP passado como parâmetro: $VARIAVEL_IP"

If you want to use other parameters, just pass the front of your scipt and make the call referring to the position of the parameter.

Example:

Command Line:

Script.sh "p1" "p2" "p3"

Script:

#!/bin/bash

# Atribuindo p1
POSICAO_01="$1"
# Atribuindo p2
POSICAO_01="$2"
# Atribuindo p3
POSICAO_01="$3"
    
18.12.2017 / 18:31