2

I'm creating script that will display 2 times the song name in FM radio station name. Radio station name is limited to 8 characters. Lets say I have this line: Best Direction - I feel good

The code will look like this: (the "PS" is start of line software know it's command to change name)

echo "PS Best" > $fifo;
sleep 1;
echo "PS Directio" > $fifo;
sleep 1;
echo "PS n - I" > $fifo;
sleep 1;
echo "PS feel" > $fifo;
sleep 1;
echo "PS good" > $fifo;
sleep 1;
//repeat this one more time

How to do that in bash script? I have no idea.

2

With fold from GNU Core Utilities:

a="Best Direction - I feel good"
fold -s -w 8 <<< "$a" | while read -r line; do 
  echo "PS $line" > "$fifo"
 sleep 1
done

Output to $fifo:

PS Best
PS Directio
PS n - I
PS feel
PS good

Without fold with same output to $fifo:

a="Best Direction - I feel good"
while [[ -n $a ]]; do     # loop until $a is empty
  if [[ ${#a} -gt 8 ]]; then
    b="${a:0:8}"          # get first 8 characters of $a
    c="${b% *}"           # crop all from right incl. first whitespace
  else
    c="$a"
  fi
  echo "PS $c" > "$fifo"
  d="${a#$c}"             # crop $c left from string $a
  a="${d# *}"             # remove a leading whitespace
  sleep 1
done
  • It giving me error Syntax error: redirection unexpected. Maybe its because I have the update code part in: while true; do; sleep 1; done to run the update again and again forever. – user73216 Dec 26 '15 at 17:21
  • Sorry, I forgot so specifi /bin/bash :D – user73216 Dec 26 '15 at 17:29
  • Now it works perfectly! – user73216 Dec 26 '15 at 17:30
  • 3
    Note that GNU fold only works with single-byte characters. Won't work properly with UTF-8 like in fold -s -w 8 <<< "La grande traversée". Then again, we don't know what charset is expected at the other end of that fifo, or if 8 is the number of characters or the display width of the string. – Stéphane Chazelas Dec 26 '15 at 17:37
  • @StéphaneChazelas: Thanks for the useful note. I agree with you. – Cyrus Dec 26 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy