0

I have a shell script that has a on/off switch inside. I programmed the Housekeeping.sh to execute this certain line of code if the value is at 1 and don't execute it if it's at 0. The following is the code on my Housekeeping.sh:

ARCHIVE_SWITCH=1
if [[ $ARCHIVE_SWITCH -eq 1 ]]; then
    sqlplus ${BPS_SCHEMA}/${DB_PASSWORD}@${ORACLE_SID} @${BATCH_HOME}/sql/switch_archive.sql
fi

Now I want to create another shell script file that I'll execute to automatically change the variable ARCHIVE_SWITCHequals to 0 everytime I execute this script. Is there any other way that I can change the value of the variable ARCHIVE_SWITCH from another shell script file that I'll execute manually?

4
  • 1
    You will need to export the ARCHIVE_SWITCH variable from the parent shell of each of the two scripts you want to have access to the variable. Generally either create a wrapper script that exports the variables and then starts each shell script in the background, or you can export in your .bashrc and have any script make use of it. (rule a child process may never change the environment of its parent -- so you need the parent to make the environment variable available) Commented Dec 21, 2022 at 8:11
  • ... Or, you can create a function or alias which sets the variable in your current shell, or create a script file which you manually source from that shell. Commented Dec 21, 2022 at 8:47
  • In any event, probably remove the assignment from the housekeeping script, or change it to one which supplies a default value but does not override the variable if it is already set. Commented Dec 21, 2022 at 8:49
  • Change ARCHIVE_SWITCH=1 to : ${ARCHIVE_SWITCH=1}. If it's not set in the environment, it will be set to 1. If it is already set in the environment, this will be a no-op. Commented Dec 21, 2022 at 16:35

1 Answer 1

1

I'd use an option to the script:

bash housekeeping.sh      # default is off
bash housekeeping.sh -a   # archive switch is on
#!/usr/bin/env bash

archive_switch=0

while getopts :a opt; do
    case $opt
        a) archive_switch=1 ;;
        *) echo "unknown option -$opt" >&2 ;;
    esac
done
shift $((OPTIND-1))

if ((archive_switch)); then
    sqlplus ...
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.