LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-10-2017, 01:57 PM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
bash - variables and adding another number to the variable


hi all,

i'm part way through building a script to process some files but i'm having an issue with the following.

so i'm finding the files in a directory by searching for a specific string in the file name.

what i need to do is after if finishes running through the script i need to then ammend the variable from 01 to 02 then 03 and so on so that next time it runs it picks up the next file in sequence.

so i'm using the following at the moment

Code:
sed -i "s/FILE=$FILE/FILE=$ADD/" temp.sh
where ADD is doing the following
Code:
ADD=$(expr $EP + 1)
the FILE variable at the start is just FILE=01

now this is working for me but the output is coming out with 1 instead of 01.

does anyone know how i can get it to add the 0 at the end upto 09?

or if there is a better way to action this?
 
Old 02-10-2017, 02:11 PM   #2
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
can't test this at the second but thinking something along the lines of...

sed -i "s/FILE=$FILE/FILE=$(printf "%02d" $ADD)/" temp.sh

may work.
 
Old 02-10-2017, 03:34 PM   #3
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
thanks for the info.

just tested and it does seem to work as expected

Thanks again
 
Old 02-10-2017, 05:16 PM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
no probs , if that has sorted your problem then please mark the post as helpful and the thread as solved . Some people are just looking to help with unsolved issues and so saves their time. Printf is a good thing to learn for when you want specific formats like this .

Last edited by r3sistance; 02-10-2017 at 05:17 PM.
 
  


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] adding 1 to a variable in bash Garrett85 Programming 4 01-09-2013 10:37 AM
Adding variables then output to another variable stevo520 Linux - Newbie 2 07-06-2012 08:06 PM
Counting the number of exit variables - Bash. 0bfuscated Programming 3 07-02-2010 12:43 PM
[SOLVED] Bash: Making a Variable out of two other variables. youarefunny Programming 4 03-29-2010 07:56 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 02:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:51 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration