0

There are two shell scripts.

One of which is a source file source.sh. This source file will exist for multiple people each with their own directories.

name = "foo"

The main script main.sh runs the majority of the code. I need to pull through the variable name.

name = "foo"

sed -i "3i\\$name" /main.sh

/scripts/main.sh

sed -i '3d' /main.sh

This part of the code has been written beforehand.

Is it not better practice remove sed and just write:

/scripts/main.sh "name" ?

This is also my first project in Bash so my expertise is limited.

9
  • Can you provide some example scripts that reproduce the situation please? It is very hard to judge without knowing exactly what is going on.
    – terdon
    Mar 9 at 16:06
  • This is confusing. Do you really want to edit /main.sh when you use sed -i? Also ./source.sh "name" does not set any variables in the calling shell. Putting your script in / is also a bad idea.
    – doneal24
    Mar 9 at 16:08
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Mar 9 at 16:12
  • If the question can be rephrased to »Is it a good idea to auto-modify a script when I could call it with a parameter instead?«, the answer is almost certainly »no«.
    – Philippos
    Mar 9 at 16:15
  • 2
    Can you show a small example that reproduces the problem? Don't describe it, actually show code so we can understand exactly what you need.
    – terdon
    Mar 9 at 16:38

2 Answers 2

0

If you can rewrite the script to use parameters instead of script modifications - do so.

There are some legitimate or semi-legitimate cases when you want to modify the script:

  1. The script can have too many parameters which would be inconvenient to pass in command line. In this case it can be easier to do something like
sed -i "s/@PARAM1@/a/" main.sh
sed -i "s/@PARAM2@/b/" main.sh
...
sed -i "s/@PARAM99999@/zzzzzzzz/" main.sh

But in this case it, would be better to put these parameters into a config file and just read it from the script

  1. The script can be part of a huge suit of scripts. There the call to sed can be looped over several scripts which should share the same hardcoded value. This approach often used in various auto-generators and/or build systems.

But in this case the scripts in question are usually done in a dual way - they do read parameters in a normal fashion, but the sed sets the default values. Something like:

#!/bin/bash
usage() {
  echo "-a=N do something (default value @A@)"
  echo "-b=ABC do something else (default value @B@)"
}

A=@A@
B=@B@
while getopts "a:b:" par; do
  case $par in
   a) A=$OPTARG
       ;;
   b) B=$OPTARG
       ;;
    *) usage
       ;;
  esac
done
echo $A
echo $B

If you have multiple such scripts inside a complicated application, it could be very convenient to pass all them through a loop with sed -i "s/@A@/somevalue" $script. And after the initial setup of default values you would use parameters.

  1. The script in question can be not a shell script and not be able to do parameters reading at all. This usually happens with scripts which are by themself are input to some tools (like sqlplus or expect) - the only really legitimate case for "sed is changing script"
0

If you don't want an explicit argument, you may be looking for an environment variable. If one you can use exists, it's in the output of env. Otherwise, you may require your users to set a new one.

The main script needs the name of the people so it can save files to their directories.

Sounds like $HOME or one of the $XDG_* variables: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

Not the answer you're looking for? Browse other questions tagged .