0

New bash user here, I'm trying to accomplish something simple but can't seem to figure it out

I want to run a script (curl.sh) using a command line argument that will change every time to supply a new IP to the .sh

So running:

~/Documents$ ./curl.sh 10.0.0.18

Would result in the following being run:

#!/bin/bash
curl 10.0.0.18 blablabla

and then running: ~/Documents$ ./curl.sh 10.0.0.19

Would change the .sh to run:

#!/bin/bash
curl 10.0.0.19 blablabla

I have tried setting variables with this article and arguments using sed in this article but must be getting the syntax wrong

7
  • You use $1 in your script to refer to the first command line argument. curl $1 blahblabla What do you have in your script now? Commented Oct 23, 2023 at 0:41
  • 1
    "I have tried setting variables with this article and arguments using sed in this article but must be getting the syntax wrong" Show us what you tried so someone can explain what is going wrong. Commented Oct 23, 2023 at 0:42
  • I have tried substituting the IP address in the script with $1 and then using a new IP as a command line argument but it doesn't seem to work. When I run the command in shell directly with the IP address I get an "OK" response. When I run the script (having $1 in lieu of an IP) I do not get a response Commented Oct 23, 2023 at 0:57
  • 1
    ip="${1:-10.0.0.18}" will use 10.0.0.18 if no arguments is provided, or it will use whatever is provided as the ip (you should validate it is an IP). Your call to curl is then curl "$ip" blablabla Commented Oct 23, 2023 at 1:11
  • When I run the script with a sh -x to view the command output in shell I can see the IP address is not being substituted by the command line argument, it runs literally "curl $1 blablabla" Commented Oct 23, 2023 at 1:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.