Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Values rotation in array with bash


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Values rotation in array with bash

Hello Smilie

I created a little script that allow to make a rotation of values in an array. The goal was to shift the values to the right and that the last value of the array became the first value in order to create a rotation.

The purpose of the exercice was to do it without using a temporary array but to create a temporary variable in which I can put one of the values of the array then shifted all values to the right and then, put the temporary variable in the array.

Here my script :

Code:
#!/bin/bash

clear
declare -a array

read -rp " How many cases ? " box
read -rp " shift : " n
  
array=( $(seq 1 "$box"))
tmp=${array[-2]}

for((i=$tmp;i>0;i--))
do
 	array[$i]=${array[$i -1]}
done

array[0]=$tmp

for((i=0; i<$box;i++))
do
	array[$i]
done

echo " Original array : " ${array[*]}
echo " Temporary variable : " $tmp 
echo " Array shifted : ${array[*]}"

But with 2 places to shift, the result is :
Code:
Original array : 9 1 2 3 4 5 6 7 8 9
Temporary variable : 2
Array shifted : 9 1 2 3 4 5 6 7 8 9

Or I want :
Code:
Original array : 1 2 3 4 5 6  7 8 9 10 
Temporary variable : 2
Array shifted : 9 10 1 2 3 4 5 6 7 8

I can't set up this little script... Can you help me to solve it ?
# 2  
Welcome to the forum.


Is that a homework / classwork assignment?
# 3  
Quote:
Originally Posted by RudiC
Welcome to the forum.


Is that a homework / classwork assignment?

Yes, absolutely.

The purpose of the exercice is to be able to manage array ( in C# normally ). I'm able to do this exercice in C#, but I preparing my self to becoming an Linux system-administrator so I learn to use bash script. I have the possiblity to use this sript language in class, so I want to know how to do this exercice with bash.

It's possible to help me for this ?
# 4  
There's a special subforum: https://www.unix.com/homework-and-coursework-questions/.
Please reopen your request there, filling in the entire form.
Thanks.
This User Gave Thanks to RudiC For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Values rotation in array with bash
Nexy
Hello :) I created a little script that allow to make a rotation of values in an array. The goal was to shift the values to the right and that the last value of the array became the first value in order to create a rotation. The purpose of the exercice was to do it without using a temporary...... Homework & Coursework Questions
6
Homework & Coursework Questions
Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...
briandanielz
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...... Shell Programming and Scripting
4
Shell Programming and Scripting
Passing values from file into array in Bash
ShiGua
Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"...... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers
Greping array values in Bash like Perl
paragkalra
Hi, Is there an easy way to simulate following Perl code in Bash. if ( grep {$my_value eq $_} @ARGV ){ print "Do Something\n"; } else { die "Invalid value"; }... Shell Programming and Scripting
0
Shell Programming and Scripting
PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.
zeekblack
Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array...... Shell Programming and Scripting
1
Shell Programming and Scripting

Featured Tech Videos