Reference the stdin sent from piped sender process

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Say I have this:
delete_lock ()
if grep -q 'PATTERN'; then
# some command here
fi
cat >/dev/null
node foo.js | delete_lock
say the node.js process writes "foodog" to delete_lock, or it sends "sandbag". (It could be any unique name...)
How can I read that in the delete_lock function? delete_lock is going to receive some unique name and then delete the lock with that name from the filesystem...how can I do that?
The only thing that I can guess, is something like this:
delete_lock ()
if grep -q 'lockname:xxx'; then
release_lock $xxx
fi
cat >/dev/null
but how do I reference the variable $xxx lol?
bash shell-script pipe
add a comment |Â
up vote
0
down vote
favorite
Say I have this:
delete_lock ()
if grep -q 'PATTERN'; then
# some command here
fi
cat >/dev/null
node foo.js | delete_lock
say the node.js process writes "foodog" to delete_lock, or it sends "sandbag". (It could be any unique name...)
How can I read that in the delete_lock function? delete_lock is going to receive some unique name and then delete the lock with that name from the filesystem...how can I do that?
The only thing that I can guess, is something like this:
delete_lock ()
if grep -q 'lockname:xxx'; then
release_lock $xxx
fi
cat >/dev/null
but how do I reference the variable $xxx lol?
bash shell-script pipe
2
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
2
if xxx=$(grep -o 'pattern'); then ...; fi
â jordanm
Apr 20 at 20:55
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Say I have this:
delete_lock ()
if grep -q 'PATTERN'; then
# some command here
fi
cat >/dev/null
node foo.js | delete_lock
say the node.js process writes "foodog" to delete_lock, or it sends "sandbag". (It could be any unique name...)
How can I read that in the delete_lock function? delete_lock is going to receive some unique name and then delete the lock with that name from the filesystem...how can I do that?
The only thing that I can guess, is something like this:
delete_lock ()
if grep -q 'lockname:xxx'; then
release_lock $xxx
fi
cat >/dev/null
but how do I reference the variable $xxx lol?
bash shell-script pipe
Say I have this:
delete_lock ()
if grep -q 'PATTERN'; then
# some command here
fi
cat >/dev/null
node foo.js | delete_lock
say the node.js process writes "foodog" to delete_lock, or it sends "sandbag". (It could be any unique name...)
How can I read that in the delete_lock function? delete_lock is going to receive some unique name and then delete the lock with that name from the filesystem...how can I do that?
The only thing that I can guess, is something like this:
delete_lock ()
if grep -q 'lockname:xxx'; then
release_lock $xxx
fi
cat >/dev/null
but how do I reference the variable $xxx lol?
bash shell-script pipe
asked Apr 20 at 20:31
Alexander Mills
1,885929
1,885929
2
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
2
if xxx=$(grep -o 'pattern'); then ...; fi
â jordanm
Apr 20 at 20:55
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57
add a comment |Â
2
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
2
if xxx=$(grep -o 'pattern'); then ...; fi
â jordanm
Apr 20 at 20:55
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57
2
2
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
2
2
if xxx=$(grep -o 'pattern'); then ...; fiâ jordanm
Apr 20 at 20:55
if xxx=$(grep -o 'pattern'); then ...; fiâ jordanm
Apr 20 at 20:55
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
I'm assuming your xxx is a stand-in for a pattern of some sort.
delete_lock ()
LOCK="$(grep -m1 -o "lockname:xxx")"
if [ -n "$LOCK#lockname:" ]; then
release_lock "$LOCK#lockname:"
fi
node foo.js | delete_lock
This will stop processing data once it observes the first match for lockname:xxx. It then uses parameter expansion to remove the lockname: prefix and, assuming there was a match (e.g. xxx, runs release_lock on it (e.g. release_lock "xxx").
If you don't want the node call to be cut short, or you want more than one match, remove the -m1 option to grep, though note the quotes may not work with your release_lock code (and be careful about removing those quotes, you don't want to allow rogue characters!).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I'm assuming your xxx is a stand-in for a pattern of some sort.
delete_lock ()
LOCK="$(grep -m1 -o "lockname:xxx")"
if [ -n "$LOCK#lockname:" ]; then
release_lock "$LOCK#lockname:"
fi
node foo.js | delete_lock
This will stop processing data once it observes the first match for lockname:xxx. It then uses parameter expansion to remove the lockname: prefix and, assuming there was a match (e.g. xxx, runs release_lock on it (e.g. release_lock "xxx").
If you don't want the node call to be cut short, or you want more than one match, remove the -m1 option to grep, though note the quotes may not work with your release_lock code (and be careful about removing those quotes, you don't want to allow rogue characters!).
add a comment |Â
up vote
1
down vote
I'm assuming your xxx is a stand-in for a pattern of some sort.
delete_lock ()
LOCK="$(grep -m1 -o "lockname:xxx")"
if [ -n "$LOCK#lockname:" ]; then
release_lock "$LOCK#lockname:"
fi
node foo.js | delete_lock
This will stop processing data once it observes the first match for lockname:xxx. It then uses parameter expansion to remove the lockname: prefix and, assuming there was a match (e.g. xxx, runs release_lock on it (e.g. release_lock "xxx").
If you don't want the node call to be cut short, or you want more than one match, remove the -m1 option to grep, though note the quotes may not work with your release_lock code (and be careful about removing those quotes, you don't want to allow rogue characters!).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I'm assuming your xxx is a stand-in for a pattern of some sort.
delete_lock ()
LOCK="$(grep -m1 -o "lockname:xxx")"
if [ -n "$LOCK#lockname:" ]; then
release_lock "$LOCK#lockname:"
fi
node foo.js | delete_lock
This will stop processing data once it observes the first match for lockname:xxx. It then uses parameter expansion to remove the lockname: prefix and, assuming there was a match (e.g. xxx, runs release_lock on it (e.g. release_lock "xxx").
If you don't want the node call to be cut short, or you want more than one match, remove the -m1 option to grep, though note the quotes may not work with your release_lock code (and be careful about removing those quotes, you don't want to allow rogue characters!).
I'm assuming your xxx is a stand-in for a pattern of some sort.
delete_lock ()
LOCK="$(grep -m1 -o "lockname:xxx")"
if [ -n "$LOCK#lockname:" ]; then
release_lock "$LOCK#lockname:"
fi
node foo.js | delete_lock
This will stop processing data once it observes the first match for lockname:xxx. It then uses parameter expansion to remove the lockname: prefix and, assuming there was a match (e.g. xxx, runs release_lock on it (e.g. release_lock "xxx").
If you don't want the node call to be cut short, or you want more than one match, remove the -m1 option to grep, though note the quotes may not work with your release_lock code (and be careful about removing those quotes, you don't want to allow rogue characters!).
answered Apr 20 at 22:33
Adam Katz
1,922920
1,922920
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f439021%2freference-the-stdin-sent-from-piped-sender-process%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
Does the Node application produce a single line with a value that you want to capture, or is it a long stream that you don't want to interrupt but you'd like to find a pattern in and use it?
â Kusalananda
Apr 20 at 20:34
2
if xxx=$(grep -o 'pattern'); then ...; fiâ jordanm
Apr 20 at 20:55
@Kusalananda probably ongoing stream, not just one line, but it would be nice to know how to do it in both scenarios
â Alexander Mills
Apr 20 at 20:57