LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script is making me zany! (https://www.linuxquestions.org/questions/programming-9/bash-script-is-making-me-zany-4175718714/)

GlockG20 11-12-2022 09:57 AM

Bash script is making me zany!
 
Below is the script as I have it now, I am still working on building it so it's not complete. Anyhow there is one area that is not working no matter how I try to get it to... and it's driving me crazy!

The setup/function...
I have a text file called... txtBuild.txt that contains the last build number of a project I am working on. Currently I have to manually tar the existing instance of the application, then manually copy over the new build so I can start using the new build. I am writing this script to use the last build number ( formatted this way YYYYMMDD-### ) for the tarball of the last build. Then the new build number is created ( that part works fabulously! ) and written into txtBuild.txt. But it isn't. And this is the problem I have been trying to sort out!

The script...
Code:

#!/bin/bash
clear
echo "Stand-by while installing updates."
# read in the build number file...
# this is the build number of the current application in use...
file="txtBuild.txt"

while read -r line; do
# read in value and extract the build number to be incremented by 1...
    nline="${line%%\"*}" #strip everything from first " seen to the end
    newline="${nline:10}" # gets everything to the right of the first 10 characters...
    i=$(($newline+1)) # increment value by 1... this is the new build numer suffix example YYYYMMDD-032

# get year and month for the rest of the new build number...
  YY= date +%Y%m%d"-0$i" # This works fine...
  build="$YY"
  echo $build # This works in that is will display the new build number to the terminal

# write that value back out to the file...
  echo $build > "txtBuild.txt" # But this doesn't work as it just deletes the content and does not write the new value to the file... ggrrrrrr!


# create the tarball name with the value and date
tar -cvpf $line.tar.gz -T TarList.txt # This works in that the tarball is created with the old build number as the name... perfect!
 
done <$file

Any ideas why I can't write the new build number to the text file? It's really making no sense to me at all why it isn't working...

ntubski 11-12-2022 12:31 PM

Quote:

Originally Posted by GlockG20 (Post 6391873)
Code:

  YY= date +%Y%m%d"-0$i" # This works fine...
  build="$YY"
  echo $build # This works in that is will display the new build number to the terminal


You seems to be missing $() or `` around the call to date, so there is no way the code you posted could possibly be described as "works fine". Maybe you have multiple copies of the script file and are editing the wrong one? Or perhaps you assigned the YY variable outside of the script, and didn't notice that the script fails to update it correctly? Oh, or you mistook the output of date for the output of echo $build (which would actually be empty). Try doing
Code:

echo "the build number is [[$build]]"
to clarify.

michaelk 11-12-2022 01:03 PM

I agree that due to several syntax errors YY is not being assigned a value.
bash does not allow spaces before or after a variable assignment.
You need to use command substitution as posted above to assign the output of the date command to your variable.

Code:

    i=$(($newline+1)) # increment value by 1
    newi=$(printf "%03d" $i) # pad i with leading zeros.
    build=$(date +%Y%m%d-$newi) # assign build to your new number.
    echo $build > "txtBuild.txt"


Again as posted your confusing the output of the date command and that echo $build is actually only a line feed.

You can check for syntax errors at
https://www.shellcheck.net/

GlockG20 11-12-2022 02:15 PM

Well, thank you gentlemen! Much appreciation on the lesson and help! I am still learning the nuances of BASH. While I have been using Linux since 1999... I never got into scripting much. Now... I am finally finding a use for it so I am learning!

grail 11-13-2022 01:59 AM

While a healthy project, I am curious why you don't just place the latest build number in the file name and then do your operations on it to extract and make the next build number? (just a thought)

MadeInGermany 11-13-2022 02:04 PM

And it's not good to write to a file you are currently reading from.
Because it has got only one line, instead of a while loop use a one-time
Code:

read -r line < $file


All times are GMT -5. The time now is 03:37 PM.