Ben Durkin
Ben Durkin

Reputation: 447

Passing in parameter to bash script

I have this simple bash script:

#!/bin/sh
(echo "AUTH xxx xxx"
sleep 3
number=0161XXXXXXX 
echo "ACTI $number"
sleep 3
echo "SET $number 1 S:[email protected]"
sleep 3
echo "STAT $number"
sleep 3
echo "QUIT") | telnet xxx.xxx 777

I want to pass the number in as a parameter when I call the script, i.e.

bash number.sh 0161XXXXXXX

How can I do that?

Thanks

Upvotes: 0

Views: 57

Answers (2)

Javier Cortejoso
Javier Cortejoso

Reputation: 9146

From bash man page:

Arguments

If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash's exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script.

So the first argument can be referred as $1, the second as $2 (until $9, if more you need to process it in other way such as using shift...)

Upvotes: 1

P.P
P.P

Reputation: 121357

Use positional parameters. You could also directly use $1 instead storing in a variable.

#!/bin/sh

arg=$1
(echo "AUTH xxx xxx"
sleep 3
number=$arg
echo "ACTI $number"
sleep 3
echo "SET $number 1 S:[email protected]"
sleep 3
echo "STAT $number"
sleep 3
echo "QUIT") | telnet xxx.xxx 777

Upvotes: 3

Related Questions