I doing a project for a Unix/Linux class and have a few Arrays in another file, like this
All the arrays should store is a 1 or 0
T1=( 1 1 1 1 1 )
T2=( 1 1 1 1 1 )
T3=( 1 1 1 1 1 )
T4=( 1 1 1 1 1 )
T5=( 1 1 1 1 1 )
However i'm having difficulty editing the arrays to 0 and making changes stick
#!/bin/bash
i=0
for line in `cat JeopardyOut`
do
let i=i+1
#Creating one big Array with all the values from the arrays file
Array[i]=$line
done
cat JeopardyOut
#Parsing the giant Array into the main 5 arrays I'm using
createArray()
{
y=0
for y in 0 1 2 3 4
do
for i in 2 3 4 5 6
do
#echo "${Array[i]}"
T1[$y]=${Array[i]}
done
for i in 9 10 11 12 13
do
#echo "${Array[i]}"
T2[$y]=${Array[i]}
done
for i in 16 17 18 19 20
do
#echo "${Array[i]}"
T3[$y]=${Array[i]}
done
for i in 23 24 25 26 27
do
#echo "${Array[i]}"
T4[$y]=${Array[i]}
done
for i in 30 31 32 33 34
do
#echo "${Array[i]}"
T5[$y]=${Array[i]}
done
done
}
createArray
ArrayNum=$1
Index=$2
There's likely way better ways to do this, However this is what ended up working for me.
#Changing the necessary indexes, this will be used by a completely
#different script
ChangeArray()
{
if [[ $ArrayNum == "1" ]]; then
T1[ $Index ]=0
elif [[ $ArrayNum == "2" ]]; then
T2[ $Index ]=0
elif [[ $ArrayNum == "3" ]]; then
T3[ $Index ]=0
elif [[ $ArrayNum == "4" ]]; then
T4[ $Index ]=0
elif [[ $ArrayNum == "5" ]]; then
T5[ $Index ]=0
else
echo "Invalid Parameters"
fi
}
if [[ $ArrayNum -ne "" || $Index -ne "" ]]; then
if [[ $ArrayNum == "5" && $Index == "5"]]; then
reset
else
ChangeArray
fi
fi
# And the part that's likely at fault for my issue but don't know how I
# should fix it
echo "T1=( ${T1[*]} )" > JeopardyOut
echo "T2=( ${T2[*]} )" >> JeopardyOut
echo "T3=( ${T3[*]} )" >> JeopardyOut
echo "T4=( ${T4[*]} )" >> JeopardyOut
echo "T5=( ${T5[*]} )" >> JeopardyOut
cat JeopardyOut
Something is wrong with the way I am trying to edit the Arrays...
While I can get any index of any of the arrays to 0, I do not know why the 1s I change to 0 turn back into 1 when I rerun the script.
PS. This is a basis class for Linux programming in Sierra, I don't really understand a lot of the bash script other than what I've learned through trial and error.
Maybe this example can help you
#!/bin/bash
while read -r line; do
#Creating one big Array with all the values from the arrays file
Array+=(`grep -oP '\(\K(.+?)(?=\))' <<< "$line"`)
done < JeopardyOut
echo "Giant Array value: ${Array[#]}"
# Or you can clone the arrays in the file
i=0
while read -r line; do
((i++))
eval "T$i=(`grep -oP '\(\K(.+?)(?=\))' <<< "$line"`)"
done < JeopardyOut
echo "This is the contents of array T1: ${T1[#]}"
echo "This is the contents of array T2: ${T2[#]}"
echo "This is the contents of array T3: ${T3[#]}"
echo "This is the contents of array T4: ${T4[#]}"
echo "This is the contents of array T5: ${T5[#]}"
# This can help you to make a new file
# I change some value to our arrays
T1[2]=0
T2[1]=true
# Now let's go to make a new arrays file
echo "T1=(${T1[#]})" > JeopardyOut2
echo "T2=(${T2[#]})" >> JeopardyOut2
echo "T3=(${T3[#]})" >> JeopardyOut2
echo "T4=(${T4[#]})" >> JeopardyOut2
echo "T5=(${T5[#]})" >> JeopardyOut2
echo "This is the content of JeopardyOut2:"
cat JeopardyOut2
Output
$ bash example
Giant Array value: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
This is the contents of array T1: 1 1 1 1 1
This is the contents of array T2: 1 1 1 1 1
This is the contents of array T3: 1 1 1 1 1
This is the contents of array T4: 1 1 1 1 1
This is the contents of array T5: 1 1 1 1 1
This is the content of JeopardyOut2:
T1=(1 1 0 1 1)
T2=(1 true 1 1 1)
T3=(1 1 1 1 1)
T4=(1 1 1 1 1)
T5=(1 1 1 1 1)
$
Related
I wrote a program to sort an array and print the element where the unsorted array and sorted array matches. In theory, this should work, but it isn't. All the elements in the sorted array for some reason combine it all and output an array element of 1.
#!/bin/bash
arr=(6 2 15 90 9 1 4 30 1 3)
function sort(){
local array=($#) max=$(($# - 1))
while ((max > 0))
do
local i=0
while ((i < max)); do
if [ ${array[$i]} \> ${array[$((i + 1))]} ]
then
local t=${array[$i]}
array[$i]=${array[$((i + 1))]}
array[$((i + 1))]=$t
fi
((i++))
done
((max--))
done
echo ${array[#]}
}
arr_sort=($(sort ${arr[#]}))
for ((j=0; j<(( ${#arr[#]} -1 )); j++)); do
for ((k=0; k<(( ${#arr[#]} -1 )); k++)); do
if (( ${arr[j]:-0} == ${arr_sort[k]:-0} )); then
echo ${arr[j]}
break
fi
done
Try this:
#!/bin/bash
arr=(6 2 15 90 9 1 4 30 1 3)
arr_sort=( $(echo ${arr[#]} | tr ' ' '\n' | sort -n) )
for ((j=0; j<${#arr[#]}; j++)); do
if (( ${arr[i]} == ${arr_sort[i]} )); then
echo "Match ${arr[j]} at position $j (starting from 0)"
fi
done
Since there are no matches between the unsorted array
6 2 15 90 9 1 4 30 1 3
and the sorted one
1 1 2 3 4 6 9 15 30 90
in the example you gave, you will have no output.
Let's say I have an array with some numbers, ordered from the lowest to the highest numeral:
root#blubb:~# min=1
root#blubb:~# echo $array
1 2 3 6 16 26 27
I want a bash script to always try the minimum number defined first (in this case min=1) and if that is not possible add 1 and try it again, until it finally works (in this case it would take 4)
I tried a lot with shuf and while-/for loops but could not get it to run properly.
min=1
array="1 2 3 6 16 26 27"
found=0
res=$min
for elem in $array; do
if [[ $elem = $res ]]; then
found=1
continue
fi
if [[ $found = 1 ]]; then
((res+=1))
if ((elem>res)); then
break
fi
fi
done
echo $res
Or with a function
function min_array {
local min array res found
min=$1
shift
array=$*
found=0
res=$min
for elem in $array; do
if [[ $elem = $res ]]; then
found=1
continue
fi
if [[ $found = 1 ]]; then
((res+=1))
if ((elem>res)); then
break
fi
fi
done
echo $res
}
$ min_array 1 1 2 3 6 16 26
4
$ min_array 6 1 2 3 6 16 26
7
$ min_array 8 1 2 3 6 16 26
8
EDIT one case was missing, another version
function min_array {
local min array res found
min=$1
shift
array=$*
found=0
res=$min
for elem in $array; do
if [[ $elem = $res ]]; then
found=1
((res+=1))
continue
fi
if [[ $found = 1 ]]; then
if ((elem>res)); then
break
else
((res+=1))
fi
fi
done
echo $res
}
Try this.
array=(7 5 3 1)
min()
{
local min=$1; shift
local n
for n in "$#"; do
if ((n<min)); then
min=$n
fi
done
echo "$min"
}
min "${array[#]}"
Hello I have the following array:
array=(1 2 3 4 5 7 8 9 12 13)
I execute this for loop:
for w in ${!array[#]}
do
comp=$(echo "${array[w+1]} - ${array[w]} " | bc)
if [ $comp = 1 ]; then
/*???*/
else
/*???*/
fi
done
What I would like to do is to insert a value when the difference between two consecutive elements is not = 1
How can I do it?
Many thanks.
Just create a loop from the minimum to the maximum values and fill the gaps:
array=(1 2 3 4 5 7 8 9 12 13)
min=${array[0]}
max=${array[-1]}
new_array=()
for ((i=min; i<=max; i++)); do
if [[ " ${array[#]} " =~ " $i " ]]; then
new_array+=($i)
else
new_array+=(0)
fi
done
echo "${new_array[#]}"
This creates a new array $new_array with the values:
1 2 3 4 5 0 7 8 9 0 0 12 13
This uses the trick in Check if an array contains a value.
You can select parts of the original array with ${arr[#]:index:count}.
Select the start, insert a new element, add the end.
To insert an element after index i=5 (the fifth element)
$ array=(1 2 3 4 5 7 8 9 12 13)
$ i=5
$ arr=("${array[#]:0:i}") ### take the start of the array.
$ arr+=( 0 ) ### add a new value ( may use $((i+1)) )
$ arr+=("${array[#]:i}") ### copy the tail of the array.
$ array=("${arr[#]}") ### transfer the corrected array.
$ printf '<%s>' "${array[#]}"; echo
<1><2><3><4><5><6><7><8><9><12><13>
To process all the elements, just do a loop:
#!/bin/bash
array=(1 2 3 4 5 7 8 9 12 13)
for (( i=1;i<${#array[#]};i++)); do
if (( array[i] != i+1 ));
then arr=("${array[#]:0:i}") ### take the start of the array.
arr+=( "0" ) ### add a new value
arr+=("${array[#]:i}") ### copy the tail of the array.
# echo "head $i ${array[#]:0:i}" ### see the array.
# echo "tail $i ${array[#]:i}"
array=("${arr[#]}") ### transfer the corrected array.
fi
done
printf '<%s>' "${array[#]}"; echo
$ chmod u+x ./script.sh
$ ./script.sh
<1><2><3><4><5><0><7><8><9><10><0><0><13>
There does not seem to be a way to insert directly into an array. You can append elements to another array instead though:
result=()
for w in ${!array[#]}; do
result+=("${array[w]}")
comp=$(echo "${array[w+1]} - ${array[w]} " | bc)
if [ $comp = 1 ]; then
/* probably leave empty */
else
/* handle missing digits */
fi
done
As a final step, you can assign result back to the original array.
I have the following script which uses awk to match fields with user input
NB=$#
FILE=myfile
#GET INPUT
if [ $NB -eq 1 ]
then
A=`awk -F "\t" -v town="$1" 'tolower($3) ~ tolower(town) {print NR}' $FILE`
fi
If I print the output, it reads :
7188 24369 77205 101441
Which is what I expected. Then if I do the following:
IFS=' '
array=($A)
echo ${#array[#]}
I actually get a length of 1 (?). Furthermore, if I try:
for x in $array
do
echo $x
done
It actually prints out :
7188
24369
77205
101441
How can I have it return the length of 4. I don't understand how the for...in works if there's only 1 element?
EDIT :
echo $A | od -c before I create the array is:
0000000 7 1 8 8 2 4 3 6 9 7 7 2 0 5
0000020 1 0 1 4 4 1 \n
0000030
echo $A | od -c after I create the array is:
0000000 7 1 8 8 \n 2 4 3 6 9 \n 7 7 2 0 5
0000020 \n 1 0 1 4 4 1 \n
0000030
It is because returned output from awk is newline (\n) delimited instead of space delimited. So if you have IFS like this instead:
IFS=$'\n' # newline between quotes
Then it will echo array length = 4 as you are expecting.
Looking the "Array" section in the bash(1) man page, I didn't find a way to slice an array.
So I came up with this overly complicated function:
#!/bin/bash
# #brief: slice a bash array
# #arg1: output-name
# #arg2: input-name
# #args: seq args
# ----------------------------------------------
function slice() {
local output=$1
local input=$2
shift 2
local indexes=$(seq $*)
local -i i
local tmp=$(for i in $indexes
do echo "$(eval echo \"\${$input[$i]}\")"
done)
local IFS=$'\n'
eval $output="( \$tmp )"
}
Used like this:
$ A=( foo bar "a b c" 42 )
$ slice B A 1 2
$ echo "${B[0]}" # bar
$ echo "${B[1]}" # a b c
Is there a better way to do this?
See the Parameter Expansion section in the Bash man page. A[#] returns the contents of the array, :1:2 takes a slice of length 2, starting at index 1.
A=( foo bar "a b c" 42 )
B=("${A[#]:1:2}")
C=("${A[#]:1}") # slice to the end of the array
echo "${B[#]}" # bar a b c
echo "${B[1]}" # a b c
echo "${C[#]}" # bar a b c 42
echo "${C[#]: -2:2}" # a b c 42 # The space before the - is necesssary
Note that the fact that a b c is one array element (and that it contains an extra space) is preserved.
There is also a convenient shortcut to get all elements of the array starting with specified index. For example "${A[#]:1}" would be the "tail" of the array, that is the array without its first element.
version=4.7.1
A=( ${version//\./ } )
echo "${A[#]}" # 4 7 1
B=( "${A[#]:1}" )
echo "${B[#]}" # 7 1
Array slicing like in Python (From the rebash library):
array_slice() {
local __doc__='
Returns a slice of an array (similar to Python).
From the Python documentation:
One way to remember how slices work is to think of the indices as pointing
between elements, with the left edge of the first character numbered 0.
Then the right edge of the last element of an array of length n has
index n, for example:
```
+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
```
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice 1:-2 "${a[#]}")
1 2 3
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice 0:1 "${a[#]}")
0
>>> local a=(0 1 2 3 4 5)
>>> [ -z "$(array.slice 1:1 "${a[#]}")" ] && echo empty
empty
>>> local a=(0 1 2 3 4 5)
>>> [ -z "$(array.slice 2:1 "${a[#]}")" ] && echo empty
empty
>>> local a=(0 1 2 3 4 5)
>>> [ -z "$(array.slice -2:-3 "${a[#]}")" ] && echo empty
empty
>>> [ -z "$(array.slice -2:-2 "${a[#]}")" ] && echo empty
empty
Slice indices have useful defaults; an omitted first index defaults to
zero, an omitted second index defaults to the size of the string being
sliced.
>>> local a=(0 1 2 3 4 5)
>>> # from the beginning to position 2 (excluded)
>>> echo $(array.slice 0:2 "${a[#]}")
>>> echo $(array.slice :2 "${a[#]}")
0 1
0 1
>>> local a=(0 1 2 3 4 5)
>>> # from position 3 (included) to the end
>>> echo $(array.slice 3:"${#a[#]}" "${a[#]}")
>>> echo $(array.slice 3: "${a[#]}")
3 4 5
3 4 5
>>> local a=(0 1 2 3 4 5)
>>> # from the second-last (included) to the end
>>> echo $(array.slice -2:"${#a[#]}" "${a[#]}")
>>> echo $(array.slice -2: "${a[#]}")
4 5
4 5
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice -4:-2 "${a[#]}")
2 3
If no range is given, it works like normal array indices.
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice -1 "${a[#]}")
5
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice -2 "${a[#]}")
4
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice 0 "${a[#]}")
0
>>> local a=(0 1 2 3 4 5)
>>> echo $(array.slice 1 "${a[#]}")
1
>>> local a=(0 1 2 3 4 5)
>>> array.slice 6 "${a[#]}"; echo $?
1
>>> local a=(0 1 2 3 4 5)
>>> array.slice -7 "${a[#]}"; echo $?
1
'
local start end array_length length
if [[ $1 == *:* ]]; then
IFS=":"; read -r start end <<<"$1"
shift
array_length="$#"
# defaults
[ -z "$end" ] && end=$array_length
[ -z "$start" ] && start=0
(( start < 0 )) && let "start=(( array_length + start ))"
(( end < 0 )) && let "end=(( array_length + end ))"
else
start="$1"
shift
array_length="$#"
(( start < 0 )) && let "start=(( array_length + start ))"
let "end=(( start + 1 ))"
fi
let "length=(( end - start ))"
(( start < 0 )) && return 1
# check bounds
(( length < 0 )) && return 1
(( start < 0 )) && return 1
(( start >= array_length )) && return 1
# parameters start with $1, so add 1 to $start
let "start=(( start + 1 ))"
echo "${#: $start:$length}"
}
alias array.slice="array_slice"
At the risk of beating a dead horse, I was inspired by #jandob's answer and made this version that
Is simpler (doesn't have so much shift logic or rewriting of variables as often).
Respects quoted strings without dealing with IFS (-r mode only).
Allows the user to specify [start, end) slicing or [start, length] slicing via -l flag.
Allows you to echo the resulting array (default behavior), or "return" it into a new array for use in the calling parent (via -r slicedArray).
Note: namerefs are only supported in Bash >= 4.3. To support earlier versions of Bash (i.e. Mac without Brew's bash), you'll need to use indirection instead: use a temp var to access array parameters, e.g. declare arrValuesCmd="$1[#]"; declare arr=("${!arrValuesCmd}"), and use eval for return values, e.g. eval $retArrName='("${newArr[#]}")' (note the single quotes around the array declaration).
array.slice() {
# array.slice [-l] [-r returnArrayName] myArray 3 5
# Default functionality is to use second number as end index for slice (exclusive).
# Can instead use second number as length by passing `-l` flag.
# `echo` doesn't maintain quoted entries, so pass in `-r returnArrayName` to keep them.
declare isLength
declare retArrName
declare OPTIND=1
while getopts "lr:" opt; do
case "$opt" in
l)
# If `end` is slice length instead of end index
isLength=true
;;
r)
retArrName="$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
declare -n arr="$1"
declare start="$2"
declare end="$3"
declare arrLength="${#arr[#]}"
declare newArr=()
declare newArrLength
# Bash native slicing:
# Positive index values: ${array:start:length}
# Negative index values: ${array: start: length}
# To use negative values, a space is required between `:` and the variable
# because `${var:-3}` actually represents a default value,
# e.g. `myVar=${otherVal:-7}` represents (pseudo-code) `myVar=otherVal || myVar=7`
if [[ -z "$end" ]]; then
# If no end is specified (regardless of `-l`/length or index), default to the rest of the array
newArrLength="$arrLength"
elif [[ -n "$isLength" ]]; then
# If specifying length instead of end-index, use native bash array slicing
newArrLength="$(( end ))"
else
# If specifying end-index, use custom slicing based on a range of [start, end):
newArrLength="$(( end - start ))"
fi
newArr=("${arr[#]: start: newArrLength}")
if [[ -n "$retArrName" ]]; then
declare -n retArr="$retArrName"
retArr=("${newArr[#]}")
else
echo "${newArr[#]}"
fi
}
Examples:
myArray=(x y 'a b c' z 5 14) # length=6
array.slice myArray 2 4
# > a b c z
array.slice -l myArray 3 2
# > z 5
# Note: Output was manually quoted to show the result more clearly.
# Actual stdout content won't contain those quotes, which is
# why the `-r returnArray` option was added.
array.slice -r slicedArray myArray -5 -3 # equivalent of [2, 4)
# > (null)
echo -e "myArray (length=${#myArray[#]}): ${myArray[#]} \nslicedArray (length=${#slicedArray[#]}): ${slicedArray[#]}"
# > myArray (length=6): x y 'a b c' z 5 14
# > slicedArray (length=2): 'a b c' z
array.slice -lr slicedArray myArray -5 3 # length instead of index, equivalent of [2, 5)
# > (null)
echo -e "myArray (length=${#myArray[#]}): ${myArray[#]} \nslicedArray (length=${#slicedArray[#]}): ${slicedArray[#]}"
# > myArray (length=6): x y 'a b c' z 5 14
# > slicedArray (length=3): 'a b c' z 5