S
S
Sergey Sashkin2018-11-08 12:14:31
linux
Sergey Sashkin, 2018-11-08 12:14:31

How to set bash variable?

I have a deployment script, the task is to substitute the value for example deploy.sh prod (prod is set in ENV).
If you do not substitute a value, then the script fulfills the HOST specified in it by default. How to implement it?

#!/bin/bash

HOST=$PROD
DEST="/var/www/agr/data/www/wiki/public/"

tar -cvzf wiki.tar.gz build/

echo 'Archived'


echo 'Coping sourcee'

scp wiki.tar.gz ${HOST}:${DEST}

echo 'Copied'

echo 'Unziping'

ssh ${HOST} "tar -xvf ${DEST}/wiki.tar.gz -C ${DEST}&& rm ${DEST}/wiki.tar.gz && rm -r ${DEST}/wiki/ && mv ${DEST}/build ${DEST}/wiki"

echo 'unziped'

echo Done

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2018-11-08
@xotkot

script (stripped down) deploy.sh

#!/usr/bin/env bash

_HOST="$1"
if ; then
  _HOST="host2"
fi

echo "Используемый хост: $_HOST"

script work:
$ export prod="host1"
$ deploy.sh $prod
Используемый хост: host1

or directly:
$ deploy.sh "host1"
Используемый хост: host1

$ deploy.sh
Используемый хост: host2

I
ISE73, 2018-11-08
@ISE73

Something like this:

HOST=$PROD
if 
then
    HOST = "default host"
fi

or like this:
HOST = $([ "$PRODx" == "x" ] && echo "default host" || echo "$PROD")

S
Saboteur, 2018-11-08
@saboteur_kiev

If you need to check the MYVAR variable and if it is empty. then set it to a default value, you can simply check it, for example, in these ways:
1.

if ; then
  MYVAR="default value"
fi

2.
echo "MYVAR is: ${MYVAR:="default value}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question