LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2022, 10:57 AM   #1
GlockG20
LQ Newbie
 
Registered: Nov 2022
Posts: 6

Rep: Reputation: 0
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...
 
Old 11-12-2022, 01:31 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,738

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Quote:
Originally Posted by GlockG20 View Post
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.
 
Old 11-12-2022, 02:03 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,985

Rep: Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678
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/

Last edited by michaelk; 11-12-2022 at 02:23 PM.
 
Old 11-12-2022, 03:15 PM   #4
GlockG20
LQ Newbie
 
Registered: Nov 2022
Posts: 6

Original Poster
Rep: Reputation: 0
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!
 
Old 11-13-2022, 02:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,988

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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)
 
Old 11-13-2022, 03:04 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,636

Rep: Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 05:22 AM
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 02:36 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM
making a bash script student04 Linux - Software 5 01-11-2005 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration