0

The variable I wish to extract will look something like this

ttyACM0
ttyACM1

I would like to iterate over this variable and issue the command:

echo disconnect > /dev/ttyACM0

and then

echo disconnect > /dev/ttyACM1

My current code that isn't working looks like:

MCCI=`ls /dev | grep "ttyACM[0-9]" | grep -v "$all_dongles"`

while IFS= read -r line do     echo disconnect > /dev/"$line"     sleep 1     echo disconnect > /dev/"$line"     sleep 1     echo disconnect > /dev/"$line" done <<< "$MCCI"

The code is currently overwriting ttyACM0 which is obviously not what I want. I'm issuing the same disconnect code 3 times because it was advised to do so, after the disconnect loop I must issue a connect code as well, which is identical to the above code. This entire code is executed over SSH

UPDATE I am connecting to a machine via ssh and I am trying to verify the serial number of a dongle that I prompt the user for. I need to disconnect unnecessary dongles that do not match the serial number provided by the user. In order to do this I must verify which MCCI corresponds to which dongle. All dongles and MCCI are /dev/ttyACM[0-9] My thinking was to disconnect all MCCIs (For which I know the numbers of thanks to the MCCI variable) and then turn them back on one at a time, check which dongle is now connected, grep the dongle serial number and concatenate it to the corresponding MCCI ttyACM#, and add that to an array, disconnect again I then plan to iterate over that array and search for the serial number, remove the relevant ttyACM# and connect the dongle

I have 3 variables Dongles & MCCI - ls /dev | grep "ttyACM[0-9]"

Dongles - a similar variable that returns information about the dongles like serial number and ttyACM[0-9]

and the MCCI which is just excluding the known dongles from the Dongles & MCCI variable

Nir
  • 1
  • 1
    Your MCCI= is .. not great, you might want to read https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead – Marcus Müller Nov 08 '22 at 15:58
  • What's in $all_dongles? – Kusalananda Nov 08 '22 at 16:05
  • $all_dongles is another variable containing similar output ttyACM2 ttyACM3 for example – Nir Nov 08 '22 at 16:12
  • 1
    @Nir then your grep -v "$all_dongles" makes no sense. – Marcus Müller Nov 08 '22 at 16:24
  • there are 4 devices: ttyACM0, ttyACM1, ttyACM2, and ttyACM3 I wanted to separate dongles from MCCIs so i excluded them – Nir Nov 08 '22 at 16:32
  • @Nir please edit your question and tell us what you are trying to do. The code you are using is just not a good way of doing anything, so we cannot use the code to guess what you need. You need to explain your setup (edit your question and add the output of ls /dev/tty*) and which devices you want to include and which devices you want to exclude. – terdon Nov 08 '22 at 18:00
  • @terdon editing now to make it more clear – Nir Nov 08 '22 at 19:15

3 Answers3

0

Honestly, since you know the input can't contain any other space-like characters aside from "newline" separating your entries, a simple

for device in /dev/ttyACM{0..9}*; do
  {echo $(basename "$device") | grep "$alldongles"} && continue
  for run in {1..3}; do
    echo disconnect > "$device"
    sleep 1
  done
done

would do. No use to parse ls output, ever!

  • I'm just wondering where is the reference to MCCI? – Nir Nov 08 '22 at 16:21
  • why should there be a reference to MCCI? – Marcus Müller Nov 08 '22 at 16:22
  • because the disconnect code I want to issue is based on the ttyACM# in the MCCI variable – Nir Nov 08 '22 at 16:23
  • I mean, the way you built the MCCI variable was pretty suboptimal. My code doesn't need that variable to achieve the same. – Marcus Müller Nov 08 '22 at 16:23
  • just to clarify i have 3 variables all built in a similar way MCCI_and_dongles, all_dongles, MCCI

    for the disconnect code i only want to use the results of the MCCI variable

    – Nir Nov 08 '22 at 16:27
  • again, then build these better than you do. – Marcus Müller Nov 08 '22 at 16:34
  • i'm not sure what the issue is, i've echoed the variables and they print exactly what i need them to – Nir Nov 08 '22 at 16:45
  • see the comment I left under your question, and how the other answer here also isn't building the list of candidate devices the way you do, namely by parsing ls. Which you really should not do. – Marcus Müller Nov 08 '22 at 16:46
  • i saw, i don't think it's an issue for me specifically with this code because i'm grepping for ttyACM[0-9] – Nir Nov 08 '22 at 17:05
  • please read the answers to the thing I linked to – Marcus Müller Nov 08 '22 at 17:19
  • i guess i'm not understanding it fully i'm a bit new to shell scripting – Nir Nov 08 '22 at 17:42
  • @MarcusMüller either explain to Nir or don't, but please don't leave patently unhelpful comments like "build these better than you do". We are not all expert script writers, obviously if Nir could build them better they would have done so, so either explain how or don't engage, but don't just leave condescending comments! – terdon Nov 08 '22 at 17:56
  • @terdon but I illustrated how to build a better way to iterate through the files in question in my answer. – Marcus Müller Nov 08 '22 at 18:30
  • the issue with the code here is that if there ttyACMs start at ttyACM0 and go up to ttyACM3, it will create a ttyACM4 up to ttyACM9 unnecessarily – Nir Nov 08 '22 at 22:19
0

Apart from how you are setting MCCI, your method for iteration over each line of text in a variable is reasonable, but you may find it simpler not to use the variable at all. Instead, simply iterate over the wildcard /dev/ filespec:

all_dongles='
/dev/ttyACM4
/dev/ttyACM9
/dev/ttyACM7
'
for line in /dev/ttyACM[0-9]
do
  if grep -Fqx "$line" <<<"$all_dongles"
  then
    : # skip
  else
  # ... do something with $line
  fi
done
Jim L.
  • 7,997
  • 1
  • 13
  • 27
0

Assuming [237] matches the digit at the end of any device name that is associated with a dongle (this could also be written 2|3|7):

shopt -s extglob

dongle='[237]' for device in /dev/ttyACM!($dongle); do printf '%s\n' disconnect disconnect disconnect >"$device" done

The extended globbing pattern /dev/ttyACM!([237]) matches any pathnames that start with /dev/ttyACM, but that does not continue with the digits 2, 3 or 7.

The body of the loop prints the string disconnect three times to the device.

Kusalananda
  • 333,661