0
function install(){

V1=version1
V2=version2
DIR=folder

echo "Select version by number to download ${reset}:"
echo "$V1"
echo "$V2"
read version

case $version in

  1) if [[ ! -d $DIR/$V1 ]]; 
        then echo "Creating $DIR/$V1 folder."
            mkdir -p $DIR
        else
            echo "$DIR/$V1 Folder exists"
     fi

  2) if [[ ! -d $DIR/$V2 ]]; 
        then echo "Creating $DIR/$V2 folder."
            mkdir -p $DIR/$V2
        else
            echo "$DIR/$V2 Folder exists"
      fi
}

opt=$1
case $opt in
    --install | -i) install ;;
    *) echo "Exit"
       exit ;;
esac

Now the only thing that changes in case $version is the variable $V[1-2]

How could i read a template and put in there?

I tried:

lines=('if [[ ! -d $DIR/$V0 ]]; 
        then echo "Creating $DIR/$V0 folder."
            mkdir -p $DIR
        else
            echo "$DIR/$V0 Folder exists"
     fi')

and then

1) $(lines | sed -e "s/\&V0/\$V1/g")

But that didn't work. Any ideas? Any help is appreciated! Thank you!

EDIT: I am sorry, but after reading the answers i regret i was not clear enough. The main part i am asking for was:

  1. I have a text with "if then else" conditions
  2. That one should be somewhere as a template.
  3. Then this template should be read and the variables should be exchanged by the correct ones.
  4. That way i have only one script and can be reused by just putting it into "case" with little modification through sed for example.

Is that possible?

Megavolt
  • 13
  • 3
  • 2
    Please include a [mre]. – oguz ismail Aug 06 '20 at 05:17
  • 2
    What does "transclude" mean? – Barmar Aug 06 '20 at 05:39
  • I am sorry, i really tried to make it tiny as possible. – Megavolt Aug 06 '20 at 05:45
  • @Barmar https://en.m.wikipedia.org/wiki/Transclusion – kvantour Aug 06 '20 at 06:17
  • @Bramar and Shawn Thank you! But i better create a new post which is more specific. What is the best behavior here? Should i delete this one or should it stay? – Megavolt Aug 06 '20 at 06:29
  • Executing shell code stored as text is difficult to do right except in simple cases (and this isn't a simple case). I'd look at other ways to do it, such as using a function for the near-common code, and using a single array instead of a bunch of similarly-named variables. So you'd have a function like `createdir() { if [[ ! -d "$DIR/$V[$1]" ]]; ...` and call it with `createdir "$version"` – Gordon Davisson Aug 06 '20 at 06:46
  • @Megavolt did you already create a new post? – kvantour Aug 06 '20 at 06:54
  • @kvantour no not yet – Megavolt Aug 06 '20 at 06:58
  • @GordonDavisson that is neat and simple. Thanks for sharing :-) I am not really new to bash, but trying to be better. – Megavolt Aug 06 '20 at 07:03
  • @kvantour I created a new question with more specific information: [https://stackoverflow.com/questions/63279474/reuse-a-function-with-modification-in-case] – Megavolt Aug 06 '20 at 08:18

2 Answers2

2

Use the select builtin to generate a prompt and menu of available of options:

#!/usr/bin/env bash

declare -a versions=(version1 version2)
dir=folder

PS3="Select version by number to download: "
select version in "${versions[@]}"; do
    if [[ -z $version ]]; then
        echo "Invalid version!"
    elif [[ ! -d $dir/$version ]]; then
        echo "Creating $dir/$version folder"
        mkdir "$dir/$version"
        break
    else
        echo "$dir/$version already exists."
        break
    fi
done
Shawn
  • 47,241
  • 3
  • 26
  • 60
0

Assign either $V1 or $V2 to another variable, and use that in the common code

case $version in
    1) v=$V1 ;;
    2) v=$V2 ;;
    *) echo "Invalid version"; exit ;;
esac

if [[ ! -d "$DIR/$v" ]]; 
then 
    echo "Creating $DIR/$v folder."
    mkdir -p "$DIR/$v"
else
    echo "$DIR/$v Folder exists"
fi
Barmar
  • 741,623
  • 53
  • 500
  • 612