0

I'm trying to get a line number with the following code, where beta.plist is on the remote machine.

lineNum="$(awk '/bundle-version/{ print NR; exit }' ~/beta.plist)" \

When executing on a remote client through ssh it is giving me the error 'No Such File Or Directory'. I'm assuming this is because it's trying to use my local directory instead of the remote directory, because of the command substitution, but I'm not sure how to tell it to use the remote directory instead. I've tried many things from various SO posts, but none worked. Both machines are Macs if that makes a difference.

Code:

ssh -t host "cd path-to-file; \
lineNum="$(awk '/bundle-version/{ print NR; exit }' ./beta.plist)""

Error Message: awk: can't open file ./beta.plist source line number 1

Alex
  • 3,861
  • 5
  • 28
  • 44
  • 1
    Include the ssh invocation as part of the question. If you're running `ssh < – Charles Duffy Sep 15 '20 at 17:53
  • 1
    (if you confirm that's the issue, btw, it should be pretty easy to find a duplicate; to summarize, though, you need to quote your heredocs if you don't want parameter substitutions and command substitutions they contain evaluated locally; `ssh <<'EOF'` will suppress this). – Charles Duffy Sep 15 '20 at 17:54
  • 1
    ...saying you "tried many things" doesn't tell us what the things are, and doesn't tell us _how_ they failed to work -- what you wanted them to do, or what they did instead. As such, it's a statement that provides no guidance to help inform what kind of answer you need. Don't just say you tried many things; show the things you tried, and show how they failed. – Charles Duffy Sep 15 '20 at 17:56
  • 1
    Add full error message to your question. – Cyrus Sep 15 '20 at 17:57
  • @CharlesDuffy Thanks for the response. I'm not sure how to format the ssh << 'EOF' , nor am I sure how the other post answers my question. – Alex Sep 15 '20 at 18:09
  • 1
    Okay, now that you include the ssh invocation it's more clear why you have the problem. The easy thing to do (that takes advantage of the linked question's answers) is to make it `ssh host 'bash -s' <<'EOF'` on the first line, then `cd path-to-file` on a second line, and `lineNum="$(awk '/bundle-version/{ print NR; exit }' ./beta.plist)"` on a third line, with an unindented line containing only `EOF` at the point where you want to return to the local machine. Note that `lineNum` will be set in the _remote_ shell, not the local one. – Charles Duffy Sep 15 '20 at 18:18
  • 1
    ...if you want to set `lineNum` on the _local_ shell, consider `lineNum=$(ssh host 'bash -s' <<'EOF'` locally, then your code to run on the remote shell (which needs to write the value you want to store to stdout), then an unindented line containing only `EOF`, then a line containing `)`. – Charles Duffy Sep 15 '20 at 18:20
  • 1
    Another thing to keep in mind: When you use multiple double-quotes, they don't nest; instead, they switch in and out of a double-quoted context. So, for example, `"foo"'bar'baz` is the string `foobarbaz`, with the first characters double-quoted, the middle characters single-quoted, and the last characters unquoted. Similarly, when you write `""` in the middle of a command in an unquoted context, it does nothing at all: It starts a double-quoted context but immediately ends it, with no characters between. (Or if you're already in a double-quoted context it ends that context and restarts it). – Charles Duffy Sep 15 '20 at 18:23
  • @CharlesDuffy Thanks for the help I appreciate it. My script is close to working now, but I need to use a variable outside the ssh, which was there before, but is gone now. Is there an easy way to pass a variable along? – Alex Sep 15 '20 at 18:41
  • @CharlesDuffy Nevermind I found a SO post where you answered this already! =D – Alex Sep 15 '20 at 18:48
  • 1
    First question: Is the variable numeric, or are its contents arbitrary? To pass an arbitrary string safely, you're looking at something like `printf -v arg_q '%q' "$arg"`, then `ssh host "bash -s $arg_q"`. – Charles Duffy Sep 15 '20 at 18:48
  • 1
    Heh, great; glad you've gotten things squared away. – Charles Duffy Sep 15 '20 at 18:48

0 Answers0