How can I jq a line from a file from a remote server?
I would like to check a particular line from a remote server. So far, I can check if the file exists or not. But can I index a particular line using tool like jq?
if ssh -q -i $pathToPem $sshUsernameIp [ -f ${remote_serial} ];
then
echo "Serial number is found externally"
serialNumber=$(cat serialNumber.json | jq '.serialNumber')
if ssh -q -i $pathToPem $sshUsernameIp [ "$serialNumber" == "${remote_serial | jq '.serialNumber'}" ];
then
echo "Serial number is same"
else
echo "Serial number is not same"
fi
fi
output>
Serial number is found externally
./test.sh: line 127: ${remote_serial | jq '.serialNumber'}: bad substitution
How can I pass variable inside {} there? Can I compare these local and remote files without scp ing?
bash shell-script ssh jq
add a comment |
I would like to check a particular line from a remote server. So far, I can check if the file exists or not. But can I index a particular line using tool like jq?
if ssh -q -i $pathToPem $sshUsernameIp [ -f ${remote_serial} ];
then
echo "Serial number is found externally"
serialNumber=$(cat serialNumber.json | jq '.serialNumber')
if ssh -q -i $pathToPem $sshUsernameIp [ "$serialNumber" == "${remote_serial | jq '.serialNumber'}" ];
then
echo "Serial number is same"
else
echo "Serial number is not same"
fi
fi
output>
Serial number is found externally
./test.sh: line 127: ${remote_serial | jq '.serialNumber'}: bad substitution
How can I pass variable inside {} there? Can I compare these local and remote files without scp ing?
bash shell-script ssh jq
add a comment |
I would like to check a particular line from a remote server. So far, I can check if the file exists or not. But can I index a particular line using tool like jq?
if ssh -q -i $pathToPem $sshUsernameIp [ -f ${remote_serial} ];
then
echo "Serial number is found externally"
serialNumber=$(cat serialNumber.json | jq '.serialNumber')
if ssh -q -i $pathToPem $sshUsernameIp [ "$serialNumber" == "${remote_serial | jq '.serialNumber'}" ];
then
echo "Serial number is same"
else
echo "Serial number is not same"
fi
fi
output>
Serial number is found externally
./test.sh: line 127: ${remote_serial | jq '.serialNumber'}: bad substitution
How can I pass variable inside {} there? Can I compare these local and remote files without scp ing?
bash shell-script ssh jq
I would like to check a particular line from a remote server. So far, I can check if the file exists or not. But can I index a particular line using tool like jq?
if ssh -q -i $pathToPem $sshUsernameIp [ -f ${remote_serial} ];
then
echo "Serial number is found externally"
serialNumber=$(cat serialNumber.json | jq '.serialNumber')
if ssh -q -i $pathToPem $sshUsernameIp [ "$serialNumber" == "${remote_serial | jq '.serialNumber'}" ];
then
echo "Serial number is same"
else
echo "Serial number is not same"
fi
fi
output>
Serial number is found externally
./test.sh: line 127: ${remote_serial | jq '.serialNumber'}: bad substitution
How can I pass variable inside {} there? Can I compare these local and remote files without scp ing?
bash shell-script ssh jq
bash shell-script ssh jq
asked Feb 18 at 13:45
Rakib FihaRakib Fiha
217
217
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
No need to make it complicated. Just grab the JSON off from the remote host and pass it through jq
, then do the same locally. Then compare. If the remote file is missing, you'll get an error (which we can throw away), and the serial numbers will compare different (unless the local serial number is also missing).
r_serial=$( ssh -q -i "$pathToPem" "$sshUsernameIp" cat "$remote_serial" 2>/dev/null | jq -r '.serialNumber' )
l_serial=$( jq -r '.serialNumber' serialNumber.json )
if [ "$r_serial" = "$l_serial" ]; then
echo 'same'
else
echo 'different (or missing)'
fi
This is assuming that $remote_serial
is the pathname of the JSON document on the remote machine.
Note also the quoting of the variable expansions in the call to ssh
.
The actual error you get comes from the fact that
"${remote_serial | jq '.serialNumber'}"
is not a valid variable expansion. You may have wanted to use something like
"$( printf '%sn' "$remote_serial" | jq '.serialNumber' )"
but that assumes that $remote_serial
contains the contents of a JSON document.
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
add a comment |
Looks like it should be
"$(echo ${remote_serial} | jq '.serialNumber')"
instead of
"${remote_serial | jq '.serialNumber'}"
I tried it, seems like I will have difficulty with parsing with this.parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.
– telcoM
Feb 18 at 15:44
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501349%2fhow-can-i-jq-a-line-from-a-file-from-a-remote-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
No need to make it complicated. Just grab the JSON off from the remote host and pass it through jq
, then do the same locally. Then compare. If the remote file is missing, you'll get an error (which we can throw away), and the serial numbers will compare different (unless the local serial number is also missing).
r_serial=$( ssh -q -i "$pathToPem" "$sshUsernameIp" cat "$remote_serial" 2>/dev/null | jq -r '.serialNumber' )
l_serial=$( jq -r '.serialNumber' serialNumber.json )
if [ "$r_serial" = "$l_serial" ]; then
echo 'same'
else
echo 'different (or missing)'
fi
This is assuming that $remote_serial
is the pathname of the JSON document on the remote machine.
Note also the quoting of the variable expansions in the call to ssh
.
The actual error you get comes from the fact that
"${remote_serial | jq '.serialNumber'}"
is not a valid variable expansion. You may have wanted to use something like
"$( printf '%sn' "$remote_serial" | jq '.serialNumber' )"
but that assumes that $remote_serial
contains the contents of a JSON document.
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
add a comment |
No need to make it complicated. Just grab the JSON off from the remote host and pass it through jq
, then do the same locally. Then compare. If the remote file is missing, you'll get an error (which we can throw away), and the serial numbers will compare different (unless the local serial number is also missing).
r_serial=$( ssh -q -i "$pathToPem" "$sshUsernameIp" cat "$remote_serial" 2>/dev/null | jq -r '.serialNumber' )
l_serial=$( jq -r '.serialNumber' serialNumber.json )
if [ "$r_serial" = "$l_serial" ]; then
echo 'same'
else
echo 'different (or missing)'
fi
This is assuming that $remote_serial
is the pathname of the JSON document on the remote machine.
Note also the quoting of the variable expansions in the call to ssh
.
The actual error you get comes from the fact that
"${remote_serial | jq '.serialNumber'}"
is not a valid variable expansion. You may have wanted to use something like
"$( printf '%sn' "$remote_serial" | jq '.serialNumber' )"
but that assumes that $remote_serial
contains the contents of a JSON document.
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
add a comment |
No need to make it complicated. Just grab the JSON off from the remote host and pass it through jq
, then do the same locally. Then compare. If the remote file is missing, you'll get an error (which we can throw away), and the serial numbers will compare different (unless the local serial number is also missing).
r_serial=$( ssh -q -i "$pathToPem" "$sshUsernameIp" cat "$remote_serial" 2>/dev/null | jq -r '.serialNumber' )
l_serial=$( jq -r '.serialNumber' serialNumber.json )
if [ "$r_serial" = "$l_serial" ]; then
echo 'same'
else
echo 'different (or missing)'
fi
This is assuming that $remote_serial
is the pathname of the JSON document on the remote machine.
Note also the quoting of the variable expansions in the call to ssh
.
The actual error you get comes from the fact that
"${remote_serial | jq '.serialNumber'}"
is not a valid variable expansion. You may have wanted to use something like
"$( printf '%sn' "$remote_serial" | jq '.serialNumber' )"
but that assumes that $remote_serial
contains the contents of a JSON document.
No need to make it complicated. Just grab the JSON off from the remote host and pass it through jq
, then do the same locally. Then compare. If the remote file is missing, you'll get an error (which we can throw away), and the serial numbers will compare different (unless the local serial number is also missing).
r_serial=$( ssh -q -i "$pathToPem" "$sshUsernameIp" cat "$remote_serial" 2>/dev/null | jq -r '.serialNumber' )
l_serial=$( jq -r '.serialNumber' serialNumber.json )
if [ "$r_serial" = "$l_serial" ]; then
echo 'same'
else
echo 'different (or missing)'
fi
This is assuming that $remote_serial
is the pathname of the JSON document on the remote machine.
Note also the quoting of the variable expansions in the call to ssh
.
The actual error you get comes from the fact that
"${remote_serial | jq '.serialNumber'}"
is not a valid variable expansion. You may have wanted to use something like
"$( printf '%sn' "$remote_serial" | jq '.serialNumber' )"
but that assumes that $remote_serial
contains the contents of a JSON document.
edited Feb 18 at 13:57
answered Feb 18 at 13:52
KusalanandaKusalananda
134k17255418
134k17255418
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
add a comment |
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
Thank you. This one makes it look clean as well.
– Rakib Fiha
Feb 18 at 15:30
add a comment |
Looks like it should be
"$(echo ${remote_serial} | jq '.serialNumber')"
instead of
"${remote_serial | jq '.serialNumber'}"
I tried it, seems like I will have difficulty with parsing with this.parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.
– telcoM
Feb 18 at 15:44
add a comment |
Looks like it should be
"$(echo ${remote_serial} | jq '.serialNumber')"
instead of
"${remote_serial | jq '.serialNumber'}"
I tried it, seems like I will have difficulty with parsing with this.parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.
– telcoM
Feb 18 at 15:44
add a comment |
Looks like it should be
"$(echo ${remote_serial} | jq '.serialNumber')"
instead of
"${remote_serial | jq '.serialNumber'}"
Looks like it should be
"$(echo ${remote_serial} | jq '.serialNumber')"
instead of
"${remote_serial | jq '.serialNumber'}"
answered Feb 18 at 13:49
telcoMtelcoM
18.6k12347
18.6k12347
I tried it, seems like I will have difficulty with parsing with this.parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.
– telcoM
Feb 18 at 15:44
add a comment |
I tried it, seems like I will have difficulty with parsing with this.parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.
– telcoM
Feb 18 at 15:44
I tried it, seems like I will have difficulty with parsing with this.
parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
I tried it, seems like I will have difficulty with parsing with this.
parse error: Invalid numeric literal at line 2, column 0 bash: line 0: [: sfsdfsdfdfsdfsd: unary operator expected Serial number is not same
– Rakib Fiha
Feb 18 at 15:38
1
1
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the
${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.– telcoM
Feb 18 at 15:44
I see Kusalananda already supplied you with a better solution with proper quoting. My assumption that the
${remote_serial}
might not have spaces nor any other problematic characters was probably incorrect. Kusalananda's solution has properly defensive quoting to handle that.– telcoM
Feb 18 at 15:44
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501349%2fhow-can-i-jq-a-line-from-a-file-from-a-remote-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown