Search code examples
shellsh

Changing the value of the variable from another shell script


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?


Solution

  • 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