Array problem in Bash Script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Array problem in Bash Script
# 1  
Old 10-16-2016
Linux Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with :

Code:
#!/bin/bash

days=(9 8 7 6 5)

for (( i = 0 ; i < ${#days[@]} ; i++ ))
  do (( days[$i]=${days[$i]} - 1 )) 
  done

This leaves me with this :
Code:
$ 8 7 6 5 4

How do I start with days=(8 7 6 5 4) the next time the script is run instead of days=(9 8 7 6 5)?

I am using Linux Mint 18 (Cinnamon) and the latest version of Bash

Thank You in advance for any and all suggestions.
Cogiz

Last edited by Scrutinizer; 10-16-2016 at 11:15 PM.. Reason: code tags
# 2  
Old 10-16-2016
Your current code explicitly initializes the array using:
Code:
days=(9 8 7 6 5)

If you want to update the array and load those updated values the next time you run your script, you need to save the updated values in a file before you exit your script, and when you run your script you need to load the array from that file; not with the initialization step shown above.

Note that you also need to be sure that you never run more than one copy of your script at a time. If you have two copies of the script running concurrently, you have to be absolutely sure that they destroy the file saving your stored data by performing concurrent updates to that file.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-17-2016
Why don't you create the array dynamically, by e.g. using the output of seq? You'd still need to keep the starting value somewhere.

What kind of logics do you wish to apply when the days reach zero? Continue into the negative numbers? Reset to sth. like 30/31?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-17-2016
Array problem in Bash script

Thank you for suggestions. I am writing a little script that will keep track of current medications (dosage, supply, refills, etc.) and send an email when it is time for the prescription to be refilled, e.g. 30 day supply with 3 refills: when 30 gets to 0 it will reset
to 30 and refills will decrease to 2. I will try saving to file then reload upon next execution of script. I will post if successful or not. Thanks again.
Cogiz
# 5  
Old 10-30-2016
Array problem in Bash script

I would like to officially thank all the people who offered advice on my problem. I can say for sure that the problem has been solved by populating an empty array from a txt file and saving output to same txt file for use when the script is run next time.

best regards to all,
Cogiz

Last edited by Don Cragun; 10-30-2016 at 04:44 PM.. Reason: Remove duplicated text.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

3. UNIX for Beginners Questions & Answers

Scanning array for partial elements in Bash Script

Example of problem: computerhand=(6H 2C JC QS 9D 3H 8H 4D) topcard=6D How do you search ${computerhand} for all elements containing either a "6" or a "D" then save the output to a file? This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash. Any... (2 Replies)
Discussion started by: cogiz
2 Replies

4. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

5. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

6. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

7. Shell Programming and Scripting

problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle... (4 Replies)
Discussion started by: adlmostwanted
4 Replies

8. Emergency UNIX and Linux Support

Problem with Array in Script

Below is my script. This script is getting an error code such as this one. fileListener.bat: entityArray=craig.uss@pnc.com: not found craig.uss@pnc.com fileListener.bat: entityArray=duns_noncusts.txt: not found duns_noncusts.txt fileListener.bat: entityArray=duns_misc.cpy: not found... (4 Replies)
Discussion started by: mkjp
4 Replies

9. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

10. Shell Programming and Scripting

bash array problem

hi friends., i have two files yy.dat and mm.dat containing 110 elements in each if i read them into variables it is just showing only 4 elements instead of 110 elements My script is like this ################################## /bin/bash declare -a yy=(`cat yy.dat`) echo "No of values in... (1 Reply)
Discussion started by: yagnesh
1 Replies
Login or Register to Ask a Question