0

Assuming one has the following directory:

mkdir """
'''\n
"

And one queries the directory with:

stat --printf '%n' **  #or for simplicity stat --printf '%n' $'\n'\'\'\'\\n$'\n'

And captures the returned filename from stdout in a variable getting the correct binary representation of the filename:

#this should be a new line which I can't show
'''\n

How can one send that variable to execute subsequent commands on stdin and have them ALWAYS work e.g. sending it as a string fails:

stat 
'''\n

This works for most cases but not all:

stat "/path/here"

The command below also works for most cases but not all:

p="$(cat<<EOF

'''\n
EOF
)"
stat -- "$p"

If I use the captured variable as an arguement via node's spawn it works - so the capture from stat is correct and the problem is the translation that occurs when sending the variable as a string to stdin - which is required

  • "the capture from stat is correct" ... I doubt that. If you used command substitution to get the output of stat, like you did with cat and heredoc, then the trailing newline will be missing, because command substitution removes all trailing newlines. – Olorin Mar 8 at 1:47
  • @Olorin - the stdout produced by stat is read into an array of bytes, transform into a string and then used to execute other commands. If the same command is sent using node.spawn("stat", [filename]) it works fine. However, when I send it spwn.stdin.write("stat ${filename}") it fails due to string translation of strange characters. – TrevTheDev Mar 8 at 2:20
  • So this is about variables and capturing in nodejs? That looks like a nodejs programming problem and should be posted on Stack Overflow. At first I thought you were trying to capture output in a shell variable and somehow passing it on to nodejs. – Olorin Mar 8 at 2:34
  • @Olorin it is not really a node issue, the question is how would one take a byte stream and format it - as if one was typing it into the terminal byte for byte and guarantee that the terminal will read and interpret the inputted byte stream correctly. When working with 'normal' text this just works, but when you mix in " and \n and other strange bytes things break. So the question is how could that be avoided? – TrevTheDev Mar 8 at 2:47
  • Doesn't nodejs have a shellescape or similar function (like Python's shlex.quote)? If it's breaking due to special characters, you may need to quote it. It all depends on how nodejs's spawn functions, and that's why it's a node issue - does it parse and exec the command itself? If so, what characters are special for this parsing? Does it call a shell? If so, which shell and how? – Olorin Mar 8 at 2:52

Your Answer

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

Browse other questions tagged or ask your own question.