简体   繁体   中英

Bash script not creating files

Wrote a Bash script to create two files, each with a list of dates, to be used later. The script requires three parameters: a data subject, a minimum date, and a maximum date. Here's my script:

    #!/bin/sh
        dataSubj=$1
        minDate=$2
        maxDate=$3

        echo -e "my variables:\nsubject:\t$dataSubj\nstart:\t$minDate\nend:\t$maxDate"
       //Wrote the above line for debugging

        configDir=/opt/site1/ETL/MFGEDW/config/MERS2
        dateCount=1
        addTime=00:00:00
        fromDates=$dataSubj_fromDates.txt
        toDates=$dataSubj_toDates.txt

        cd $configDir

        echo "Creating fromDates file and adding $minDate"
        echo -e "$minDate $addTime" > ./$fromDates

        echo "Creating toDates file"
        >./$toDates

        while [[ $minDate < $maxDate ]]
            do
                minDate=$(date -d "$minDate 7 days" +%Y-%m-%d)
                ((dateCount++))
                if [[ $minDate < $maxDate ]]; then
                    echo "Adding $minDate to fromDates file"
                    echo -e "$minDate $addTime," >> ./$fromDates
                fi

                echo "Adding $minDate to toDates file"      
                echo -e "$minDate $addTime," >> ./$toDates  

                echo "$dateCount dates total"   
        done

        exit $dateCount

My issue is that instead of having two files with the desired dates, I have one hidden file with all the dates that should have been written in the two files. I'm fairly new to scripting, but modeled this after other scripts that I've used and know work. Is there something I'm missing or added unnecessarily? Thanks in advance.

This is your problem:

fromDates=$dataSubj_fromDates.txt
toDates=$dataSubj_toDates.txt

Bash doesn't know you mean dataSubj is the name of the variable. You're trying to use two different variables:

dataSubj_fromDates
dataSubj_toDates

Pretty sure those don't exist. Note '.' is a stopper for variable naming. Try using:

fromDates=${dataSubj}_fromDates.txt
toDates=${dataSubj}_toDates.txt

Next time print all variables when debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM