0

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

1
  • Paste your headline in google and click on first result. Commented Apr 19, 2015 at 18:25

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.