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:
- I have a text with "if then else" conditions
- That one should be somewhere as a template.
- Then this template should be read and the variables should be exchanged by the correct ones.
- 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?