Shell Bash, How to pass given from Shel as parameter

Asked

Viewed 8,165 times

1

i need to run a file file1.sh, but in addition I need to take the data that comes after it, for example: file 1.sh 171.55.8.45, this ip that comes after, in the same line of 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 the read.

2 answers

2

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

$ ./myscript.sh param1 param2

So,

$0 = myscript.sh 
$1 = param1
$2 = param2

0


Your script getting the IP as parameter is thus:

#!/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 skip ahead of your scipt and make the call regarding 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"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.