Pass arguments to bash script

myscript.sh

#!/bin/bash

ARGA=$1

if [ "${ARGA}" = "jsmith" ] ; then
        echo "${ARGA}:Confirmed"
else
        echo "${ARGA}:Unconfirmed"
fi

when I run the above script from the command line, i run it as:

./myscript.sh jsmith

now some times, i need to runn it this way:

sed -n 1,9p myscript.sh | bash

notice, there's no where for me to pass whatever argument i need to pass in this command.

does anyone know of a way around this? i want to be able able to pass the "jsmith" as an argument when using the sed command.

my weak attempts at this were:

sed -n 1,9p myscript.sh | bash jsmith 

and

sed -n 1,9p myscript.sh | bash $* 

OK, good.

Why in the world do you want to do this? The stream editor is not a shell; you aren't changing anything in the script; you are just making it hard to pass an argument to the script and making it EXTREMELY inefficient.

You have a working script. You know how to pass parameters to it. What are you trying to do with sed that will be different that what was already working for you without using sed???

thanks don. i do understand it can be considered as inefficient. but i'm working on something that uses the idea behidn this for a different purpose. i just wanted to know if what i was trying to do is doable. i wouldn't do this with any of my other scripts. just this one in particular

If you explain what "different purpose", there is probably an easy way to do it in the shell script itself.

I repeat: What are you trying to do?

this is related to this thread:

i would like to be able to plug in as many arguments as i want to in replacement of jsmith.

Self modifying code is generally not a good idea. Would you consider something like the following instead?

#!/bin/bash
IAm=${0##*/}
IAmDone=".$IAm"
ARGA=$1

if [ -f "$IAmDone" ]
then
        date
        exit
fi

if [ "${ARGA}" = "jsmith" ]
then    echo "${ARGA}:Confirmed"
        touch "$IAmDone"
else    echo "${ARGA}:Unconfirmed"
fi

This will work like your script originally worked until it confirms that jsmith was passed in as an argument. Any time it is called after that, it will just print the current date and exit. For example, the following commands:

./myscript.sh abc;./myscript.sh jsmith;./myscript.sh abc;./myscript.sh jsmith

produces the output:

abc:Unconfirmed
jsmith:Confirmed
Sun Jul 14 19:53:06 PDT 2013
Sun Jul 14 19:53:06 PDT 2013
1 Like

im curious, why isn't a self modifying code a good idea?

Self-modifying code isn't a good idea in general because:

  1. It is harder to maintain. (In your case you're trying to modify lines 1-9 in your shell script. If someone adds a couple of more lines to your script [such as a comment saying what the script does]; the modification no longer works.)
  2. If the size of the file being executed changes while it is running (which is true with what you were doing) and the size of the file is more than one buffer full when when the interpreter reads in the program, the interpreter may skip some characters in the script or read multiple copies of some characters in the script.
  3. If something goes wrong while updating the script (e.g., running out of space), you may end up with a program that only has half of your changes (leaving you with no obvious way to get back to where you were before you started making changes nor to get to where you wanted to end up).
  4. If you have to restore a back-up copy of your script, will it do what you want?
1 Like
  1. If you look at your script 1 month from now, you will have no idea why you're doing what you're doing.