1

I have this output:

$ ./adb devices

List of devices attached
R9WM80DNNPJ     device

(eventually this will return multiple lines of devices such as:

List of devices attached
R9WM80DNNPJ     device
R9WM80DNNPA     device
R9WM80DNNPB     device
R9WM80DNNPC     device

)

and I am trying to get the device name to pass it to multiple other commands. Trying to eventually get to a script that looks like this (it will do more than echo ofc):

adb devices |  more +1 | cut -sf 1 | xargs -I % sh -c '{ ^
echo % ; ^
echo "Test %"; ^
}'

When I put the command all on one line, the cygwin terminal has no problem echo'ing it out:

correct terminal result

When I move that command into a file, it no longer works correctly (the first line output is a print of the command used, but then it doesn't do the first echo):

single-line script

single-line command result

And running the multi-line command does not work either:

multi-line script

multi-line command result

My guess is I am doing something wrong with the new line characters? or is it something to do with sh -c? or both!

3
  • Are you writing a shell script or a batch file?
    – choroba
    Feb 3, 2020 at 10:34
  • I have a working linux version of this script. So want to write something as close to that as possible. So ideally bash script that runs in cygwin on windows. (Please tell me if I've got this all mixed up :-) )
    – Blundell
    Feb 3, 2020 at 10:43
  • Instead of notepad consider using a text editor that will write UNIX line endings by default or can be configured to. Or at least run dos2unix over the file after saving.
    – Iguananaut
    Feb 4, 2020 at 17:24

1 Answer 1

0

Moving to the default argument list marker {} has resolved this problem!

adb devices |  more +1 | cut -sf 1 | xargs -I {} sh -c {' ^
    echo {}; ^
    echo "Test {}"; ^
'}

enter image description here

enter image description here

1
  • 1
    Make sure your script file uses linux line endings (\n) instead of windows (\r\n). Also put in a shebang line as first line, I.e. #!/usr/bin/bash. Feb 4, 2020 at 8:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.