1

This is my Bash script that reads values from a temperature sensor and shows them in a line. I want to separate my array elements by a comma, like this example: [1,2,3,5], then to replace a line that exists in another file by this string.

temp.sh:

#!/bin/bash
for ((i=0 ; 12 - $i ; i++))
  do
  x=$(cat /sys/bus/w1/devices/28-0000075292ed/w1_slave | grep "t=" | awk -F "t=" '{print $2/1000}')
  field[$i]=$x
  echo "${field[$i]}"
done | column
7
  • Is column doing anything here? Does the echo output more than one value on a given line? What does the output of this command look like exactly? What about if you don't use column? Commented Mar 30, 2016 at 22:40
  • Why are you sampling the same device each time through the loop? Commented Mar 30, 2016 at 22:41
  • @EtanReisner I'm assuming to get temperatures multiple times and average the value or something similar. Although many of these sensors don't like being read over and over without sleeping inbetween. Commented Mar 30, 2016 at 22:45
  • Are you saying you want the values of field as a string? You can always do mystr="${field[@]}" for a space separated string of values. If you want to remove the spaces, then tr -d ' ' <<<$mystr. Commented Mar 30, 2016 at 23:00
  • @Etan Reisner column gives me the opportunity to have the array elements horizontally I am sampling the same device to see the change of temperature and show them in graph ( the file that I said I want to access is a html file that contains the graph) Commented Mar 31, 2016 at 11:02

2 Answers 2

5

So one way we could do this is to emulate the join() functionality provided by many languages:

function join()
{
    local IFS="$1"
    shift
    echo "$*"
}

Then we can call:

READINGS="[$(join "," "${field[@]})"]"

Here's an example:

$ MY_ARRAY=("one" "two" "three")
$ join "," "${MY_ARRAY[@]}"
one,two,three

This works by changing Bash's Internal Field Separator. $* is the used for expanding all arguments using the $IFS as a separator.

We could also avoid the join function and just do it like this:

READINGS="[$(IFS=","; echo "${field[*]}")]"
Sign up to request clarification or add additional context in comments.

2 Comments

when I tried it , it shows the same result as without IFS : pi@raspberrypi:~ $ ./temp.sh 21.437 21.437 21.437 21.437 21.437 21.437 21.437 21.437 21.5 21.5 21.5 21.5
If not, try posting your new code in an edit to the question and I'll take a look.
1

Thank you I have found a solution

#!/bin/bash
f="myArray_a = ["
for ((i=0 ; 12 - $i ; i++))
  do
  x=$(cat /sys/bus/w1/devices/28-0000075292ed/w1_slave | grep "t=" | awk - F "t=" '{print $2/1000}')
  if [ $i -eq 11 ]
  then
  x=$(printf %.3f] $x)
  f="$f $x"
  echo "$f"
  else
  x=$(printf %.3f, $x)
  f="$f $x"
  fi



done | column

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.