1

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.

2
  • What's the name of the file? give that and the arguments to your script. Commented Dec 15, 2016 at 18:49
  • @kabanus The name of the hidden file is .txt, arguments are: mySubject, 2016-10-10, 2016-12-10 Commented Dec 15, 2016 at 18:52

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That did it. Do you have a good source where I can read up on when I need to use { }? I've used scripts others wrote where they create files with echo -e "whatever" > ./$variableName.txt. They are ksh scripts, not sure if that makes a difference.
It's not a big 'law' so I don't think there is something that focuses on this. Basically if your variable is part of a string then you need it - this is true for all scripting languages. Otherwise the interpreter thinks the name of the variable is the entire string.

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.