0

So i have a file "scenes.txt", containing a 2-column set of numbers, about 500 lines in total:

0 01
6 22
5 03
4 97
7 05
3 98
2 99
9 00 etc...

First column range is 0-9, second column range is 0-99. The 2 fields are tab-separated. On each invocation of the shell script, I want the script to output the set of the 2 numbers from the next line in the list to 2 variables.

So if I run the script for the first time, var1 should be 0 and var2 should be 01, on the next run of the script var1 should be 6 and var2 should be 22, and so on... These values are going to the sendmidi command, https://github.com/gbevin/SendMIDI, to control a piece of hardware via MIDI.
You could call it a clumsy MIDI command line sequencer.

what I tried so far: I created a bash script "test1":

#!/bin/bash
read LINECNT <  pointer
echo "next line to be processed: $LINECNT"
VAR1=$(sed "${LINECNT}q;d" scenes.txt)
echo "Line and Variables: $LINECNT: $VAR1"
# here the actual magic happens:
/usr/local/bin/sendmidi dev "USB2.0-MIDI Anschluss 2" cc 32 $VAR1 PC $VAR2
((LINECNT++))
echo $LINECNT > pointer

and another file named "pointer", which just holds the pointer position for the value to output upon next invocation.

File pointer:

1

This results in:

[dirk@dirks-MacBook-Pro] ~  
❯ ./test2
next line to be processed: 1
Line and Variables: 1: 0 01

[dirk@dirks-MacBook-Pro] ~  
❯ ./test2
next line to be processed: 2
Line and Variables: 2: 6 22

[dirk@dirks-MacBook-Pro] ~  
❯ ./test2
next line to be processed: 3
Line and Variables: 3: 5 03

[dirk@dirks-MacBook-Pro] ~  
❯ ./test2
next line to be processed: 4
Line and Variables: 4: 4 97

[dirk@dirks-MacBook-Pro] ~  
❯ ./test2
next line to be processed: 5
Line and Variables: 5: 7 05

My Problem: There are two Values in each line of "scenes.txt", but the VAR=$(sed "${LINECNT}q;d" scenes.txt) line only gives the whole line with both values. I tried several "cut" modifications, like this

VAR1=cut -f1 - $(sed "${LINECNT}q;d" scenes.txt)
VAR2=$(cut -f2 $(sed "${LINECNT}q;d" scenes.txt))

but with no luck... How do I push the two values from that line into VAR1 and VAR2?

John Conde
  • 217,595
  • 99
  • 455
  • 496
shivahoj
  • 1
  • 2
  • Please try: `read -r VAR1 VAR2 < <(sed -n "${LINECNT}p" scenes.txt)`. Btw by convention, environment variables and internal shell variables are capitalized. All other variable names should be lower case. – tshiono Feb 17 '21 at 00:40

2 Answers2

0

Your sed commands are incorrect. Also, the variables can be read with a single read command:

#!/bin/bash

read -r linecnt < pointer
read -r var1 var2 < <(sed "$linecnt!d; q" scenes.txt)
/usr/local/bin/sendmidi dev "USB2.0-MIDI Anschluss 2" cc 32 "$var1" PC "$var2"
echo $((linecnt + 1)) > pointer

I changed your variable names to lower case since, by convention, capitalized variables are environment variables and internal shell variables.

M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
0
IFS='\t'
arr=($VAR1)
/usr/local/bin/sendmidi dev "USB2.0-MIDI Anschluss 2" cc 32 ${arr[0]} PC ${arr[1]}
     
user2272143
  • 469
  • 5
  • 22
  • 2
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 17 '21 at 05:55